import adulib.rest
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"
},={"Content-Type": "application/json"}
headers )
{'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"
},={"Content-Type": "application/json"}
headers )
{'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",
={"Content-Type": "application/json"}
headers )
{'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.
"https://httpbin.org/get",
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
self) remaining_call_quota(
reset_quota
self) reset_quota(
__get_defaults
self, method, endpoint, params, headers) __get_defaults(
__load_cache_or_make_call (async)
self, func, args, only_use_cache, cache_key) __load_cache_or_make_call(
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)
self, endpoint, params, headers, only_use_cache, **param_kwargs) get(
put (async)
self, endpoint, data, only_use_cache, headers) put(
post (async)
self, endpoint, data, only_use_cache, headers) post(
delete (async)
self, endpoint, only_use_cache, headers) delete(
check_cache
self, method, endpoint, params, headers, **param_kwargs) check_cache(
clear_cache_key
self, method, endpoint, params, headers, **param_kwargs) clear_cache_key(
= AsyncAPIHandler(
api_handler ="https://httpbin.org/",
base_url={"api_key": "your_api_key"},
default_params={"User-Agent": "MyTestClient/1.0"},
default_headers=10
rate_limit
)
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'}
"get", "get") api_handler.check_cache(
True
"get", "get")
api_handler.clear_cache_key("get", "get") api_handler.check_cache(
False