RaspberryRequest is a simple, easy-to-use py package for making HTTP requests and managing responses. RaspberryRequest is designed for easy building around getting and posting data to APIs, utilising the well-known requests module.
- Custom Handling of responses based on response status codes.
- Exponential backoff implementation, with custom number of retries and maximum delay time.
- Call counter.
Installing raspberryrequest can be done simply and easily via pip:
pip install raspberryrequest # And you're good to go!The APIRequestHandler class is a utility for sending API requests with retry logic. It provides a convenient way to handle common API request scenarios, such as setting headers, handling retries, and implementing backoff logic.
- Import the
APIRequestHandlerclass:
from api_request_handler import APIRequestHandler
- Create an instance of the
APIRequestHandlerclass:
handler = APIRequestHandler(headers={'some key', 'some val'}, max_attempts=3, max_delay=10)
- To send an API request, use the
send_api_requestmethod:
response = api_handler.send_api_request(
base_url='https://api.example.com',
method='GET',
params={'param1': 'value1'},
headers={'Content-Type': 'application/json'}
)
The send_api_request method returns the response JSON data as a dictionary if the request is successful. If a fatal status code is received, a FatalStatusCodeError will be raised.
If you have anything you think will be of a great addition to RaspberryRequest, or have a bug you would like to report, please follow the steps in CONTRIBUTIONS to see how you can do so!