httprest 0.1.3__tar.gz → 0.3.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.1.3 → httprest-0.3.0}/PKG-INFO +3 -2
- {httprest-0.1.3 → httprest-0.3.0}/README.md +1 -1
- {httprest-0.1.3 → httprest-0.3.0}/httprest/api.py +3 -0
- httprest-0.3.0/httprest/http/__init__.py +9 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/base.py +4 -64
- httprest-0.3.0/httprest/http/errors.py +21 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/fake_client.py +2 -1
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/requests_client.py +14 -13
- httprest-0.3.0/httprest/http/response.py +59 -0
- httprest-0.3.0/httprest/http/timeout.py +18 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/urllib_client.py +7 -11
- {httprest-0.1.3 → httprest-0.3.0}/pyproject.toml +1 -1
- httprest-0.1.3/httprest/http/__init__.py +0 -17
- {httprest-0.1.3 → httprest-0.3.0}/LICENSE +0 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/__init__.py +0 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/auth.py +0 -0
- {httprest-0.1.3 → httprest-0.3.0}/httprest/http/cert.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: httprest
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
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
|
|
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.9
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.10
|
|
18
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
19
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
21
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
21
22
|
Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
22
23
|
Classifier: Topic :: Software Development :: Libraries
|
|
@@ -57,7 +58,7 @@ The library exposes an `HTTPClient` interface and provides two implementations f
|
|
|
57
58
|
1. `http.urllib_client.UrllibHTTPClient`: the default implementation, uses the `urllib` library under the hood
|
|
58
59
|
2. `http.requests_client.RequestsHTTPClient`: uses the `requests` library under the hood
|
|
59
60
|
|
|
60
|
-
|
|
61
|
+
### Custom HTTP client
|
|
61
62
|
```python
|
|
62
63
|
from httprest.http import HTTPClient, HTTPResponse
|
|
63
64
|
|
|
@@ -29,7 +29,7 @@ The library exposes an `HTTPClient` interface and provides two implementations f
|
|
|
29
29
|
1. `http.urllib_client.UrllibHTTPClient`: the default implementation, uses the `urllib` library under the hood
|
|
30
30
|
2. `http.requests_client.RequestsHTTPClient`: uses the `requests` library under the hood
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
### Custom HTTP client
|
|
33
33
|
```python
|
|
34
34
|
from httprest.http import HTTPClient, HTTPResponse
|
|
35
35
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"""Base client."""
|
|
2
2
|
|
|
3
|
-
import json as jsonlib
|
|
4
3
|
import logging
|
|
5
4
|
from abc import ABC, abstractmethod
|
|
6
5
|
from typing import Optional
|
|
@@ -8,74 +7,15 @@ from urllib.parse import urlencode
|
|
|
8
7
|
|
|
9
8
|
from .auth import BaseAuth
|
|
10
9
|
from .cert import ClientCertificate
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
class HTTPRequestError(Exception):
|
|
14
|
-
"""Base HTTP request error."""
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
class HTTPConnectionError(HTTPRequestError):
|
|
18
|
-
"""Any error related to connection."""
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class HTTPTimeoutError(HTTPRequestError):
|
|
22
|
-
"""HTTP request timed out."""
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
class HTTPInvalidResponseError(HTTPRequestError):
|
|
26
|
-
"""HTTP response is invalid."""
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
class HTTPResponse:
|
|
30
|
-
"""HTTP response wrapper."""
|
|
31
|
-
|
|
32
|
-
def __init__(
|
|
33
|
-
self, status_code: int, body: Optional[bytes], headers: dict
|
|
34
|
-
) -> None:
|
|
35
|
-
self.status_code = status_code
|
|
36
|
-
self.body = body or b""
|
|
37
|
-
self._headers = headers
|
|
38
|
-
self._json = None
|
|
39
|
-
|
|
40
|
-
def ok(self) -> bool:
|
|
41
|
-
"""Return whether the response is successful."""
|
|
42
|
-
return self.status_code >= 200 and self.status_code < 400
|
|
43
|
-
|
|
44
|
-
@property
|
|
45
|
-
def json(self) -> Optional[dict]:
|
|
46
|
-
"""Return body as JSON."""
|
|
47
|
-
if self._json is not None:
|
|
48
|
-
return self._json
|
|
49
|
-
|
|
50
|
-
headers = {key.lower(): val for key, val in self._headers.items()}
|
|
51
|
-
if "application/json" in headers.get("content-type", ""):
|
|
52
|
-
try:
|
|
53
|
-
self._json = jsonlib.loads(self.body)
|
|
54
|
-
except Exception as exc:
|
|
55
|
-
raise HTTPInvalidResponseError(
|
|
56
|
-
f"Invalid JSON in response: {exc}"
|
|
57
|
-
) from exc
|
|
58
|
-
|
|
59
|
-
return self._json
|
|
60
|
-
|
|
61
|
-
def __str__(self) -> str:
|
|
62
|
-
return f"{self.__class__.__name__}(status={self.status_code})"
|
|
63
|
-
|
|
64
|
-
def __repr__(self) -> str:
|
|
65
|
-
return (
|
|
66
|
-
f"{self.__class__.__name__}("
|
|
67
|
-
f"status={self.status_code}, "
|
|
68
|
-
f"body={self.body!r}, "
|
|
69
|
-
f"headers={self._headers}"
|
|
70
|
-
")"
|
|
71
|
-
)
|
|
10
|
+
from .response import HTTPResponse
|
|
11
|
+
from .timeout import Timeout
|
|
72
12
|
|
|
73
13
|
|
|
74
14
|
class HTTPClient(ABC):
|
|
75
15
|
"""Base HTTP client."""
|
|
76
16
|
|
|
77
|
-
def __init__(self,
|
|
78
|
-
self.
|
|
17
|
+
def __init__(self, timeout: Optional[Timeout] = None) -> None:
|
|
18
|
+
self._timeout = timeout
|
|
79
19
|
|
|
80
20
|
@abstractmethod
|
|
81
21
|
def _request(
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""HTTP errors."""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class HTTPRequestError(Exception):
|
|
5
|
+
"""Base HTTP request error."""
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HTTPConnectionError(HTTPRequestError):
|
|
9
|
+
"""Any error related to connection."""
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class HTTPTimeoutError(HTTPRequestError):
|
|
13
|
+
"""HTTP request timed out."""
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class HTTPInvalidResponseError(HTTPRequestError):
|
|
17
|
+
"""HTTP response is invalid."""
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class HTTPError(HTTPRequestError):
|
|
21
|
+
"""HTTP error based on the status code."""
|
|
@@ -4,21 +4,18 @@ from typing import Optional
|
|
|
4
4
|
|
|
5
5
|
import requests
|
|
6
6
|
|
|
7
|
-
from .
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
HTTPRequestError,
|
|
11
|
-
HTTPResponse,
|
|
12
|
-
HTTPTimeoutError,
|
|
13
|
-
)
|
|
7
|
+
from httprest.http import errors as _errors
|
|
8
|
+
|
|
9
|
+
from .base import HTTPClient, HTTPResponse
|
|
14
10
|
from .cert import ClientCertificate
|
|
11
|
+
from .timeout import Timeout
|
|
15
12
|
|
|
16
13
|
|
|
17
14
|
class RequestsHTTPClient(HTTPClient):
|
|
18
15
|
"""`requests` HTTP client."""
|
|
19
16
|
|
|
20
|
-
def __init__(self,
|
|
21
|
-
super().__init__(
|
|
17
|
+
def __init__(self, timeout: Optional[Timeout] = None) -> None:
|
|
18
|
+
super().__init__(timeout)
|
|
22
19
|
self._session = requests.Session()
|
|
23
20
|
|
|
24
21
|
def _request(
|
|
@@ -43,16 +40,20 @@ class RequestsHTTPClient(HTTPClient):
|
|
|
43
40
|
)(
|
|
44
41
|
url,
|
|
45
42
|
json=json,
|
|
46
|
-
timeout=
|
|
43
|
+
timeout=(
|
|
44
|
+
(self._timeout.connect, self._timeout.read)
|
|
45
|
+
if self._timeout
|
|
46
|
+
else None
|
|
47
|
+
),
|
|
47
48
|
headers=headers,
|
|
48
49
|
cert=client_cert,
|
|
49
50
|
)
|
|
50
51
|
except ConnectionError as exc:
|
|
51
|
-
raise HTTPConnectionError(exc) from exc
|
|
52
|
+
raise _errors.HTTPConnectionError(exc) from exc
|
|
52
53
|
except requests.Timeout as exc:
|
|
53
|
-
raise HTTPTimeoutError(exc) from exc
|
|
54
|
+
raise _errors.HTTPTimeoutError(exc) from exc
|
|
54
55
|
except requests.RequestException as exc:
|
|
55
|
-
raise HTTPRequestError(exc) from exc
|
|
56
|
+
raise _errors.HTTPRequestError(exc) from exc
|
|
56
57
|
|
|
57
58
|
return HTTPResponse(
|
|
58
59
|
response.status_code, response.content, dict(response.headers)
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"""HTTP response."""
|
|
2
|
+
|
|
3
|
+
import json as jsonlib
|
|
4
|
+
from typing import Optional
|
|
5
|
+
|
|
6
|
+
from .errors import HTTPError, HTTPInvalidResponseError
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HTTPResponse:
|
|
10
|
+
"""HTTP response wrapper."""
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self, status_code: int, body: Optional[bytes], headers: dict
|
|
14
|
+
) -> None:
|
|
15
|
+
self.status_code = status_code
|
|
16
|
+
self.body = body or b""
|
|
17
|
+
self._headers = headers
|
|
18
|
+
self._json = None
|
|
19
|
+
|
|
20
|
+
@property
|
|
21
|
+
def ok(self) -> bool:
|
|
22
|
+
"""Return whether the response is successful."""
|
|
23
|
+
return self.status_code >= 200 and self.status_code < 400
|
|
24
|
+
|
|
25
|
+
def raise_for_status(self) -> None:
|
|
26
|
+
"""Raise exception if the response is not successful."""
|
|
27
|
+
if 400 <= self.status_code < 500:
|
|
28
|
+
raise HTTPError(f"Client error {self.status_code}")
|
|
29
|
+
if 500 <= self.status_code < 600:
|
|
30
|
+
raise HTTPError(f"Server error {self.status_code}")
|
|
31
|
+
|
|
32
|
+
@property
|
|
33
|
+
def json(self) -> Optional[dict]:
|
|
34
|
+
"""Return body as JSON."""
|
|
35
|
+
if self._json is not None:
|
|
36
|
+
return self._json
|
|
37
|
+
|
|
38
|
+
headers = {key.lower(): val for key, val in self._headers.items()}
|
|
39
|
+
if "application/json" in headers.get("content-type", ""):
|
|
40
|
+
try:
|
|
41
|
+
self._json = jsonlib.loads(self.body)
|
|
42
|
+
except Exception as exc:
|
|
43
|
+
raise HTTPInvalidResponseError(
|
|
44
|
+
f"Invalid JSON in response: {exc}"
|
|
45
|
+
) from exc
|
|
46
|
+
|
|
47
|
+
return self._json
|
|
48
|
+
|
|
49
|
+
def __str__(self) -> str:
|
|
50
|
+
return f"{self.__class__.__name__}(status={self.status_code})"
|
|
51
|
+
|
|
52
|
+
def __repr__(self) -> str:
|
|
53
|
+
return (
|
|
54
|
+
f"{self.__class__.__name__}("
|
|
55
|
+
f"status={self.status_code}, "
|
|
56
|
+
f"body={self.body!r}, "
|
|
57
|
+
f"headers={self._headers}"
|
|
58
|
+
")"
|
|
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
|
|
@@ -7,13 +7,9 @@ import urllib.parse
|
|
|
7
7
|
import urllib.request
|
|
8
8
|
from typing import Optional
|
|
9
9
|
|
|
10
|
-
from .
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
HTTPRequestError,
|
|
14
|
-
HTTPResponse,
|
|
15
|
-
HTTPTimeoutError,
|
|
16
|
-
)
|
|
10
|
+
from httprest.http import errors as _errors
|
|
11
|
+
|
|
12
|
+
from .base import HTTPClient, HTTPResponse
|
|
17
13
|
from .cert import ClientCertificate
|
|
18
14
|
|
|
19
15
|
|
|
@@ -44,21 +40,21 @@ class UrllibHTTPClient(HTTPClient):
|
|
|
44
40
|
headers=headers,
|
|
45
41
|
method=method.upper(),
|
|
46
42
|
),
|
|
47
|
-
timeout=self.
|
|
43
|
+
timeout=self._timeout.read if self._timeout else None,
|
|
48
44
|
) as response:
|
|
49
45
|
return HTTPResponse(
|
|
50
46
|
response.status, response.read(), dict(response.headers)
|
|
51
47
|
)
|
|
52
48
|
except ConnectionError as exc:
|
|
53
|
-
raise HTTPConnectionError(exc) from exc
|
|
49
|
+
raise _errors.HTTPConnectionError(exc) from exc
|
|
54
50
|
except TimeoutError as exc:
|
|
55
|
-
raise HTTPTimeoutError(exc) from exc
|
|
51
|
+
raise _errors.HTTPTimeoutError(exc) from exc
|
|
56
52
|
except urllib.error.HTTPError as exc:
|
|
57
53
|
return HTTPResponse(
|
|
58
54
|
exc.status or 500, exc.read(), dict(exc.headers)
|
|
59
55
|
)
|
|
60
56
|
except urllib.error.URLError as exc:
|
|
61
|
-
raise HTTPRequestError(exc) from exc
|
|
57
|
+
raise _errors.HTTPRequestError(exc) from exc
|
|
62
58
|
|
|
63
59
|
def __str__(self) -> str:
|
|
64
60
|
return self.__class__.__name__
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
"""Package for dealing with HTTP."""
|
|
2
|
-
|
|
3
|
-
from .base import (
|
|
4
|
-
HTTPClient,
|
|
5
|
-
HTTPConnectionError,
|
|
6
|
-
HTTPRequestError,
|
|
7
|
-
HTTPResponse,
|
|
8
|
-
HTTPTimeoutError,
|
|
9
|
-
)
|
|
10
|
-
|
|
11
|
-
__all__ = [
|
|
12
|
-
"HTTPClient",
|
|
13
|
-
"HTTPConnectionError",
|
|
14
|
-
"HTTPRequestError",
|
|
15
|
-
"HTTPResponse",
|
|
16
|
-
"HTTPTimeoutError",
|
|
17
|
-
]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|