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.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import time
|
||||
from typing import Any, Dict, Optional
|
||||
|
|
@ -13,17 +14,20 @@ import urllib.parse
|
|||
from warnings import warn
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class HttpResponse:
|
||||
def __init__(self, status: int, headers: dict, data: bytes, reason: str):
|
||||
self.status_code = status
|
||||
self.headers = headers
|
||||
self.data = data
|
||||
self.reason = reason
|
||||
self.text = data.decode("utf-8", errors="replace")
|
||||
status_code: int
|
||||
headers: dict[str, str]
|
||||
data: bytes
|
||||
reason: Optional[str] = None
|
||||
|
||||
def json(self):
|
||||
return json.loads(self.data.decode("utf-8"))
|
||||
|
||||
@property
|
||||
def text(self) -> str:
|
||||
return self.data.decode("utf-8", errors="replace")
|
||||
|
||||
|
||||
def _request(
|
||||
url: str,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue