Skip to main content

Types for HTTP requests and responses

Project description

HTTP types in Python

CircleCI PyPi License

Types for HTTP request and response.

Requires Python >= 3.6.

Installation

pip install http-types

Usage

Creating objects by hand

You can create typed Request and Response objects directly by listing arguments:

from http_types import Request, Response, RequestBuilder

req = Request(method="get",
              host="api.github.com",
              path="/user/repos?id=1",
              pathname="/user/repos",
              protocol="https",
              query={"id": ["1"]},
              body="",
              bodyAsJson="",
              headers={})  # Valid `Request` object, passes type-checking

res = Response(statusCode=200, body="OK", headers={})  # Valid `Response` object, passes type-checking

invalid_req = Request(method="get")  # Fails type-checking, missing arguments
invalid_res = Response(body=1, ...)  # Fails type-checking, wrong type for `body`

Creating objects via helper methods

from http_types import RequestBuilder, ResponseBuilder, HttpExchangeBuilder

# Create Request object from URL
url = "https://api.github.com/v1/repos?id=1"
req = RequestBuilder.from_url(url)  # Validated Request object

# Create request object from dictionary
req_obj = {
    'host': 'api.github.com',
    'protocol': 'https',
    'method': 'get',
    'path': '/v1/users',  # pathname inferred automatically
    'query': {}
}
req = RequestBuilder.from_dict(req_obj)  # `body`, `pathname`, and `headers` are optional

Validating objects

from http_types import RequestBuilder, ResponseBuilder

req = Request(...)
RequestBuilder.validate(req)  # Validate `Request` object

res = Response(...)
ResponseBuilder.validate(res)  # Validate `Response` object

Development

Local development

  1. Create a new virtual environment.
  2. Install dependencies: pip install --upgrade -e '.[dev]'

Running tests

Run pytest tests/ or python setup.py test.

Configuration for pytest is found in pytest.ini file.

Running type-checking

Install pyright and run pyright --lib or python setup.py typecheck.

Configuration for pyright is found in pyrightconfig.json file.

Automated tests

See .circleci/config.yml.

Publishing package

  1. Bump the version in setup.py if the version is the same as in the published package. Commit and push.
  2. Run python setup.py test, python setup.py typecheck and python setup.py dist to check everything works
  3. To build and upload the package, run python setup.py upload. Insert PyPI credentials to upload the pacakge to PyPI. The command will also run git tag to tag the commit as a release and push the tags to remote.

To see what the different commands do, see Command classes in setup.py.

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-types-0.0.6.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

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

http_types-0.0.6-py2.py3-none-any.whl (7.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file http-types-0.0.6.tar.gz.

File metadata

  • Download URL: http-types-0.0.6.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for http-types-0.0.6.tar.gz
Algorithm Hash digest
SHA256 8dc0041d9f3874fa5d8a0390c71b3f5e8b28141f59890777c03c4fb3afde4df5
MD5 50cd6250bdfd7712decc0dd366d0f204
BLAKE2b-256 6e5d9772d4482a742e5444fee3f4c7c970d2923aebedf8440b7c8851146cfbc4

See more details on using hashes here.

File details

Details for the file http_types-0.0.6-py2.py3-none-any.whl.

File metadata

  • Download URL: http_types-0.0.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.1 CPython/3.6.10

File hashes

Hashes for http_types-0.0.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4614573e2ce6cb856d3a463d89140c27b31870c12b369dc8017848fb18c24016
MD5 f6cf94e4f07574503a0c414914c4ac6f
BLAKE2b-256 a8d3481cd71aeb094ae2e5c63360d06749aa1316afa928d02ba58afddd06d796

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