httprest 0.1.0__tar.gz → 0.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {httprest-0.1.0 → httprest-0.1.2}/PKG-INFO +20 -4
- {httprest-0.1.0 → httprest-0.1.2}/README.md +17 -3
- httprest-0.1.2/httprest/http/fake_client.py +43 -0
- httprest-0.1.2/httprest/http/requests_client.py +46 -0
- {httprest-0.1.0 → httprest-0.1.2}/pyproject.toml +9 -2
- {httprest-0.1.0 → httprest-0.1.2}/LICENSE +0 -0
- {httprest-0.1.0 → httprest-0.1.2}/httprest/__init__.py +0 -0
- {httprest-0.1.0 → httprest-0.1.2}/httprest/api.py +0 -0
- {httprest-0.1.0 → httprest-0.1.2}/httprest/http/__init__.py +0 -0
- {httprest-0.1.0 → httprest-0.1.2}/httprest/http/base.py +0 -0
- {httprest-0.1.0 → httprest-0.1.2}/httprest/http/urllib_client.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: httprest
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A library for making simple REST-like HTTP requests
|
|
5
5
|
Home-page: https://github.com/litteratum/httprest
|
|
6
6
|
License: MIT
|
|
@@ -21,6 +21,8 @@ Classifier: Programming Language :: Python :: 3 :: Only
|
|
|
21
21
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
22
|
Classifier: Topic :: Software Development :: Libraries
|
|
23
23
|
Classifier: Topic :: Utilities
|
|
24
|
+
Provides-Extra: requests-client
|
|
25
|
+
Requires-Dist: requests (>=2.32.3,<3.0.0) ; extra == "requests-client"
|
|
24
26
|
Project-URL: Repository, https://github.com/litteratum/httprest
|
|
25
27
|
Description-Content-Type: text/markdown
|
|
26
28
|
|
|
@@ -51,10 +53,11 @@ pip install httprest
|
|
|
51
53
|
|
|
52
54
|
|
|
53
55
|
## HTTP client
|
|
54
|
-
The library exposes an `HTTPClient` interface
|
|
55
|
-
|
|
56
|
+
The library exposes an `HTTPClient` interface and provides two implementations for it:
|
|
57
|
+
1. `http.urllib_client.UrllibHTTPClient`: the default implementation, uses the `urllib` library under the hood
|
|
58
|
+
2. `http.requests_client.RequestsHTTPClient`: uses the `requests` library under the hood
|
|
56
59
|
|
|
57
|
-
You may define your own HTTP client like this:
|
|
60
|
+
You may also define your own HTTP client like this:
|
|
58
61
|
```python
|
|
59
62
|
from httprest.http import HTTPClient, HTTPResponse
|
|
60
63
|
|
|
@@ -68,3 +71,16 @@ And then you simply use it in the API client:
|
|
|
68
71
|
api = MyAPI(..., http_client=MyHTTPClient())
|
|
69
72
|
```
|
|
70
73
|
|
|
74
|
+
### Fake client
|
|
75
|
+
The library provides the `http.fake_client` module containing `FakeHTTPClient` class.
|
|
76
|
+
That class may be used for API testing. Example:
|
|
77
|
+
```python
|
|
78
|
+
from httprest.http.fake_client import FakeHTTPClient, HTTPResponse
|
|
79
|
+
|
|
80
|
+
http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
|
|
81
|
+
api = MyAPI(..., http_client=http_client)
|
|
82
|
+
api.operation()
|
|
83
|
+
# assert your expectations here
|
|
84
|
+
assert http_client.history == [...]
|
|
85
|
+
```
|
|
86
|
+
|
|
@@ -25,10 +25,11 @@ pip install httprest
|
|
|
25
25
|
|
|
26
26
|
|
|
27
27
|
## HTTP client
|
|
28
|
-
The library exposes an `HTTPClient` interface
|
|
29
|
-
|
|
28
|
+
The library exposes an `HTTPClient` interface and provides two implementations for it:
|
|
29
|
+
1. `http.urllib_client.UrllibHTTPClient`: the default implementation, uses the `urllib` library under the hood
|
|
30
|
+
2. `http.requests_client.RequestsHTTPClient`: uses the `requests` library under the hood
|
|
30
31
|
|
|
31
|
-
You may define your own HTTP client like this:
|
|
32
|
+
You may also define your own HTTP client like this:
|
|
32
33
|
```python
|
|
33
34
|
from httprest.http import HTTPClient, HTTPResponse
|
|
34
35
|
|
|
@@ -41,3 +42,16 @@ And then you simply use it in the API client:
|
|
|
41
42
|
|
|
42
43
|
api = MyAPI(..., http_client=MyHTTPClient())
|
|
43
44
|
```
|
|
45
|
+
|
|
46
|
+
### Fake client
|
|
47
|
+
The library provides the `http.fake_client` module containing `FakeHTTPClient` class.
|
|
48
|
+
That class may be used for API testing. Example:
|
|
49
|
+
```python
|
|
50
|
+
from httprest.http.fake_client import FakeHTTPClient, HTTPResponse
|
|
51
|
+
|
|
52
|
+
http_client = FakeHTTPClient(responses=[HTTPResponse(200, b"", headers={})])
|
|
53
|
+
api = MyAPI(..., http_client=http_client)
|
|
54
|
+
api.operation()
|
|
55
|
+
# assert your expectations here
|
|
56
|
+
assert http_client.history == [...]
|
|
57
|
+
```
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Fake client."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, List, Optional, Union
|
|
4
|
+
|
|
5
|
+
from httprest.http.base import HTTPResponse
|
|
6
|
+
|
|
7
|
+
from .base import HTTPClient, HTTPTimeoutError
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class FakeHTTPClient(HTTPClient):
|
|
11
|
+
"""Fake HTTP client."""
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self, responses: Optional[List[Union[Exception, HTTPResponse]]] = None
|
|
15
|
+
) -> None:
|
|
16
|
+
# pylint:disable=super-init-not-called
|
|
17
|
+
self.history: List[Dict[str, Optional[Any]]] = []
|
|
18
|
+
self._responses = responses
|
|
19
|
+
|
|
20
|
+
def _request(
|
|
21
|
+
self,
|
|
22
|
+
method: str,
|
|
23
|
+
url: str,
|
|
24
|
+
json: Optional[dict] = None,
|
|
25
|
+
headers: Optional[dict] = None,
|
|
26
|
+
) -> HTTPResponse:
|
|
27
|
+
self.history.append(
|
|
28
|
+
{
|
|
29
|
+
"_method": "_request",
|
|
30
|
+
"method": method,
|
|
31
|
+
"url": url,
|
|
32
|
+
"json": json,
|
|
33
|
+
"headers": headers,
|
|
34
|
+
}
|
|
35
|
+
)
|
|
36
|
+
if not self._responses:
|
|
37
|
+
raise HTTPTimeoutError("No response provided")
|
|
38
|
+
|
|
39
|
+
response = self._responses.pop(0)
|
|
40
|
+
if isinstance(response, Exception):
|
|
41
|
+
raise response
|
|
42
|
+
|
|
43
|
+
return response
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""HTTP client which uses `requests` under the hood."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
from .base import (
|
|
8
|
+
HTTPClient,
|
|
9
|
+
HTTPConnectionError,
|
|
10
|
+
HTTPRequestError,
|
|
11
|
+
HTTPResponse,
|
|
12
|
+
HTTPTimeoutError,
|
|
13
|
+
)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RequestsHTTPClient(HTTPClient):
|
|
17
|
+
"""`requests` HTTP client."""
|
|
18
|
+
|
|
19
|
+
def __init__(self, request_timeout: float = 5) -> None:
|
|
20
|
+
super().__init__(request_timeout)
|
|
21
|
+
self._session = requests.Session()
|
|
22
|
+
|
|
23
|
+
def _request(
|
|
24
|
+
self,
|
|
25
|
+
method: str,
|
|
26
|
+
url: str,
|
|
27
|
+
json: Optional[dict] = None,
|
|
28
|
+
headers: Optional[dict] = None,
|
|
29
|
+
) -> HTTPResponse:
|
|
30
|
+
try:
|
|
31
|
+
response: requests.Response = getattr(
|
|
32
|
+
self._session, method.lower()
|
|
33
|
+
)(url, json=json, timeout=self.request_timeout, headers=headers)
|
|
34
|
+
except ConnectionError as exc:
|
|
35
|
+
raise HTTPConnectionError(exc) from exc
|
|
36
|
+
except requests.Timeout as exc:
|
|
37
|
+
raise HTTPTimeoutError(exc) from exc
|
|
38
|
+
except requests.RequestException as exc:
|
|
39
|
+
raise HTTPRequestError(exc) from exc
|
|
40
|
+
|
|
41
|
+
return HTTPResponse(
|
|
42
|
+
response.status_code, response.content, dict(response.headers)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
def __str__(self) -> str:
|
|
46
|
+
return self.__class__.__name__
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "httprest"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.2"
|
|
4
4
|
description = "A library for making simple REST-like HTTP requests"
|
|
5
5
|
authors = ["Andrii Nechaiev <andrewnech@gmail.com>"]
|
|
6
6
|
readme = "README.md"
|
|
@@ -22,13 +22,18 @@ classifiers=[
|
|
|
22
22
|
"Topic :: Utilities",
|
|
23
23
|
]
|
|
24
24
|
|
|
25
|
+
|
|
25
26
|
[tool.poetry.dependencies]
|
|
26
27
|
python = "^3.9"
|
|
28
|
+
requests = {version = "^2.32.3", optional = true}
|
|
27
29
|
|
|
30
|
+
[tool.poetry.extras]
|
|
31
|
+
requests-client = ["requests"]
|
|
28
32
|
|
|
29
33
|
[tool.poetry.group.test.dependencies]
|
|
30
34
|
pytest = "^8.3.2"
|
|
31
35
|
pytest-cov = "^5.0.0"
|
|
36
|
+
responses = "^0.25.3"
|
|
32
37
|
|
|
33
38
|
|
|
34
39
|
[tool.poetry.group.lint.dependencies]
|
|
@@ -38,6 +43,7 @@ flake8 = {version = "^7.1.1", python = "3.11"}
|
|
|
38
43
|
flake8-bugbear = {version = "^24.8.19", python = "3.11"}
|
|
39
44
|
mypy = {version = "^1.11.1", python = "3.11"}
|
|
40
45
|
pydocstyle = {version = "^6.3.0", python = "3.11"}
|
|
46
|
+
types-requests = "^2.32.0.20240914"
|
|
41
47
|
|
|
42
48
|
[build-system]
|
|
43
49
|
requires = ["poetry-core"]
|
|
@@ -61,7 +67,7 @@ ignore_missing_imports = true
|
|
|
61
67
|
ignore = "D102,D105,D107,D203,D213,D413"
|
|
62
68
|
|
|
63
69
|
[tool.pylint.'MAIN']
|
|
64
|
-
py-version=3.
|
|
70
|
+
py-version=3.9
|
|
65
71
|
|
|
66
72
|
[tool.pylint.'BASIC']
|
|
67
73
|
good-names = [
|
|
@@ -76,6 +82,7 @@ good-names = [
|
|
|
76
82
|
[tool.pylint.'MESSAGES CONTROL']
|
|
77
83
|
disable = [
|
|
78
84
|
"fixme",
|
|
85
|
+
"import-error"
|
|
79
86
|
]
|
|
80
87
|
|
|
81
88
|
[tool.pylint.'VARIABLES']
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|