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.2.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.2-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.2.tar.gz.

File metadata

  • Download URL: django_requests_api-0.6.2.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.2.tar.gz
Algorithm Hash digest
SHA256 3aa3bf7dc41ed33d24012f81522583cd2d2d68cba8f9dddfdfe312db127eee07
MD5 6a13e827487d05001817764db86bb7a5
BLAKE2b-256 cb9d10c7f2f688ba77f8bdb2d45cacd0c28a618e5eed45beb543622a6ec2f9e3

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_requests_api-0.6.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 41fdc9b4409aa16b85d303281295a4112ae2b5d5eed095519757234e9c598e38
MD5 fdbcbe321fe1b0c5bcfb9f57e81995ec
BLAKE2b-256 64be44e3316b74942eaf69d0889dd9da1ad76a5ed7b87fadd8491db4684fa791

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