def requests_response(content, status_code=200, headers={}): resp = RequestsResponse() headers = CaseInsensitiveDict(dict(headers or {})) if isinstance(content, dict): content = json.dumps(content) if not headers.get(HEADER_CONTENT_TYPE): headers[HEADER_CONTENT_TYPE] = APPLICATION_JSON resp._content = content resp.status_code = int(status_code) # Note: update headers (instead of assigning directly), to ensure we're using a case-insensitive dict resp.headers.update(headers) return resp
def parse_headers(headers: CaseInsensitiveDict, key: str) -> Optional[dict]: """ Get rate limit information from header for key. :param headers: Response headers :param key: rate limit key :return: """ usage = headers.get(key) if usage: try: data = json.loads(usage) return data except JSONDecodeError as ex: logger.error( f"Exception in parse {key} data error. Usage is: {usage}. errors: {ex}" ) return None return None