def run_training_job(): """POST call for initiating retraining of models.""" required_fields = [ "data_version", "bucket_name", "github_repo", "ecosystem" ] input_data = request.get_json() missing_fields = check_field_exists(input_data, required_fields) if missing_fields: raise HTTPError( 400, "These field(s) {} are missing from input " "data".format(missing_fields)) if not input_data: raise HTTPError(400, "Expected JSON request") if type(input_data) != dict: raise HTTPError(400, "Expected dict of input parameters") input_data['environment'] = config.DEPLOYMENT_PREFIX ecosystem = input_data.get('ecosystem') emr_instance = emr_instances.get(ecosystem) if emr_instance: emr_instance = emr_instance() status = emr_instance.run_job(input_data) else: raise HTTPError(400, "Ecosystem {} not supported yet.".format(ecosystem)) return jsonify(status), 200
def version_details(): """POST call to fetch model details.""" required_fields = ["bucket_name", "ecosystem"] input_data = request.get_json() missing_fields = check_field_exists(input_data, required_fields) if missing_fields: raise HTTPError(400, "These field(s) {} are missing from input " "data".format(missing_fields)) if not input_data: raise HTTPError(400, "Expected JSON request") if type(input_data) != dict: raise HTTPError(400, "Expected dict of input parameters") bucket = input_data['bucket_name'] ecosystem = input_data['ecosystem'] output = trained_model_details(bucket, ecosystem) return output
def parse_path(path): """ Given a path, get the query string params. :param path: :type path: bytes :rtype: bytes, """ try: parts = path.split(b'?') if len(parts) == 1: return parts[0], {} params = {param[0].decode('utf8'): param[1] for param in [kv.split(b'=', 1) for kv in parts[1].split(b'&')]} return parts[0], params except (AttributeError, IndexError): raise HTTPError(HTTPStatus.BAD_REQUEST) except Exception: raise HTTPError(HTTPStatus.INTERNAL_SERVER_ERROR)
def parse_request(data): """ Given the data received, parse it :param data: byte string of request :type data: bytes :rtype: bytes, """ try: request_lines = data.splitlines() method, path, protocol = request_lines.pop(0).split(b' ') header_lines = request_lines[:request_lines.index(b'')] headers = {header[0].decode('utf8').lower(): header[1].lstrip() for header in [header.split(b':', 1) for header in header_lines]} body = b'\n'.join(request_lines[request_lines.index(b'') + 1:]) return method, path, headers, body except (AttributeError, IndexError): raise HTTPError(HTTPStatus.BAD_REQUEST) except Exception: raise HTTPError(HTTPStatus.INTERNAL_SERVER_ERROR)
def wrapper(*args, **kwargs): # Disable authentication for local setup if getenv('DISABLE_AUTHENTICATION') in ('1', 'True', 'true'): return view(*args, **kwargs) lgr = current_app.logger user = None try: decoded = decode_token() if not decoded: lgr.exception( 'Provide an Authorization token with the API request') raise HTTPError(401, 'Authentication failed - token missing') lgr.info('Successfuly authenticated user {e} using JWT'.format( e=decoded.get('email'))) except jwt.ExpiredSignatureError as exc: lgr.exception('Expired JWT token') decoded = {'email': '*****@*****.**'} raise HTTPError( 401, 'Authentication failed - token has expired') from exc except Exception as exc: lgr.exception('Failed decoding JWT token') decoded = {'email': '*****@*****.**'} raise HTTPError( 401, 'Authentication failed - could not decode JWT token') from exc else: user = APIUser(decoded.get('email', '*****@*****.**')) if user: g.current_user = user else: g.current_user = APIUser('*****@*****.**') raise HTTPError(401, 'Authentication required') return view(*args, **kwargs)
def render_body_in_acceptable_mime(render_data, template, headers): """ Given the render data, the template, and the request headers, render the content in the first acceptable format :param render_data: :param template: :param headers: :return: """ for content_type in determine_content_types(headers): try: body = render(render_data, template, content_type) return body, content_type except (KeyError, IOError): pass raise HTTPError(HTTPStatus.NOT_ACCEPTABLE)
def test_http_error_attributes(): """Test the basic behaviour of HTTPError class.""" e = HTTPError(404, "Not found") assert e.status_code == 404 assert e.error == "Not found"
def test_http_error_exception_handling(): """Test the basic behaviour of HTTPError class.""" try: raise HTTPError(404, "Not found") except HTTPError as e: print(e)
def test_http_error_raise(): """Test the basic behaviour of HTTPError class.""" with pytest.raises(HTTPError) as e: raise HTTPError(404, "Not found")