Skip to main content

HTTP Types in Python

CircleCI PyPi License

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 HttpExchangeWritera 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:

  1. Create a new virtual environment.
  2. 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

  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 and python setup.py dist to check that everything works.
  3. To build and upload the package, run python setup.py upload. Insert PyPI credentials to upload the package 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.

Metadata

Release files for http-types 0.0.18

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for http-types 0.0.18
File Size Uploaded
http-types-0.0.18.tar.gz 8.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for http-types 0.0.18
File Interpreter ABI Platform
http_types-0.0.18-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 16.6 kB

Release files / http-types-0.0.18.tar.gz

Download URL http-types-0.0.18.tar.gz
Size 8.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d3fd2d56784efd0be102d8e47cd8c1fa555ebf03806b940dc1bef20cb4a78a04
BLAKE2b-256 checksum
How to use checksums
12eed363f0f6597d1eca7da2b96c129ce577984e148377144e06c40fbf754efb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release files / http_types-0.0.18-py2.py3-none-any.whl

Download URL http_types-0.0.18-py2.py3-none-any.whl
Size 8.1 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
274be507e4dea51efe3ad0b40eae730e9b65f07a6b08e6c18191bd894b19ccb7
BLAKE2b-256 checksum
How to use checksums
5cf12e1d75b59c2e9426150fe0054fd908f9933173d4dc9ab6bae50a3448ccf5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

Release history Release notifications | RSS feed

This release

0.0.18 This release

2 release files

0.0.16

2 release files

0.0.15

1 release file

0.0.14

2 release files

0.0.12

2 release files

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

0.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page