Skip to main content

A Python package to manage API rate limits by automatically queuing and pacing API requests.

Project description

apirl

apirl is a Python package designed to manage API rate limits by automatically queuing and pacing API requests. It supports various APIs and provides customizable rate limiting strategies to help avoid hitting API rate limits.

Installation

You can install apirl using pip:

pip install apirl

Usage

Here is an example of how to use 'apirl' to manage API rate limits:

1. Set Up Environment Variables

Create a .env file in the root directory of your project and add your API keys and any other necessary environment variables:

API_KEY=your_api_key_here

2. Example Usage

import os
from dotenv import load_dotenv
from apirl.core import RateLimiter
from apirl.queue import RateLimitedQueue
import http.client
import ssl
import certifi

# Load environment variables
load_dotenv()

# Initialize RateLimiter and RateLimitedQueue
rate_limiter = RateLimiter(rate_limit=5, per_seconds=10)
rate_limited_queue = RateLimitedQueue(rate_limiter, "https://your-api-base-url.com")

def make_api_call(method, endpoint, headers, body=None):
    try:
        context = ssl.create_default_context(cafile=certifi.where())
        conn = http.client.HTTPSConnection("your-api-host.com", context=context, timeout=10)
        conn.request(method, endpoint, body=body, headers=headers)
        res = conn.getresponse()
        status = res.status
        data = res.read()
        if status == 200:
            print(f"Response for {endpoint}: {data.decode('utf-8')}")
        else:
            print(f"Error {status} for {endpoint}: {data.decode('utf-8')}")
    except (http.client.HTTPException, ssl.SSLError) as e:
        print(f"Network error for {endpoint}: {e}")

if __name__ == "__main__":
    headers = {
        'Authorization': f'Bearer {os.getenv("API_KEY")}',
        'Content-Type': 'application/json'
    }
    endpoints = [
        ('GET', '/endpoint', headers)
    ]
    for method, endpoint, headers in endpoints:
        rate_limited_queue.put(make_api_call, method, endpoint, headers)
    rate_limited_queue.queue.join()

Features

  • Rate Limiter: Manages and enforces API rate limits with configurable limits and intervals.
  • Rate-Limited Queue: Queues API requests and ensures they are sent within rate limit constraints.
  • Customizable Headers: Supports custom headers for API requests.
  • SSL Certificate Handling: Uses certifi to manage SSL certificates.

Contributing

Contributions are welcome! Feel free to submit bug reports, feature requests, or pull requests through GitHub issues and pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

apirl-0.1.0.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

apirl-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file apirl-0.1.0.tar.gz.

File metadata

  • Download URL: apirl-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for apirl-0.1.0.tar.gz
Algorithm Hash digest
SHA256 98f84763d956be3a458563d49c36f1851fc956efe6f20edff24a1677a323cffa
MD5 489edb9805831ec310ae5c54d4448a1b
BLAKE2b-256 f6c85e782f61956dcfd8c5d9764fe40ad0166eca9a690e1a87f14a5d04bc06dd

See more details on using hashes here.

File details

Details for the file apirl-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: apirl-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for apirl-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1345776f64444ab848f4bdb1a1c4514ef4e9ae2d8dcdfc6008867adf9f269a75
MD5 5cc75947147bc9c731a7fcdfad58da47
BLAKE2b-256 96dbb0b9a0a8c934cf1102c9039ff3489eddc2c744a69b5525e08e5545b09796

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page