Types for HTTP requests and responses


Keywords
http, http-types, json, python
License
MIT
Install
pip install http-types==0.0.10

Documentation

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)

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"] == "get"
    assert exchange["request"]["protocol"] == "https"
    assert exchange["response"]["statusCode"] == 200

Development

Initial setup:

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

To test, run python setup.py test, which will:

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.