Skip to content

Latest commit

Β 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Β 
Β 
Β 
Β 

README.md

🌐 Python API Integration Template

Python Requests

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.


πŸš€ Features

  • βœ… 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

πŸ“¦ Requirements

  • Python 3.x
  • requests library

Install the requests library if needed:

pip install requests

βš™ Setup

  1. 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"
  1. Run the Script

The script includes example usage:

python API integration.py

πŸ”§ Functions

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

πŸ“‹ Example Workflow

# 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)

πŸ“ Logging & Error Handling

  • 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.

πŸ’‘ Extending the Script

  • 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 retries or time.sleep delay as needed for your API rate limits.