Skip to main content

Django''s application to provide simple and shared requests client.

Project description

django-requests-api PyPi license

PyPi status PyPi version PyPi python version PyPi downloads PyPi downloads PyPi downloads

GitHub GitHub release GitHub release

Test codecov.io pre-commit.ci status gitthub.com

Check Demo Project

  • Browser the demo app on-line on Heroku
  • Check the demo repo on GitHub

Requirements

  • Python 3.8+ supported.
  • Django 4.2+ supported.

Setup

  1. Install from pip:
pip install django-requests-api
  1. Modify settings.py by adding the app to INSTALLED_APPS:
INSTALLED_APPS = (
    # ...
    "requests_api",
    # ...
)

Usage

from requests_api import RequestsApi

client = RequestsApi("https://api.publicapis.org")
r = client.get("/entries")
print(r.json())

github = RequestsApi("https://api.github.com", headers={"Authorization": "token abcdef"})
r = github.get("/user", headers={"Accept": "application/json"})
print(r.text)

Paths are joined to base_url with a single slash (with or without leading/trailing slashes on either part). If the path is already an absolute URL (http:// or https://), it is used as-is. The package declares a dependency on requests (see install_requires).

Reusable helpers

RequestsApi lives in requests_api.requests_api; the utilities are in requests_api.helpers. All are re-exported from the package root so you can still use from requests_api import ….

from requests_api import (
    copy_get_params_with_overrides,
    normalize_api_language,
    requests_api_for_base,
)

# Map Django language to an API that only supports a subset of codes
lang = normalize_api_language(
    request.LANGUAGE_CODE,
    allowed=("it", "en"),
    fallback="en",
)

# Outbound GET: keep the browser query string but force lang=
params = copy_get_params_with_overrides(request, lang=lang)

# One shared Session / connection pool per base URL in the process
client = requests_api_for_base("https://www.example.com")
r = client.get("api/resource", params=params, timeout=30)

Run Example Project

git clone --depth=50 --branch=django-requests-api https://github.com/DLRSP/example.git DLRSP/example
cd DLRSP/example
python manage.py runserver

Now browser the app @ http://127.0.0.1:8000

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

django_requests_api-0.6.1.tar.gz (24.7 kB view details)

Uploaded Source

Built Distribution

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

django_requests_api-0.6.1-py2.py3-none-any.whl (6.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django_requests_api-0.6.1.tar.gz.

File metadata

  • Download URL: django_requests_api-0.6.1.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for django_requests_api-0.6.1.tar.gz
Algorithm Hash digest
SHA256 41cae5c3429e7e7e62bf3dc38e5f177c1b6ec3608763e493a014e815564cb13f
MD5 2f1ace7425a672e5a6f79648e911a097
BLAKE2b-256 20029a046729a5679274ddc28945d79df9a98d716d6bc1b5c904e7dea56a6a1b

See more details on using hashes here.

File details

Details for the file django_requests_api-0.6.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for django_requests_api-0.6.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c98f00554606292ed7f3b5b064988eff7af469fa3b6e63df8727421376a136a6
MD5 071ff9c0c23344e0cbba55f1e19c044d
BLAKE2b-256 c6dce12d7f9990eba9aa5a229ac130d8ffd40b81ec0452ad1141beda8a2c9c55

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