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
httppackage exposing an HTTP client interface. There is also a client implementation that usesurllibunder the hood. So no need to use extra libraries likerequests - It has the
api.APIclass 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:
http.urllib_client.UrllibHTTPClient: the default implementation, uses theurlliblibrary under the hoodhttp.requests_client.RequestsHTTPClient: uses therequestslibrary 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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httprest-0.4.3.tar.gz.
File metadata
- Download URL: httprest-0.4.3.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5ef30e0f2978fc7293db7d1d3f5f258dd597e48dd4c22c980ee1f3dda0b4067c
|
|
| MD5 |
eca5d86be6c83f66ae8aa5c95b6974b9
|
|
| BLAKE2b-256 |
90936cd1a3eaa9d6e7a280d312e776c08ce221074426c8432667311b88802e9e
|
File details
Details for the file httprest-0.4.3-py3-none-any.whl.
File metadata
- Download URL: httprest-0.4.3-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
67974ae592eede8b607225d33f6dbea5d2d1900671137eaecc2058ab4541d8df
|
|
| MD5 |
b05324436474b65b1e2615ddd860ed81
|
|
| BLAKE2b-256 |
7a20113f062b47d5fc032685c137375390380427c4167d86523c8319586de01f
|