Types for HTTP requests and responses
Project description
HTTP types in Python
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="",
body_as_json="",
headers={}) # Valid `Request` object, passes type-checking
res = Response(status_code=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, RequestResponseBuilder
# 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 = { 'method': 'get', 'body': 'body', ... }
req = RequestBuilder.from_dict(req_obj)
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
- Create a new virtual environment.
- 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
- Bump the version in setup.py if the version is the same as in the published package. Commit and push.
- Run
python setup.py test
,python setup.py typecheck
andpython setup.py dist
to check everything works - To build and upload the package, run
python setup.py upload
. Insert PyPI credentials to upload the pacakge toPyPI
. The command will also rungit 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
Release history Release notifications | RSS feed
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.2.tar.gz
(5.7 kB
view hashes)
Built Distribution
Close
Hashes for http_types-0.0.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8adc6cb75db368a9abcd56af2ebf480f4c5afe98e7982146f4079ac14b5d5047 |
|
MD5 | 052aa6bd377c3ca3f4aa15a2768aa1dc |
|
BLAKE2b-256 | 4693d7f9876e787cffee69d25bc8eafbd8beb1b45c7735b9fe9daad073fe6f0d |