Skip to main content

Utils for HTTP requests using the requests library

Project description

Http Utils

Code style: black GitHub

A simple requests session wrapper.

Installation

pip install pip install git+git://github.com/juntossomosmais/http-utils.git

Configuration process

If needed you can provide some settings as environment variables.

HTTP_RETRIES=3
HTTP_BACKOFF_FACTOR=0.1
HTTP_STATUS_FORCELIST=
HTTP_TIMEOUT=30

Usage

The basic usage is to call the request_session as a context manager.

from http_utils.session import request_session


def awesome_request():
    with request_session() as request:
        response = request.get(url="https://www.some-awesome-service.com")
    return response

This will add:

  • A retry mechanism of 3 attempts for status codes (defined in the HTTP_RETRIES variable or as the total parameter)
  • A backoff factor for each retry in seconds (defined in the HTTP_BACKOFF_FACTOR variable or as the backoff_factor parameter)
  • A list of status to force retries (defined in the HTTP_STATUS_FORCELIST variable or as the status_forcelist parameter)
  • A list of allowed methods to request (Defined as ["HEAD", "GET", "PUT", "DELETE", "OPTIONS", "TRACE"] methods by default or as the allowed_methods parameter)
  • A list of hooks (The default hooks used are the raise_for_status and a DEBUG level log for the response body, header and status code)

Helpers

There are also some helper functions that can be used to parse the response object

  • get_response_body: Attemps to return a parsed json response body, returning the response text if it can't.

  • convert_header_to_meta_key: Converts the Django Http Request META header to be separated with a - instead of _

Tests

In order to execute tests, execute the following:

docker-compose up -d tests

This will run the tests in the python 3.9.4, 3.8.5, 3.7.5, 3.6.9 tox environments

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

http_utils-0.1.6.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

http_utils-0.1.6-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file http_utils-0.1.6.tar.gz.

File metadata

  • Download URL: http_utils-0.1.6.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.19 Linux/6.14.0-1017-azure

File hashes

Hashes for http_utils-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f58d4bf4fd8e7538e9f45ddbc45d865620dd8f1c2577b4f10cc8053f886aa784
MD5 24101e0d280e765db896d92cabb5517e
BLAKE2b-256 3381b3b6b3f0f082e85a34eac6a092ab528066fd023e4aa7abef9dc4a1b89bb3

See more details on using hashes here.

File details

Details for the file http_utils-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: http_utils-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.19 Linux/6.14.0-1017-azure

File hashes

Hashes for http_utils-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0e3334e47f378f5694f1890f3373af827c727b38fb99509a9a20726950c59758
MD5 bd3f3b86eff43d7ff6a6022e379145a8
BLAKE2b-256 5a7dca6f250fdb11583be326697c7b1f36d6c3fd8c224824f1a47a0b1df6805f

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