refactor: convert HttpResponse to frozen dataclass
text becomes a derived property; reason is now optional.
This commit is contained in:
parent
18aca33e42
commit
9a4d2041f9
1 changed files with 10 additions and 6 deletions
|
|
@ -5,6 +5,7 @@ Thin urllib wrapper with retry-on-rate-limit. No domain knowledge —
|
||||||
GitHub, Bitbucket, etc. are handled by higher-level modules.
|
GitHub, Bitbucket, etc. are handled by higher-level modules.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
import json
|
import json
|
||||||
import time
|
import time
|
||||||
from typing import Any, Dict, Optional
|
from typing import Any, Dict, Optional
|
||||||
|
|
@ -13,17 +14,20 @@ import urllib.parse
|
||||||
from warnings import warn
|
from warnings import warn
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
class HttpResponse:
|
class HttpResponse:
|
||||||
def __init__(self, status: int, headers: dict, data: bytes, reason: str):
|
status_code: int
|
||||||
self.status_code = status
|
headers: dict[str, str]
|
||||||
self.headers = headers
|
data: bytes
|
||||||
self.data = data
|
reason: Optional[str] = None
|
||||||
self.reason = reason
|
|
||||||
self.text = data.decode("utf-8", errors="replace")
|
|
||||||
|
|
||||||
def json(self):
|
def json(self):
|
||||||
return json.loads(self.data.decode("utf-8"))
|
return json.loads(self.data.decode("utf-8"))
|
||||||
|
|
||||||
|
@property
|
||||||
|
def text(self) -> str:
|
||||||
|
return self.data.decode("utf-8", errors="replace")
|
||||||
|
|
||||||
|
|
||||||
def _request(
|
def _request(
|
||||||
url: str,
|
url: str,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue