A robust and reusable Python template for integrating with RESTful APIs.
Supports all major HTTP methods, handles authentication, retries failed requests, and logs all activity for easy debugging.
- β Supports GET, POST, PUT, DELETE requests
- β Automatic retry mechanism for failed requests
- β Structured logging with timestamps and error handling
- β Bearer Token Authentication
- β Easy-to-use functions for *common API operations *
- β Fully extensible for any REST API
Python 3.xrequestslibrary
Install the requests library if needed:
pip install requests- Configure API Details
Edit the script to set your API base URL and API key:
API_BASE_URL = "https://api.example.com/v1"
API_KEY = "your_api_key_here"- Run the Script
The script includes example usage:
python API integration.py
| Function | Method | Description |
|---|---|---|
send_request |
GET/POST/PUT/DELETE | Core function with retries & error handling |
get_resource(resource_id) |
GET | Fetch a specific resource |
list_resources(query_params=None) |
GET | Fetch a list of resources |
create_resource(data) |
POST | Create a new resource |
update_resource(resource_id, data) |
PUT | Update an existing resource |
delete_resource(resource_id) |
DELETE | Delete a resource by ID |
# List all resources
resources = list_resources()
print("Resources:", resources)
# Create a new resource
new_data = {"name": "Test Item", "value": 100}
new_resource = create_resource(new_data)
print("Created:", new_resource)
# Update the resource
if new_resource:
updated_data = {"name": "Updated Item", "value": 150}
updated = update_resource(new_resource.get("id"), updated_data)
print("Updated:", updated)
# Delete the resource
if new_resource:
deleted = delete_resource(new_resource.get("id"))
print("Deleted:", deleted)- Logs every request with method, URL, and attempt number.
- Automatically retries failed requests up to 3 times with a 2-second delay.
- Logs success, warnings, and errors to the console.
- Change endpoint paths to match your API structure.
- Add additional helper functions for other endpoints.
- Integrate with CLI or web frameworks for automation.
π Notes
- Ensure your API key and base URL are correct.
- Adjust
retriesortime.sleepdelay as needed for your API rate limits.