httprest 0.2.0__tar.gz → 0.4.0__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.2.0 → httprest-0.4.0}/PKG-INFO +3 -3
- {httprest-0.2.0 → httprest-0.4.0}/httprest/api.py +3 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/__init__.py +2 -1
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/base.py +11 -4
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/errors.py +12 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/fake_client.py +18 -11
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/requests_client.py +11 -4
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/response.py +6 -6
- httprest-0.4.0/httprest/http/timeout.py +18 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/urllib_client.py +8 -3
- {httprest-0.2.0 → httprest-0.4.0}/pyproject.toml +1 -1
- {httprest-0.2.0 → httprest-0.4.0}/LICENSE +0 -0
- {httprest-0.2.0 → httprest-0.4.0}/README.md +0 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/__init__.py +0 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/auth.py +0 -0
- {httprest-0.2.0 → httprest-0.4.0}/httprest/http/cert.py +0 -0
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
2
|
Name: httprest
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.4.0
|
|
4
4
|
Summary: A library for making simple REST-like HTTP requests
|
|
5
|
-
Home-page: https://github.com/litteratum/httprest
|
|
6
5
|
License: MIT
|
|
7
6
|
Author: Andrii Nechaiev
|
|
8
7
|
Author-email: andrewnech@gmail.com
|
|
@@ -17,6 +16,7 @@ Classifier: Programming Language :: Python :: 3.9
|
|
|
17
16
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
20
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
21
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
22
|
Classifier: Topic :: Software Development :: Libraries
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"""API."""
|
|
2
2
|
|
|
3
3
|
from typing import Optional as _Optional
|
|
4
|
+
from typing import Union as _Union
|
|
4
5
|
|
|
5
6
|
from httprest.http import HTTPClient as _HTTPClient
|
|
6
7
|
from httprest.http import HTTPResponse as _HTTPResponse
|
|
@@ -31,6 +32,7 @@ class API:
|
|
|
31
32
|
self,
|
|
32
33
|
method: str,
|
|
33
34
|
endpoint: str,
|
|
35
|
+
data: _Optional[_Union[dict, bytes]] = None,
|
|
34
36
|
json: _Optional[dict] = None,
|
|
35
37
|
headers: _Optional[dict] = None,
|
|
36
38
|
params: _Optional[dict] = None,
|
|
@@ -47,6 +49,7 @@ class API:
|
|
|
47
49
|
return self._http_client.request(
|
|
48
50
|
method=method,
|
|
49
51
|
url=self._build_url(endpoint),
|
|
52
|
+
data=data,
|
|
50
53
|
json=json,
|
|
51
54
|
headers=headers,
|
|
52
55
|
params=params,
|
|
@@ -2,25 +2,27 @@
|
|
|
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
|
|
9
9
|
from .cert import ClientCertificate
|
|
10
10
|
from .response import HTTPResponse
|
|
11
|
+
from .timeout import Timeout
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class HTTPClient(ABC):
|
|
14
15
|
"""Base HTTP client."""
|
|
15
16
|
|
|
16
|
-
def __init__(self,
|
|
17
|
-
self.
|
|
17
|
+
def __init__(self, timeout: Optional[Timeout] = None) -> None:
|
|
18
|
+
self._timeout = timeout
|
|
18
19
|
|
|
19
20
|
@abstractmethod
|
|
20
21
|
def _request(
|
|
21
22
|
self,
|
|
22
23
|
method: str,
|
|
23
24
|
url: str,
|
|
25
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
24
26
|
json: Optional[dict] = None,
|
|
25
27
|
headers: Optional[dict] = None,
|
|
26
28
|
cert: Optional[ClientCertificate] = None,
|
|
@@ -42,6 +44,7 @@ class HTTPClient(ABC):
|
|
|
42
44
|
self,
|
|
43
45
|
method: str,
|
|
44
46
|
url: str,
|
|
47
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
45
48
|
json: Optional[dict] = None,
|
|
46
49
|
headers: Optional[dict] = None,
|
|
47
50
|
params: Optional[dict] = None,
|
|
@@ -53,6 +56,8 @@ class HTTPClient(ABC):
|
|
|
53
56
|
|
|
54
57
|
:param method: HTTP method to use
|
|
55
58
|
:param url: API URL
|
|
59
|
+
:param data: data to post. If dict is provided,
|
|
60
|
+
application/x-www-form-urlencoded is set automatically
|
|
56
61
|
:param json: JSON data to post
|
|
57
62
|
:param headers: headers
|
|
58
63
|
:param params: query parameters. If provided, the url will be extended
|
|
@@ -65,4 +70,6 @@ class HTTPClient(ABC):
|
|
|
65
70
|
headers = auth.apply(headers or {})
|
|
66
71
|
if params:
|
|
67
72
|
url = f"{url}?{urlencode(params)}"
|
|
68
|
-
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
|
|
|
@@ -8,19 +8,21 @@ from httprest.http import errors as _errors
|
|
|
8
8
|
|
|
9
9
|
from .base import HTTPClient, HTTPResponse
|
|
10
10
|
from .cert import ClientCertificate
|
|
11
|
+
from .timeout import Timeout
|
|
11
12
|
|
|
12
13
|
|
|
13
14
|
class RequestsHTTPClient(HTTPClient):
|
|
14
15
|
"""`requests` HTTP client."""
|
|
15
16
|
|
|
16
|
-
def __init__(self,
|
|
17
|
-
super().__init__(
|
|
17
|
+
def __init__(self, timeout: Optional[Timeout] = None) -> None:
|
|
18
|
+
super().__init__(timeout)
|
|
18
19
|
self._session = requests.Session()
|
|
19
20
|
|
|
20
21
|
def _request(
|
|
21
22
|
self,
|
|
22
23
|
method: str,
|
|
23
24
|
url: str,
|
|
25
|
+
data: Optional[Union[dict, bytes]] = None,
|
|
24
26
|
json: Optional[dict] = None,
|
|
25
27
|
headers: Optional[dict] = None,
|
|
26
28
|
cert: Optional[ClientCertificate] = None,
|
|
@@ -38,8 +40,13 @@ class RequestsHTTPClient(HTTPClient):
|
|
|
38
40
|
self._session, method.lower()
|
|
39
41
|
)(
|
|
40
42
|
url,
|
|
43
|
+
data=data,
|
|
41
44
|
json=json,
|
|
42
|
-
timeout=
|
|
45
|
+
timeout=(
|
|
46
|
+
(self._timeout.connect, self._timeout.read)
|
|
47
|
+
if self._timeout
|
|
48
|
+
else None
|
|
49
|
+
),
|
|
43
50
|
headers=headers,
|
|
44
51
|
cert=client_cert,
|
|
45
52
|
)
|
|
@@ -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
|
)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Timeout wrappers."""
|
|
2
|
+
|
|
3
|
+
from typing import Optional
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class Timeout:
|
|
7
|
+
"""HTTP request timeout."""
|
|
8
|
+
|
|
9
|
+
def __init__(
|
|
10
|
+
self, connect: Optional[float] = None, read: Optional[float] = None
|
|
11
|
+
) -> None:
|
|
12
|
+
"""Init timeout.
|
|
13
|
+
|
|
14
|
+
:param connect: connect timeout in seconds
|
|
15
|
+
:param read: read timeout in seconds
|
|
16
|
+
"""
|
|
17
|
+
self.connect = connect
|
|
18
|
+
self.read = read
|
|
@@ -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"
|
|
@@ -40,7 +45,7 @@ class UrllibHTTPClient(HTTPClient):
|
|
|
40
45
|
headers=headers,
|
|
41
46
|
method=method.upper(),
|
|
42
47
|
),
|
|
43
|
-
timeout=self.
|
|
48
|
+
timeout=self._timeout.read if self._timeout else None,
|
|
44
49
|
) as response:
|
|
45
50
|
return HTTPResponse(
|
|
46
51
|
response.status, response.read(), dict(response.headers)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|