demands documentation

Base HTTP service client

class demands.HTTPServiceClient(url, **kwargs)

Bases: requests.sessions.Session

Extendable base service client.

Client can be configured with any param allowed by the requests API. These params will be uses with each and every request and can be overridden with kwargs. demands adds the following params:

Parameters:
  • expected_response_codes – (optional) Workaround for services which returns non-expected results, example: when search for users, and expect [] for when nobody is found, yet a 404 is returned.
  • client_name – (optional) Sets the User-Agent header. Important because we want to accurately log errors and throw deprecation warnings when clients are outdated
  • client_version – (optional) Used with client_name
  • app_name – (optional) Used with client_name
  • cookies – (optional) Dict only, CookieJar not supported
is_acceptable(response, request_params)

Override this method to create a different definition of what kind of response is acceptable. If bool(the_return_value) is False then an HTTPServiceError will be raised.

For example, you might want to assert that the body must be empty, so you could return len(response.content) == 0.

In the default implementation, a response is acceptable if and only if the response code is either less than 300 (typically 200, i.e. OK) or if it is in the expected_response_codes parameter in the constructor.

post_send(response, **kwargs)

Override this method to modify returned response

pre_send(request_params)

Override this method to modify sent request parameters

request(method, path, **kwargs)

Send a requests.Request and demand a requests.Response

exception demands.HTTPServiceError(response)

Bases: exceptions.AssertionError