Skip to main content

A simple HTTP requester with retry and proxy support

Project description

resilient-requester

A robust Python library for making resilient HTTP requests with retries, proxies, and custom headers.

resilient_requester is a lightweight Python package designed to simplify and improve HTTP GET requests by incorporating features such as retries, proxies, customizable headers, and timeout management.

It is built to handle network errors gracefully and provide informative logging for debugging and monitoring.

Features

  • Resilient HTTP Requests: Automatically retries failed requests with configurable retry limits and randomized wait times.

  • Proxy Support: Easily integrate proxies for requests.

  • Customizable Headers: Generate random user-agent headers or when not provided by user.

  • Timeout Management: Enforce minimum timeout values to prevent overly aggressive requests.

  • Informative Logging: Detailed logging for request attempts, failures, and retries.

  • Type Checking: Ensures correct parameter types to prevent runtime errors.

  • Lightweight and Simple: Minimal dependencies and easy-to-use interface.

Installation

You can install resilient_requester using pip: pip install resilient_requester

Quick Start

Here’s a simple example to get started with resilient_requester:

Usage

Basic GET Request

Make a simple GET request with default settings.

ffrom resilient_requester import get_request

response = get_request("https://example.com")
if response:
    print(f"Status Code: {response.status_code}")
    print(response.text)
else:
    print("Request failed after retries.")

Using Proxies

You can specify a proxy dictionary to route requests through a proxy server.

from resilient_requester import get_request

proxy = {
    "http": "http://proxy.example.com:8080",
    "https": "https://proxy.example.com:8080"
}

response = get_request(
    url="https://example.com",
    proxy=proxy
)
if response:
    print("Request successful through proxy!")

GET Request with Custom Headers and Retries

Send a request with custom headers, a specific timeout, and multiple retries.

from resilient_requester import get_request

custom_headers = {
    "User-Agent": "MyCustomAgent/1.0",
    "Accept": "application/json"
}

response = get_request(
    url="https://api.example.com/data",
    headers=custom_headers,
    timeout=10,
    retries=3
)
if response:
    print(f"Request successful: {response.status_code}")
    print(response.json())
else:
    print("Request failed after retries.")

Timeouts and Retries

You can configure the timeout and number of retries for requests. The library enforces a minimum timeout of 5 seconds and at least 1 retry.

from resilient_requester import get_request

# Make a request with custom timeout and retries
response = get_request(
    url="https://example.com",
    timeout=10,
    retries=3
)

if response:
    print("Request successful!")
else:
    print("Request failed after 3 retries.")

API Reference

Attributes:

  • headers: A dictionary containing default HTTP headers, including a random user-agent.

get_request(url, proxy=None, headers=None, timeout=0, retries=0)

Makes an HTTP GET request to the specified URL with retry logic.

Parameters:

  • url (str): The URL to request.
  • proxy (dict, optional): A dictionary specifying proxy settings (e.g., {"http": "http://proxy:8080"}).
  • headers (dict, optional): A dictionary of HTTP headers. If not provided, defaults to headers with a random user-agent.
  • timeout (int, optional): The timeout for the request in seconds. Defaults to 5 if not provided or less than 5.
  • retries (int, optional): The number of retries for failed requests. Defaults to 1 if not provided or less than 1.

Returns:

  • requests.Response: The response object if the request is successful.
  • None: If the request fails after all retries.

Example:

response = get_request(
    url="https://example.com",
    proxy={"http": "http://proxy:8080"},
    headers={"User-Agent": "CustomAgent"},
    timeout=10,
    retries=3
)

Helper Functions

  • helpers.helper_functions module contains utility functions used by the Requester class. get_headers()

    Generates default HTTP headers with a random user-agent.

    Returns:

    dict: A dictionary of HTTP headers with the following keys:

    • accept: "*/*"
      • accept-language: "en-GB,en-US;q=0.9,en;q=0.8"
      • connection: "keep-alive"
      • user-agent: A random user-agent string generated by fake_useragent.
  • check_params(proxy, headers, timeout, retries): Validates and normalizes input parameters.

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

resilient_requester-0.1.3.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.

resilient_requester-0.1.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file resilient_requester-0.1.3.tar.gz.

File metadata

  • Download URL: resilient_requester-0.1.3.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.12

File hashes

Hashes for resilient_requester-0.1.3.tar.gz
Algorithm Hash digest
SHA256 6c28284b0834ffd6a21d61972db016b9ceb240258bd0b756e68583571c863476
MD5 5060f30d513225f35fe9ac0c59919bc1
BLAKE2b-256 4ee9ad8c8f393fc5a50ea1e173589b204fd2122bfc34e7d91191d8ace3d2292c

See more details on using hashes here.

File details

Details for the file resilient_requester-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for resilient_requester-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 847b5360e91c580ae81dce30c914024586df1de87dd902adb2c9427a9a1076b7
MD5 f0ba552dfc0f9cda29891dcf61a6e5c7
BLAKE2b-256 7453e462aa1927551b06fac7cf1af3fb96b8e7d2f19b514705c666c5d9296423

See more details on using hashes here.

Supported by

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