def deletejob(job_id): '''API endpoint to delete a print job. Response body is empty. Response codes: 200 - print job deleted successfully 400 - invalid request - missing print job id 401 - invalid credentials 504 - database error 502 - scraping error ''' if job_id is None: abort(400, 'Missing print job id.') try: email, password = util.get_current_user_credentials() except ValueError: abort(401) username = email.split('@')[0] try: uniflow = printstatus.get_uniflow_client(username, password) uniflow.delete_print_jobs([job_id]) except printstatus.InvalidCredentialsError: abort(401) except printstatus.NetworkError: abort(504) except printstatus.ScrapingError: abort(502) return '', 200
def login(): """API endpoint to login. Saves email and password in the session cookie. Expects an email and password as POST data. Response body is empty. Response codes: 200 - login successful 400 - invalid request - missing email or password 401 - invalid credentials 504 - error connecting to uniflow 502 - unexpected response from uniflow, the scraper is broken """ session.clear() email = request.form.get('email') password = request.form.get('password') if email is None or password is None: abort(400) email = email.strip() try: username = email.split('@')[0] except IndexError: abort(401) try: printstatus.get_uniflow_client(username, password) except printstatus.InvalidCredentialsError: abort(401) except printstatus.NetworkError: abort(504) except printstatus.ScrapingError: abort(502) session['email'] = email session['password'] = password session.permanent = True return '', 200
def uniflowstatus(): """API endpoint to get a user's print budget and print queue. Response body is JSON. Response codes: 200 - budget and print queue retrieved successfully 401 - invalid credentials 504 - error connecting to uniflow 502 - unexpected response from uniflow; the scraper is broken """ try: email, password = util.get_current_user_credentials() except ValueError: abort(401) username = email.split('@')[0] try: uniflow = printstatus.get_uniflow_client(username, password) queue = uniflow.get_print_queue() budget = uniflow.get_budget() except printstatus.InvalidCredentialsError: abort(401) except printstatus.NetworkError: abort(504) except printstatus.ScrapingError: abort(502) jobs = [] for job in queue: parsed_job = job._asdict() parsed_job['color'] = _is_color_job(job) jobs.append(parsed_job) response = {} response['queue'] = jobs response['budget'] = budget return flask.jsonify(**response), 200