Table Of Contents

Previous topic

9.9. MemoryCache

Next topic

10. Testing

9.10. EndpointExtractor

exception safeguard.sessions.plugin.endpoint_extractor.EndpointException(message, variables=None)

The EndpointException exception is raised when an endpoint responds with non-OK status code or the response does not contain the data queried by the user. It is a subclass of PluginSDKRuntimeError.

class safeguard.sessions.plugin.endpoint_extractor.EndpointExtractor(base_url=None)

The EndpointExtractor class represents an utility class which sends GET and POST requests to an URL, then extracts data from the responses.

base_url

The self.base_url attribute contains the common prefix of the URLs to where the HTTP requests will be sent. If self.base_url is not specified, then the user must provide the full URL when calling extract_data_from_endpoint().

extract_data_from_endpoint(session, endpoint_url, data_path=None, method='get', headers=None, data=None, params=None)

The extract_data_from_endpoint() method uses invoke_http_method to invokes a HTTP method on the specified endpoint, then extracts the needed data from the endpoint’s response.

Parameters
  • session – the requests.Session or RequestsTLS object on which the HTTP method is invoked

  • endpoint_url (str) – the URL where the HTTP request will be sent. If self.base_url is defined, then it will be prepended to this URL.

  • data_path (str) – path to the data to be retrieved, delimited by dots (eg. “key.another_key.last_key”)

  • method (str) – HTTP method to invoke, one of GET or POST

  • headers (dict) – dictionary of HTTP Headers to send with the request

  • data (dict) – dictionary, list of tuples, bytes, or file-like object to send in the body of the request

  • params (dict) – dictionary, list of tuples or bytes to send in the query string for the request

Returns

the extracted data from the decoded response JSON

Raises

EndpointException

invoke_http_method(session, endpoint_url, method, headers=None, data=None, params=None)

The invoke_http_method() method invokes a HTTP method on the specified endpoint.

Parameters
  • session – the requests.Session or RequestsTLS object on which the HTTP method is invoked

  • endpoint_url (str) – the URL where the HTTP request will be sent. If self.base_url is defined, then it will be prepended to this URL.

  • method (str) – HTTP method to invoke, one of GET or POST

  • headers (dict) – dictionary of HTTP Headers to send with the request

  • data (dict) – dictionary, list of tuples, bytes, or file-like object to send in the body of the request

  • params (dict) – dictionary, list of tuples or bytes to send in the query string for the request

Returns

response JSON

Raises

EndpointException