def get(self, obj_url, interface=INTERFACES.JSON, headers=None, **query_params): """Gets the object in the format specified by 'interface'. Args: obj_url (str): The relative URL to the object. interface (:obj:`INTERFACES`, optional): The interface format to request for the object. headers (:obj:`dict`, optional): Any headers to be sent to the request. **query_params (dict): A dictionary of query parameters that may be used in object retrieval. Returns: dict: If the interface is a JSON interface, the response is encoded as a JSON dictionary object. str: If the interface is a text interface, the response is returned as text. bytes: If the interface is a binary interface, the response is returned as bytes. """ if interface is not None: obj_url = '{}/{}'.format(obj_url, interface) response = self.api_client.get( obj_url, headers=headers, **query_params) raise_http_exception(response) return format_object(response, interface)
def delete(self, obj_url, headers=None, **query_params): """Sends a delete request to the specified URL. Args: obj_url (str): A relative URL for the object in question. headers (:obj:`dict`, optional): Any headers to be sent to the request. **query_params (dict): A dictionary of query parameters to be sent along with the request. """ response = self.api_client.delete(obj_url, headers=headers, **query_params) raise_http_exception(response)
def list(self, list_url, headers=None, **query_params): """Lists the number of objects matching the provided parameters. Args: list_url (str): A relative URL for listing objects. headers (:obj:`dict`, optional): Any headers to be sent to the request. **query_params (dict): A dictionary of query parameters to be used as criteria for listing. Returns: list of dict: The list of matching objects is returned as a list of dictionaries. """ response = self.api_client.get(list_url, headers=headers, **query_params) raise_http_exception(response) return format_object(response, INTERFACES.JSON)
def count(self, count_url, headers=None, **query_params): """Counts the number of objects matching the provided parameters. Args: count_url (str): A relative URL for counting objects. headers (:obj:`dict`, optional): Any headers to be sent to the request. **query_params (dict): A dictionary of query parameters to be used as criteria for counting. Returns: int: The number of objects matching the specified criteria """ response = self.api_client.get( count_url, headers=headers, **query_params) raise_http_exception(response) return format_object(response, INTERFACES.JSON)['count']
def post(self, post_url, files=None, headers=None, **query_params): """Submits a POST request to the server. Args: post_url (str): A relative URL where the POST request will be made. files (:obj:`dict`, optional): A dictionary with file query parameter name keys and tuple values with (file name, open file-like object or string, and optionally a mime-type for the file). headers (:obj:`dict`, optional): Any headers to be sent to the request. **query_params (dict): A dictionary of query parameters to be sent with the POST request. Returns: dict: A JSON dictionary returned from the POST request """ response = self.api_client.post( post_url, files=files, headers=headers, **query_params ) raise_http_exception(response) return format_object(response, INTERFACES.JSON)
def test_no_error(self): """Test that a successful response passes through.""" resp = DummyResponse(HttpStatus.OK, 'http://someurl.com') assert client_exceptions.raise_http_exception(resp) is None
def test_service_unavailable_error(self): """Test that a service unavailable error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.SERVICE_UNAVAILABLE, 'http://someurl.com') with pytest.raises(client_exceptions.ServiceUnavailableError): client_exceptions.raise_http_exception(resp)
def test_uncaught_server_error(self): """Test that an uncaught server error still raises an exception.""" resp = DummyResponse(599, 'http://someurl.com') with pytest.raises(Exception): client_exceptions.raise_http_exception(resp)
def test_not_acceptable_error(self): """Test that a not acceptable error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.NOT_ACCEPTABLE, 'http://someurl.com') with pytest.raises(client_exceptions.NotAcceptableError): client_exceptions.raise_http_exception(resp)
def test_not_found_error(self): """Test that a not found error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.NOT_FOUND, 'http://someurl.com') with pytest.raises(client_exceptions.NotFoundError): client_exceptions.raise_http_exception(resp)
def test_method_not_allowed_error(self): """Test that a method not allowed error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.METHOD_NOT_ALLOWED, 'http://someurl.com') with pytest.raises(client_exceptions.MethodNotAllowedError): client_exceptions.raise_http_exception(resp)
def test_internal_server_error(self): """Test that an internal server error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.INTERNAL_SERVER_ERROR, 'http://someurl.com') with pytest.raises(client_exceptions.InternalServerError): client_exceptions.raise_http_exception(resp)
def test_forbidden_error(self): """Test that a forbidden error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.FORBIDDEN, 'http://someurl.com') with pytest.raises(client_exceptions.ForbiddenError): client_exceptions.raise_http_exception(resp)
def test_conflict_error(self): """Test that a conflict error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.CONFLICT, 'http://someurl.com') with pytest.raises(client_exceptions.ConflictError): client_exceptions.raise_http_exception(resp)
def test_bad_request_error(self): """Test that a bad request error is thrown when appropriate.""" resp = DummyResponse(HttpStatus.BAD_REQUEST, 'http://someurl.com') with pytest.raises(client_exceptions.BadRequestError): client_exceptions.raise_http_exception(resp)