Utility function which parses API json response using orjson. requests uses simplejson, but this version uses orjson which can be up to 50% faster and that is especially pronounced with larger response.
(response: requests.models.Response)
| 48 | |
| 49 | |
| 50 | def parse_api_response(response: requests.models.Response) -> dict: |
| 51 | """ |
| 52 | Utility function which parses API json response using orjson. |
| 53 | |
| 54 | requests uses simplejson, but this version uses orjson which can be up to 50% faster and that |
| 55 | is especially pronounced with larger response. |
| 56 | """ |
| 57 | # Upstream implementation is available at |
| 58 | # https://github.com/psf/requests/blob/master/requests/models.py#L876 |
| 59 | # NOTE: It's important that we manually decode response.content attribute before passing it to |
| 60 | # orjson otherwise orjson will try to decode it and will be slower. |
| 61 | |
| 62 | # Inside tests content is not always bytes |
| 63 | if isinstance(response.content, str): |
| 64 | data = response.content |
| 65 | else: |
| 66 | data = response.content.decode("utf-8") |
| 67 | |
| 68 | data = orjson.loads(data) |
| 69 | return data |
| 70 | |
| 71 | |
| 72 | class Resource(object): |
no outgoing calls
no test coverage detected