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 ofPluginSDKRuntimeError
.
-
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. Ifself.base_url
is not specified, then the user must provide the full URL when callingextract_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 invokedendpoint_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
-
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 invokedendpoint_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
-