| 20 | |
| 21 | |
| 22 | class CacheData: |
| 23 | __slots__ = ("data",) |
| 24 | |
| 25 | def __init__(self, data: str): |
| 26 | self.data = data |
| 27 | |
| 28 | def json(self): |
| 29 | try: |
| 30 | return json.loads(self.data) |
| 31 | except json.JSONDecodeError: |
| 32 | logger.warning( |
| 33 | f"Failed to decode JSON data: {self.data[:100]}... Trying workaround" |
| 34 | ) |
| 35 | return self.workaround_decode_json() |
| 36 | |
| 37 | def workaround_decode_json(self): |
| 38 | if self.data.startswith("\\x"): |
| 39 | s = self.data[2:] |
| 40 | else: |
| 41 | s = self.data |
| 42 | |
| 43 | byte_string = codecs.decode(s, "hex") |
| 44 | json_string = byte_string.decode("utf-8") |
| 45 | |
| 46 | return json.loads(json_string) |
| 47 | |
| 48 | def __repr__(self): |
| 49 | return self.data |
| 50 | |
| 51 | def __str__(self): |
| 52 | return self.data |
| 53 | |
| 54 | def has_data(self): |
| 55 | return self.data is not None and self.data != "" |
| 56 | |
| 57 | |
| 58 | class CacheResponse: |