Skip to main content

poet-api

Requests with Limiter.

Uses pyrate_limiter.SQLiteBucket with use_file_lock=True.

It has a default RATE_LIMIT_SITES setting and waits for settings.DJANGO_RATE_LIMIT_SITES:

Configuration:

DJANGO_RATE_LIMIT_SITES = {
    "payment_gateway": {
        "domain_keyword": "adomain.com",
        "rates": [Rate(25, Duration.MINUTE)],
        "max_wait_seconds": 5,  # Custom timeout in seconds
    },
    "analytics_service": {
        "domain_keyword": "anotherdomain.com",
        "rates": [Rate(50, Duration.MINUTE)],
        # No timeout here: default = -1
    },
    # The following line, already exists as default setting:
    # "default": {"rates": [Rate(59, Duration.MINUTE)], "max_wait_seconds": -1},
}
  • DJANGO_RATE_LIMIT_SITES.keys()[0]: str -> Unique identity keyword
  • DJANGO_RATE_LIMIT_SITES.values()[0]: dict ->
    • domain_keyword: str -> urlparse(url).netlock or "default". A new autonomous limiter is based on this keyword.
    • rates: list -> List of Rate objects for the domain_keyword
    • max_wait_seconds: int -> Max seconds the limiter can wait before failing.
      • -1: Wait until the bucket allows a request, the request never fails.
      • positive int: If the bucket needs more than positive int seconds to allow a request, the request fails.
        • A mocked HTTP 429 ("Too Many Requests") response object is returned without making an actual request.
        • requests().raise_for_status() raises requests.exceptions.HTTPError.

Example

import requests

from api.rate_limiter import send_request

try:
    # 1. Send request
    response = send_request("https://example.com", method="POST", json={"amount": 10})
    
    # 2. Raise for error
    # If:
    #   - max_wait_seconds != -1 (-1 is the default) 
    #   - the bucket cannot afford it
    # -> a mocked HTTP 429 ("Too Many Requests") response is returned

    response.raise_for_status()
    
    # 3. Continue if everything is OK
    data = response.json()
    print("Success:", data)

except requests.exceptions.HTTPError as e:
    print(f"HTTP Error occurred: {e}")
    if e.response and e.response.status_code == 429:
        print("Reason: The application queue was too full and exceeded max_wait_seconds!")
        
except Exception as e:
    print(f"An unexpected error occurred: {e}")
  ```

Download files

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

Source Distribution

poet_api-4.0.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

poet_api-4.0.1-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file poet_api-4.0.1.tar.gz.

File metadata

  • Download URL: poet_api-4.0.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.13 Linux/6.12.96+deb13-amd64

File hashes

Hashes for poet_api-4.0.1.tar.gz
Algorithm Hash digest
SHA256 085d8a14c67b8b5f4e723329e602cc4d8e93887f4d46919d1ef23305e8869a08
MD5 9196a627e2027e86e15c6142eada3907
BLAKE2b-256 ce140bb6386c0abf4baade2edec4412552f96663494479b1ae5161516b1e25a4

See more details on using hashes here.

File details

Details for the file poet_api-4.0.1-py3-none-any.whl.

File metadata

  • Download URL: poet_api-4.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.4.1 CPython/3.13.13 Linux/6.12.96+deb13-amd64

File hashes

Hashes for poet_api-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dccae3ce48433bcdc7ed57144f8fc685eb374ff55dff17adf916a707c3b8f7a9
MD5 22bcdebf3cd76bdb036439746a0d3d9c
BLAKE2b-256 6f95636109758f4ffe799e62a8c27b79a6047a9d53eff6395d482ef0b7de1b2f

See more details on using hashes here.

Release history Release notifications | RSS feed

4.2.2

2 files

4.2.1

2 files

4.2.0

2 files

4.1.2

2 files

4.1.1

2 files

4.1.0

2 files

This release

4.0.1 This release

2 files

3.0.4

2 files

3.0.3

2 files

3.0.2

2 files

3.0.0

2 files

2.0.3

2 files

2.0.1

2 files

1.2.18

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page