rest

import adulib.rest

Async REST functions

async_get (async)

async_get(endpoint, params, headers)

Fetch data from a given RESTful API endpoint using an HTTP GET request.

Arguments: - endpoint: The API endpoint URL (string). - params: A dictionary of query parameters (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


async_put (async)

async_put(endpoint, data, headers)

Update data at a given RESTful API endpoint using an HTTP PUT request.

Arguments: - endpoint: The API endpoint URL (string). - data: A dictionary of data to send in the body of the request (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


async_post (async)

async_post(endpoint, data, headers)

Send data to a given RESTful API endpoint using an HTTP POST request.

Arguments: - endpoint: The API endpoint URL (string). - data: A dictionary of data to send in the body of the request (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


async_delete (async)

async_delete(endpoint, headers)

Delete a resource at a given RESTful API endpoint using an HTTP DELETE request.

Arguments: - endpoint: The API endpoint URL (string). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


await async_get("https://httpbin.org/get",
    params={
        "query": "test",
        "page": 2
    },
    headers={
        "User-Agent": "MyTestClient/1.0",
        "Authorization": "Bearer testtoken123"
    }
)
{'args': {'page': '2', 'query': 'test'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Authorization': 'Bearer testtoken123',
  'Host': 'httpbin.org',
  'User-Agent': 'MyTestClient/1.0',
  'X-Amzn-Trace-Id': 'Root=1-68529df5-3b1e0cb152cfb3a84f74a5b4'},
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/get?query=test&page=2'}
await async_put("https://httpbin.org/put",
    data={
        "key1": "value1",
        "key2": "value2"
    },
    headers={"Content-Type": "application/json"}
)
{'args': {},
 'data': '{"key1": "value1", "key2": "value2"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Content-Length': '36',
  'Content-Type': 'application/json',
  'Host': 'httpbin.org',
  'User-Agent': 'Python/3.11 aiohttp/3.12.0',
  'X-Amzn-Trace-Id': 'Root=1-68529df6-0064f1e105c1486132aa75b7'},
 'json': {'key1': 'value1', 'key2': 'value2'},
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/put'}
await async_post("https://httpbin.org/post",
    data={
        "key1": "value1",
        "key2": "value2"
    },
    headers={"Content-Type": "application/json"}
)
{'args': {},
 'data': '{"key1": "value1", "key2": "value2"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Content-Length': '36',
  'Content-Type': 'application/json',
  'Host': 'httpbin.org',
  'User-Agent': 'Python/3.11 aiohttp/3.12.0',
  'X-Amzn-Trace-Id': 'Root=1-68529df7-440e45b90c825d0c2e14859c'},
 'json': {'key1': 'value1', 'key2': 'value2'},
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/post'}
await async_delete("https://httpbin.org/delete",
    headers={"Content-Type": "application/json"}
)
{'args': {},
 'data': '',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Content-Length': '0',
  'Content-Type': 'application/json',
  'Host': 'httpbin.org',
  'User-Agent': 'Python/3.11 aiohttp/3.12.0',
  'X-Amzn-Trace-Id': 'Root=1-68529df8-73b0bb6f71de0a6f04377ab6'},
 'json': None,
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/delete'}

Sync REST functions

get

get(endpoint, params, headers)

Fetch data from a given RESTful API endpoint using an HTTP GET request.

Arguments: - endpoint: The API endpoint URL (string). - params: A dictionary of query parameters (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


post

post(endpoint, data, headers)

Send data to a given RESTful API endpoint using an HTTP POST request.

Arguments: - endpoint: The API endpoint URL (string). - data: A dictionary of data to send in the body of the request (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


put

put(endpoint, data, headers)

Update data at a given RESTful API endpoint using an HTTP PUT request.

Arguments: - endpoint: The API endpoint URL (string). - data: A dictionary of data to send in the body of the request (default is None). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


delete

delete(endpoint, headers)

Delete a resource at a given RESTful API endpoint using an HTTP DELETE request.

Arguments: - endpoint: The API endpoint URL (string). - headers: A dictionary of HTTP headers (default is None).

Returns:: The JSON response as a dictionary, or an error message.


get("https://httpbin.org/get",
    params={
        "query": "test",
        "page": 2
    },
    headers={
        "User-Agent": "MyTestClient/1.0",
        "Authorization": "Bearer testtoken123"
    }
)
{'args': {'page': '2', 'query': 'test'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Authorization': 'Bearer testtoken123',
  'Host': 'httpbin.org',
  'User-Agent': 'MyTestClient/1.0',
  'X-Amzn-Trace-Id': 'Root=1-68529df8-66bdbd7e3424e54a41dfc002'},
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/get?query=test&page=2'}

AsyncAPIHandler

AsyncAPIHandler


Methods

init

__init__(
   self,
   base_url,
   default_params,
   default_headers,
   rate_limit,
   use_cache,
   cache_dir,
   call_quota
)

A handler for making asynchronous API calls with support for caching, rate limiting, and default parameters.

Arguments: - base_url: The base URL of the API. This will be prepended to all endpoint calls. - default_params: A dictionary of default query parameters to be included in every request. - default_headers: A dictionary of default headers to be included in every request. - rate_limit: The rate limit for API calls, specified as the number of calls per second. - use_cache: A boolean indicating whether to enable caching of API responses. - cache_dir: The directory where cached responses will be stored. If None, a temporary directory will be created. - call_quota: An optional limit on the number of API calls that can be made. If None, there is no limit. This class provides methods for making GET, POST, PUT, and DELETE requests asynchronously, while managing caching and rate limiting. It also allows checking and clearing the cache for specific API calls.


remaining_call_quota

remaining_call_quota(self)

reset_quota

reset_quota(self)

__get_defaults

__get_defaults(self, method, endpoint, params, headers)

__load_cache_or_make_call (async)

__load_cache_or_make_call(self, func, args, only_use_cache, cache_key)

call (async)

call(
   self,
   method,
   endpoint,
   params,
   data,
   headers,
   only_use_cache,
   **param_kwargs
)

Make a request to the API.

Arguments: - method: The HTTP method to use (e.g., “get”, “put”, “post”, “delete”). - endpoint: The API endpoint to request. - params: A dictionary of query parameters for the request.


get (async)

get(self, endpoint, params, headers, only_use_cache, **param_kwargs)

put (async)

put(self, endpoint, data, only_use_cache, headers)

post (async)

post(self, endpoint, data, only_use_cache, headers)

delete (async)

delete(self, endpoint, only_use_cache, headers)

check_cache

check_cache(self, method, endpoint, params, headers, **param_kwargs)

clear_cache_key

clear_cache_key(self, method, endpoint, params, headers, **param_kwargs)

api_handler = AsyncAPIHandler(
    base_url="https://httpbin.org/",
    default_params={"api_key": "your_api_key"},
    default_headers={"User-Agent": "MyTestClient/1.0"},
    rate_limit=10
)

await api_handler.get("get")
{'args': {'api_key': 'your_api_key'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Host': 'httpbin.org',
  'User-Agent': 'MyTestClient/1.0',
  'X-Amzn-Trace-Id': 'Root=1-68529dfa-0c9af2a13a5ffb97116ff534'},
 'origin': '171.22.106.220',
 'url': 'https://httpbin.org/get?api_key=your_api_key'}
api_handler.check_cache("get", "get")
True
api_handler.clear_cache_key("get", "get")
api_handler.check_cache("get", "get")
False