| 37 | |
| 38 | @dataclass |
| 39 | class HTTPSRecord: |
| 40 | priority: int |
| 41 | target_name: str |
| 42 | params: dict[int, bytes] |
| 43 | |
| 44 | def __repr__(self): |
| 45 | return str(self.to_json()) |
| 46 | |
| 47 | def to_json(self) -> HTTPSRecordJSON: |
| 48 | ret: HTTPSRecordJSON = { |
| 49 | "target_name": self.target_name, |
| 50 | "priority": self.priority, |
| 51 | } |
| 52 | typ: str | int |
| 53 | for typ, val in self.params.items(): |
| 54 | try: |
| 55 | typ = SVCParamKeys(typ).name.lower() |
| 56 | except ValueError: |
| 57 | pass |
| 58 | ret[typ] = strutils.bytes_to_escaped_str(val) |
| 59 | return ret |
| 60 | |
| 61 | @classmethod |
| 62 | def from_json(cls, data: HTTPSRecordJSON) -> Self: |
| 63 | target_name = data.pop("target_name") |
| 64 | assert isinstance(target_name, str) |
| 65 | priority = data.pop("priority") |
| 66 | assert isinstance(priority, int) |
| 67 | params: dict[int, bytes] = {} |
| 68 | for k, v in data.items(): |
| 69 | if isinstance(k, str): |
| 70 | k = SVCParamKeys[k.upper()].value |
| 71 | assert isinstance(v, str) |
| 72 | params[k] = strutils.escaped_str_to_bytes(v) |
| 73 | return cls(target_name=target_name, priority=priority, params=params) |
| 74 | |
| 75 | |
| 76 | def _unpack_params(data: bytes, offset: int) -> dict[int, bytes]: |
no outgoing calls
no test coverage detected
searching dependent graphs…