httprest 0.3.0__tar.gz → 0.4.1__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.3.0 → httprest-0.4.1}/PKG-INFO +4 -3
- {httprest-0.3.0 → httprest-0.4.1}/httprest/api.py +8 -4
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/base.py +8 -2
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/errors.py +12 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/fake_client.py +18 -11
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/requests_client.py +3 -1
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/response.py +6 -6
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/urllib_client.py +7 -2
- {httprest-0.3.0 → httprest-0.4.1}/pyproject.toml +9 -6
- {httprest-0.3.0 → httprest-0.4.1}/LICENSE +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/README.md +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/__init__.py +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/__init__.py +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/auth.py +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/cert.py +0 -0
- {httprest-0.3.0 → httprest-0.4.1}/httprest/http/timeout.py +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: httprest
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.1
|
|
4
4
|
Summary: A library for making simple REST-like HTTP requests
|
|
5
|
-
Home-page: https://github.com/litteratum/httprest
|
|
6
5
|
License: MIT
|
|
6
|
+
License-File: LICENSE
|
|
7
7
|
Author: Andrii Nechaiev
|
|
8
8
|
Author-email: andrewnech@gmail.com
|
|
9
9
|
Requires-Python: >=3.9,<4.0
|
|
@@ -18,6 +18,7 @@ Classifier: Programming Language :: Python :: 3.10
|
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
20
|
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
22
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
22
23
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
23
24
|
Classifier: Topic :: Software Development :: Libraries
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"""API."""
|
|
2
2
|
|
|
3
|
+
import urllib.parse
|
|
3
4
|
from typing import Optional as _Optional
|
|
5
|
+
from typing import Union as _Union
|
|
4
6
|
|
|
5
7
|
from httprest.http import HTTPClient as _HTTPClient
|
|
6
8
|
from httprest.http import HTTPResponse as _HTTPResponse
|
|
@@ -30,7 +32,8 @@ class API:
|
|
|
30
32
|
def _request(
|
|
31
33
|
self,
|
|
32
34
|
method: str,
|
|
33
|
-
endpoint: str,
|
|
35
|
+
endpoint: _Optional[str] = None,
|
|
36
|
+
data: _Optional[_Union[dict, bytes]] = None,
|
|
34
37
|
json: _Optional[dict] = None,
|
|
35
38
|
headers: _Optional[dict] = None,
|
|
36
39
|
params: _Optional[dict] = None,
|
|
@@ -40,13 +43,14 @@ class API:
|
|
|
40
43
|
# pylint: disable=too-many-arguments
|
|
41
44
|
"""Make API request.
|
|
42
45
|
|
|
43
|
-
:param endpoint: API endpoint. Will be
|
|
46
|
+
:param endpoint: API endpoint. Will be joined with the base URL
|
|
44
47
|
|
|
45
48
|
Other parameters are the same as for the `HTTPClient.request` method
|
|
46
49
|
"""
|
|
47
50
|
return self._http_client.request(
|
|
48
51
|
method=method,
|
|
49
52
|
url=self._build_url(endpoint),
|
|
53
|
+
data=data,
|
|
50
54
|
json=json,
|
|
51
55
|
headers=headers,
|
|
52
56
|
params=params,
|
|
@@ -54,8 +58,8 @@ class API:
|
|
|
54
58
|
cert=cert,
|
|
55
59
|
)
|
|
56
60
|
|
|
57
|
-
def _build_url(self, endpoint: str) -> str:
|
|
58
|
-
return
|
|
61
|
+
def _build_url(self, endpoint: _Optional[str]) -> str:
|
|
62
|
+
return urllib.parse.urljoin(self._base_url, endpoint)
|
|
59
63
|
|
|
60
64
|
def __str__(self) -> str:
|
|
61
65
|
return f"{self.__class__.__name__}(base_url='{self._base_url}')"
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
4
|
from abc import ABC, abstractmethod
|
|
5
|
-
from typing import Optional
|
|
5
|
+
from typing import Optional, Union
|
|
6
6
|
from urllib.parse import urlencode
|
|
7
7
|
|
|
8
8
|
from .auth import BaseAuth
|
|
@@ -22,6 +22,7 @@ class HTTPClient(ABC):
|
|
|
22
22
|
self,
|
|
23
23
|
method: str,
|
|
24
24
|
url: str,
|
|
25
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
25
26
|
json: Optional[dict] = None,
|
|
26
27
|
headers: Optional[dict] = None,
|
|
27
28
|
cert: Optional[ClientCertificate] = None,
|
|
@@ -43,6 +44,7 @@ class HTTPClient(ABC):
|
|
|
43
44
|
self,
|
|
44
45
|
method: str,
|
|
45
46
|
url: str,
|
|
47
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
46
48
|
json: Optional[dict] = None,
|
|
47
49
|
headers: Optional[dict] = None,
|
|
48
50
|
params: Optional[dict] = None,
|
|
@@ -54,6 +56,8 @@ class HTTPClient(ABC):
|
|
|
54
56
|
|
|
55
57
|
:param method: HTTP method to use
|
|
56
58
|
:param url: API URL
|
|
59
|
+
:param data: data to post. If dict is provided,
|
|
60
|
+
application/x-www-form-urlencoded is set automatically
|
|
57
61
|
:param json: JSON data to post
|
|
58
62
|
:param headers: headers
|
|
59
63
|
:param params: query parameters. If provided, the url will be extended
|
|
@@ -66,4 +70,6 @@ class HTTPClient(ABC):
|
|
|
66
70
|
headers = auth.apply(headers or {})
|
|
67
71
|
if params:
|
|
68
72
|
url = f"{url}?{urlencode(params)}"
|
|
69
|
-
return self._request(
|
|
73
|
+
return self._request(
|
|
74
|
+
method, url, data, json, headers=headers, cert=cert
|
|
75
|
+
)
|
|
@@ -19,3 +19,15 @@ class HTTPInvalidResponseError(HTTPRequestError):
|
|
|
19
19
|
|
|
20
20
|
class HTTPError(HTTPRequestError):
|
|
21
21
|
"""HTTP error based on the status code."""
|
|
22
|
+
|
|
23
|
+
def __init__(self, status_code: int) -> None:
|
|
24
|
+
self.status_code = status_code
|
|
25
|
+
super().__init__(f"HTTP error {status_code}")
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class HTTPClientError(HTTPError):
|
|
29
|
+
"""HTTP client error."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class HTTPServerError(HTTPError):
|
|
33
|
+
"""HTTP server error."""
|
|
@@ -5,8 +5,8 @@ from typing import Any, Dict, List, Optional, Union
|
|
|
5
5
|
from httprest.http.base import HTTPResponse
|
|
6
6
|
|
|
7
7
|
from .base import HTTPClient
|
|
8
|
-
from .errors import HTTPTimeoutError
|
|
9
8
|
from .cert import ClientCertificate
|
|
9
|
+
from .errors import HTTPTimeoutError
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
class FakeHTTPClient(HTTPClient):
|
|
@@ -23,21 +23,28 @@ class FakeHTTPClient(HTTPClient):
|
|
|
23
23
|
self,
|
|
24
24
|
method: str,
|
|
25
25
|
url: str,
|
|
26
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
26
27
|
json: Optional[dict] = None,
|
|
27
28
|
headers: Optional[dict] = None,
|
|
28
29
|
cert: Optional[ClientCertificate] = None,
|
|
29
30
|
) -> HTTPResponse:
|
|
30
31
|
# pylint:disable=too-many-arguments
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
32
|
+
entry: dict = {
|
|
33
|
+
"_method": "_request",
|
|
34
|
+
"method": method,
|
|
35
|
+
"url": url,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if data is not None:
|
|
39
|
+
entry["data"] = data
|
|
40
|
+
if json is not None:
|
|
41
|
+
entry["json"] = json
|
|
42
|
+
if headers is not None:
|
|
43
|
+
entry["headers"] = headers
|
|
44
|
+
if cert is not None:
|
|
45
|
+
entry["cert"] = cert
|
|
46
|
+
|
|
47
|
+
self.history.append(entry)
|
|
41
48
|
if not self._responses:
|
|
42
49
|
raise HTTPTimeoutError("No response provided")
|
|
43
50
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"""HTTP client which uses `requests` under the hood."""
|
|
2
2
|
|
|
3
|
-
from typing import Optional
|
|
3
|
+
from typing import Optional, Union
|
|
4
4
|
|
|
5
5
|
import requests
|
|
6
6
|
|
|
@@ -22,6 +22,7 @@ class RequestsHTTPClient(HTTPClient):
|
|
|
22
22
|
self,
|
|
23
23
|
method: str,
|
|
24
24
|
url: str,
|
|
25
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
25
26
|
json: Optional[dict] = None,
|
|
26
27
|
headers: Optional[dict] = None,
|
|
27
28
|
cert: Optional[ClientCertificate] = None,
|
|
@@ -39,6 +40,7 @@ class RequestsHTTPClient(HTTPClient):
|
|
|
39
40
|
self._session, method.lower()
|
|
40
41
|
)(
|
|
41
42
|
url,
|
|
43
|
+
data=data,
|
|
42
44
|
json=json,
|
|
43
45
|
timeout=(
|
|
44
46
|
(self._timeout.connect, self._timeout.read)
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import json as jsonlib
|
|
4
4
|
from typing import Optional
|
|
5
5
|
|
|
6
|
-
from .errors import
|
|
6
|
+
from .errors import HTTPClientError, HTTPInvalidResponseError, HTTPServerError
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
class HTTPResponse:
|
|
@@ -14,7 +14,7 @@ class HTTPResponse:
|
|
|
14
14
|
) -> None:
|
|
15
15
|
self.status_code = status_code
|
|
16
16
|
self.body = body or b""
|
|
17
|
-
self.
|
|
17
|
+
self.headers = headers
|
|
18
18
|
self._json = None
|
|
19
19
|
|
|
20
20
|
@property
|
|
@@ -25,9 +25,9 @@ class HTTPResponse:
|
|
|
25
25
|
def raise_for_status(self) -> None:
|
|
26
26
|
"""Raise exception if the response is not successful."""
|
|
27
27
|
if 400 <= self.status_code < 500:
|
|
28
|
-
raise
|
|
28
|
+
raise HTTPClientError(self.status_code)
|
|
29
29
|
if 500 <= self.status_code < 600:
|
|
30
|
-
raise
|
|
30
|
+
raise HTTPServerError(self.status_code)
|
|
31
31
|
|
|
32
32
|
@property
|
|
33
33
|
def json(self) -> Optional[dict]:
|
|
@@ -35,7 +35,7 @@ class HTTPResponse:
|
|
|
35
35
|
if self._json is not None:
|
|
36
36
|
return self._json
|
|
37
37
|
|
|
38
|
-
headers = {key.lower(): val for key, val in self.
|
|
38
|
+
headers = {key.lower(): val for key, val in self.headers.items()}
|
|
39
39
|
if "application/json" in headers.get("content-type", ""):
|
|
40
40
|
try:
|
|
41
41
|
self._json = jsonlib.loads(self.body)
|
|
@@ -54,6 +54,6 @@ class HTTPResponse:
|
|
|
54
54
|
f"{self.__class__.__name__}("
|
|
55
55
|
f"status={self.status_code}, "
|
|
56
56
|
f"body={self.body!r}, "
|
|
57
|
-
f"headers={self.
|
|
57
|
+
f"headers={self.headers}"
|
|
58
58
|
")"
|
|
59
59
|
)
|
|
@@ -5,7 +5,7 @@ import urllib
|
|
|
5
5
|
import urllib.error
|
|
6
6
|
import urllib.parse
|
|
7
7
|
import urllib.request
|
|
8
|
-
from typing import Optional
|
|
8
|
+
from typing import Optional, Union
|
|
9
9
|
|
|
10
10
|
from httprest.http import errors as _errors
|
|
11
11
|
|
|
@@ -21,12 +21,17 @@ class UrllibHTTPClient(HTTPClient):
|
|
|
21
21
|
self,
|
|
22
22
|
method: str,
|
|
23
23
|
url: str,
|
|
24
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
24
25
|
json: Optional[dict] = None,
|
|
25
26
|
headers: Optional[dict] = None,
|
|
26
27
|
cert: Optional[ClientCertificate] = None,
|
|
27
28
|
) -> HTTPResponse:
|
|
28
29
|
headers = headers or {}
|
|
29
|
-
|
|
30
|
+
|
|
31
|
+
if data is not None:
|
|
32
|
+
if isinstance(data, dict):
|
|
33
|
+
data = urllib.parse.urlencode(data).encode() # type: ignore
|
|
34
|
+
headers["Content-Type"] = "application/x-www-form-urlencoded"
|
|
30
35
|
|
|
31
36
|
if json:
|
|
32
37
|
headers["Content-Type"] = "application/json"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[tool.poetry]
|
|
2
2
|
name = "httprest"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.4.1"
|
|
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"
|
|
@@ -14,6 +14,8 @@ classifiers=[
|
|
|
14
14
|
"Programming Language :: Python :: 3.9",
|
|
15
15
|
"Programming Language :: Python :: 3.11",
|
|
16
16
|
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Programming Language :: Python :: 3.13",
|
|
18
|
+
"Programming Language :: Python :: 3.14",
|
|
17
19
|
"Programming Language :: Python :: 3 :: Only",
|
|
18
20
|
"Programming Language :: Python :: Implementation :: CPython",
|
|
19
21
|
"Topic :: Software Development :: Libraries",
|
|
@@ -35,15 +37,15 @@ pytest = "^8.3.2"
|
|
|
35
37
|
pytest-cov = "^5.0.0"
|
|
36
38
|
responses = "^0.25.3"
|
|
37
39
|
|
|
38
|
-
|
|
39
40
|
[tool.poetry.group.lint.dependencies]
|
|
40
|
-
black = {version = "^
|
|
41
|
-
pylint = {version = "^
|
|
41
|
+
black = {version = "^25", python = "3.11"}
|
|
42
|
+
pylint = {version = "^4", python = "3.11"}
|
|
42
43
|
flake8 = {version = "^7.1.1", python = "3.11"}
|
|
43
|
-
flake8-bugbear = {version = "^
|
|
44
|
+
flake8-bugbear = {version = "^25", python = "3.11"}
|
|
44
45
|
mypy = {version = "^1.11.1", python = "3.11"}
|
|
45
46
|
pydocstyle = {version = "^6.3.0", python = "3.11"}
|
|
46
47
|
types-requests = "^2.32.0.20240914"
|
|
48
|
+
pip-audit = "^2.9.0"
|
|
47
49
|
|
|
48
50
|
[build-system]
|
|
49
51
|
requires = ["poetry-core"]
|
|
@@ -82,7 +84,8 @@ good-names = [
|
|
|
82
84
|
[tool.pylint.'MESSAGES CONTROL']
|
|
83
85
|
disable = [
|
|
84
86
|
"fixme",
|
|
85
|
-
"import-error"
|
|
87
|
+
"import-error",
|
|
88
|
+
"too-many-positional-arguments",
|
|
86
89
|
]
|
|
87
90
|
|
|
88
91
|
[tool.pylint.'VARIABLES']
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|