def test_call_endpoints(): with mock.patch( "mlflow.utils.rest_utils.call_endpoint") as mock_call_endpoint: response_proto = GetRun.Response() mock_call_endpoint.side_effect = [ RestException({"error_code": ErrorCode.Name(ENDPOINT_NOT_FOUND)}), None, ] host_only = MlflowHostCreds("http://my-host") endpoints = [("/my/endpoint", "POST"), ("/my/endpoint", "GET")] resp = call_endpoints(host_only, endpoints, "", response_proto) mock_call_endpoint.assert_has_calls([ mock.call(host_only, endpoint, method, "", response_proto) for endpoint, method in endpoints ]) assert resp is None
def _call_endpoint(self, api, json_body): endpoint, method = _METHOD_TO_INFO[api] response_proto = api.Response() # Convert json string to json dictionary, to pass to requests if json_body: json_body = json.loads(json_body) host_creds = self.get_host_creds() response = http_request(host_creds=host_creds, endpoint=endpoint, method=method, json=json_body) js_dict = json.loads(response.text) if 'error_code' in js_dict: raise RestException(js_dict) parse_dict(js_dict=js_dict, message=response_proto) return response_proto
def verify_rest_response(response, endpoint): """Verify the return code and format, raise exception if the request was not successful.""" if response.status_code != 200: if _can_parse_as_json(response.text): raise RestException(json.loads(response.text)) else: base_msg = "API request to endpoint %s failed with error code " "%s != 200" % ( endpoint, response.status_code, ) raise MlflowException("%s. Response body: '%s'" % (base_msg, response.text)) if not _can_parse_as_json(response.text): base_msg = ( "API request to endpoint was successful but the response body was not " "in a valid JSON format") raise MlflowException("%s. Response body: '%s'" % (base_msg, response.text)) return response
def verify_rest_response(response, endpoint): """Verify the return code and format, raise exception if the request was not successful.""" if response.status_code != 200: if _can_parse_as_json_object(response.text): raise RestException(json.loads(response.text)) else: base_msg = "API request to endpoint %s failed with error code " "%s != 200" % ( endpoint, response.status_code, ) raise MlflowException("%s. Response body: '%s'" % (base_msg, response.text)) # Skip validation for endpoints (e.g. DBFS file-download API) which may return a non-JSON # response if endpoint.startswith(_REST_API_PATH_PREFIX) and not _can_parse_as_json_object(response.text): base_msg = ( "API request to endpoint was successful but the response body was not " "in a valid JSON format" ) raise MlflowException("%s. Response body: '%s'" % (base_msg, response.text)) return response
def test_rest_exception(): mlflow_exception = MlflowException("test", error_code=RESOURCE_ALREADY_EXISTS) json_exception = mlflow_exception.serialize_as_json() deserialized_rest_exception = RestException(json.loads(json_exception)) assert deserialized_rest_exception.error_code == "RESOURCE_ALREADY_EXISTS" assert "test" in deserialized_rest_exception.message
def test_rest_exception_without_message(): exc = RestException({"my_property": "something important."}) assert "something important." in str(exc) json.loads(exc.serialize_as_json())
def test_rest_exception_error_code_is_not_none(): error_string = "something important." exc = RestException({"message": error_string}) assert "None" not in error_string assert "None" not in str(exc) json.loads(exc.serialize_as_json())
def test_rest_exception_default_error_code(): exc = RestException({"message": "something important."}) assert "something important." in str(exc)
def test_rest_exception_error_code_and_no_message(): exc = RestException({"error_code": 2, "messages": "something important."}) assert "something important." in str(exc) assert "2" in str(exc) json.loads(exc.serialize_as_json())