Types for HTTP requests and responses
Project description
HTTP Types in Python
Python (3.6 or later) library to read and write records of HTTP exchanges in the HTTP types format.
Installation
pip install http-types
Writing HTTP exchanges
Using HttpExchangeWriter
a recording of HTTP traffic can be serialised for use with any program that can handle the HTTP Types format:
request = RequestBuilder.from_dict({
"host": "api.github.com",
"protocol": "https",
"method": "get",
"pathname": "/v1/users",
"query": {"a": "b", "q": ["1", "2"]},
}
)
response = ResponseBuilder.from_dict({
"statusCode": 200,
"headers": {"content-type": "text/plain"},
"body": "(response body string)",
}
)
exchange = HttpExchange(request=request, response=response)
with tempfile.TemporaryFile(mode="w") as output:
writer = HttpExchangeWriter(output)
writer.write(exchange)
# Serialize to dictionary
as_dict = HttpExchangeWriter.to_dict(exchange)
# Serialize to JSON string
as_str = HttpExchangeWriter.to_json(exchange)
Reading HTTP exchanges
With HttpExchangeReader
recordings in the HTTP Types format can be read for processing:
for exchange in HttpExchangeReader.from_jsonl(input_file):
assert exchange.request.method == HttpMethod.GET
assert exchange.request.protocol == Protocol.HTTPS
assert exchange.response.statusCode == 200
Development
Initial setup:
- Create a new virtual environment.
- Install dependencies:
pip install --upgrade -e '.[dev]'
To test, run python setup.py test
, which will:
- Enforce code formatting using black.
- Test with
pytest
, configured in pytest.ini. - Type check with
mypy
. - Enforce style guide with flake8, configured in .flake8.
Publishing
- 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
andpython setup.py dist
to check that everything works. - To build and upload the package, run
python setup.py upload
. Insert PyPI credentials to upload the package 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.17.tar.gz
(7.8 kB
view details)
Built Distribution
File details
Details for the file http-types-0.0.17.tar.gz
.
File metadata
- Download URL: http-types-0.0.17.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0c9fd545ef8a6b71991d1aeb5abf930953929d47d6be6cd640c21c902c30512a |
|
MD5 | 9f468640116335f5aa4676e0988ffba2 |
|
BLAKE2b-256 | 21035fe71b6cd7e3f2c5c5f105efa087db4ebb2f59b8961eb57b8d3048538f17 |
File details
Details for the file http_types-0.0.17-py2.py3-none-any.whl
.
File metadata
- Download URL: http_types-0.0.17-py2.py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae8b50657fd55272a270e45ee5fdd0ffd3c46f3376d2ac27c1ce527d17892e31 |
|
MD5 | 3c44d36f6992c05d2845d6da717c82e2 |
|
BLAKE2b-256 | f6f9544a83792d312b53e4d264d2d5a9a24ae4544e9d5e21ec12b807c6481774 |