API Tools

class oscopilot.tool_repository.manager.tool_request_util.ToolRequestUtil[source]

Bases: object

A utility class for making HTTP requests to an API.

This class simplifies the process of sending HTTP requests using a persistent session and predefined headers, including a User-Agent header to mimic a browser request. It’s designed to interact with APIs by sending GET or POST requests and handling file uploads.

session

A requests session for making HTTP requests.

Type:

requests.Session

headers

Default headers to be sent with each request.

Type:

dict

base_url

The base URL for the API endpoints.

Type:

str

request(api_path, method, params=None, files=None, content_type='application/json')[source]

Sends a request to the specified API endpoint using the defined HTTP method.

This method constructs the request URL from the base URL and the API path. It supports both GET and POST methods, including handling of JSON parameters, file uploads, and different content types.

Parameters:
  • api_path (str) – The path of the API endpoint.

  • method (str) – The HTTP method to use for the request (‘get’ or ‘post’).

  • params (dict, optional) – The parameters to include in the request. Defaults to None.

  • files (dict, optional) – Files to be uploaded in a POST request. Defaults to None.

  • content_type (str, optional) – The content type of the request, such as ‘application/json’ or ‘multipart/form-data’. Defaults to “application/json”.

Returns:

The JSON response from the API, or None if an error occurs.

Return type:

dict

Raises:

Prints an error message to the console if an HTTP request error occurs.