Skip to main content

A library for making simple REST-like HTTP requests

Project description

httprest

httprest is a minimalistic framework for communicating with REST APIs. Its goal is to reduce a boilerplate code required to communicate with APIs.

  • It has the http package exposing an HTTP client interface. There is also a client implementation that uses urllib under the hood. So no need to use extra libraries like requests
  • It has the api.API class which may be overridden and used to make API calls

Usage

from httprest import API

class MyAPI(API):
    def operation(self):
        result = self._request("POST", "/operation/endpoint", json={...})
        if result.ok:
            print(result.json)

api = MyAPI("http://api.com")
api.operation()

Installation

pip install httprest

HTTP client

The library exposes an HTTPClient interface and provides two implementations for it:

  1. http.urllib_client.UrllibHTTPClient: the default implementation, uses the urllib library under the hood
  2. http.requests_client.RequestsHTTPClient: uses the requests library under the hood

Custom HTTP client

from httprest.http import HTTPClient, HTTPResponse

class MyHTTPClient(HTTPClient):
    def _request(...) -> HTTPResponse

And then you simply use it in the API client:

api = MyAPI(..., http_client=MyHTTPClient())

Fake client

The library provides the http.fake_client module containing FakeHTTPClient class. That class may be used for API testing. Example:

from httprest.http.fake_client import FakeHTTPClient, HTTPResponse

http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
api = MyAPI(..., http_client=http_client)
api.operation()
# assert your expectations here
assert http_client.history == [...]

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

httprest-0.4.2.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

httprest-0.4.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file httprest-0.4.2.tar.gz.

File metadata

  • Download URL: httprest-0.4.2.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.9 Linux/6.14.0-1017-azure

File hashes

Hashes for httprest-0.4.2.tar.gz
Algorithm Hash digest
SHA256 2e70f5aff02bbfe93d1977cde42479e49c1a37a3693e79f33e01d1e8dc77ecc2
MD5 7d66e7c3b5a260c438f6ffd8de7baeec
BLAKE2b-256 84c6de6f9d11d994fe00672bccd4d816d95970154e1b0b58d8ad11d6517b53ea

See more details on using hashes here.

File details

Details for the file httprest-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: httprest-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.10.9 Linux/6.14.0-1017-azure

File hashes

Hashes for httprest-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a6ae3c852c87b32299b64c296127ab042a48a9850e10aca5b3e5d4a561f6f46
MD5 962c962ba17585176b8b8f3f46567607
BLAKE2b-256 f80a3b5e667229b67284cc177d242d461327fff8985954ce85d677c01f93c6e1

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