policyweaver.core.api.rest

policyweaver.core.api.rest

class policyweaver.core.api.rest.RestAPIProxy(base_url, headers=None, weaver_type=None)

Bases: object

A class to interact with a REST API. This class provides methods to perform GET, POST, PUT, and DELETE requests. Attributes:

logger (logging.Logger): Logger instance for logging API interactions. base_url (str): The base URL of the REST API. headers (dict): Default headers to be used in API requests.

_handle_response(response)

Handles the response from the REST API. Args:

response (Response): The response object from the requests library.

Returns:

Response object: The response if the status code is 200, 201, or 202.

Raises:

HTTPError: If the response status code is not 200, 201, or 202.

delete(endpoint, headers=None)

Performs a DELETE request to the specified endpoint of the REST API. Args:

endpoint (str): The endpoint to which the DELETE request is made. headers (dict, optional): Headers to be included in the request. Defaults to None.

Returns:

Response object: The response from the DELETE request.

get(endpoint, params=None, headers=None)

Performs a GET request to the specified endpoint of the REST API. Args:

endpoint (str): The endpoint to which the GET request is made. params (dict, optional): Query parameters to be included in the request. Defaults to None. headers (dict, optional): Headers to be included in the request. Defaults to None.

Returns:

Response object: The response from the GET request.

post(endpoint, data=None, json=None, files=None, headers=None)

Performs a POST request to the specified endpoint of the REST API. Args:

endpoint (str): The endpoint to which the POST request is made. data (dict, optional): Form data to be included in the request. Defaults to None json (dict, optional): JSON data to be included in the request. Defaults to None. files (dict, optional): Files to be uploaded in the request. Defaults to None. headers (dict, optional): Headers to be included in the request. Defaults to None.

Returns:

Response object: The response from the POST request.

put(endpoint, data=None, json=None, headers=None)

Performs a PUT request to the specified endpoint of the REST API. Args:

endpoint (str): The endpoint to which the PUT request is made. data (dict, optional): Form data to be included in the request. Defaults to None json (dict, optional): JSON data to be included in the request. Defaults to None. headers (dict, optional): Headers to be included in the request. Defaults to None.

Returns:

Response object: The response from the PUT request.