|
8 | 8 | :attribute str BASE_URL: path to the Lokalise APIv2. |
9 | 9 | :attribute list PAGINATION_HEADERS: list of response headers that contain pagination data. |
10 | 10 | """ |
11 | | -from typing import Optional, Dict |
| 11 | +from typing import Optional, Dict, Any |
12 | 12 | import requests |
13 | 13 | from lokalise.request_utils import raise_on_error, path_to_endpoint, __format_params |
14 | 14 | import lokalise.client |
@@ -124,18 +124,29 @@ def respond_with(response: requests.models.Response) -> Dict: |
124 | 124 | return {**data, **extract_headers_from(response)} |
125 | 125 |
|
126 | 126 |
|
127 | | -def extract_headers_from(response: requests.models.Response) -> Dict: |
128 | | - """Fetches pagination-related data from the response headers |
129 | | -
|
130 | | - :param response: Response from the API |
131 | | - :rtype dict: |
| 127 | +def extract_headers_from(response: requests.models.Response) -> Dict[str, Any]: |
132 | 128 | """ |
133 | | - return { |
134 | | - "_pagination": { |
135 | | - k.lower(): v for k, |
136 | | - v in response.headers.items() if k.lower() in PAGINATION_HEADERS |
137 | | - } |
| 129 | + Pull pagination metadata (and the oversized flag) out of an HTTP response. |
| 130 | +
|
| 131 | + Returns a dict like: |
| 132 | + { |
| 133 | + "_pagination": { "x-pagination-page": "3", ... }, |
| 134 | + "_response_too_big": True # only when x-response-too-big header is present |
138 | 135 | } |
| 136 | + """ |
| 137 | + |
| 138 | + headers_lower = {k.lower(): v for k, v in response.headers.items()} |
| 139 | + |
| 140 | + pagination = { |
| 141 | + k: v for k, |
| 142 | + v in headers_lower.items() if k in PAGINATION_HEADERS} |
| 143 | + |
| 144 | + result: Dict[str, Any] = {"_pagination": pagination} |
| 145 | + |
| 146 | + if "x-response-too-big" in headers_lower: |
| 147 | + result["_response_too_big"] = True |
| 148 | + |
| 149 | + return result |
139 | 150 |
|
140 | 151 |
|
141 | 152 | def options(client: lokalise.client.Client) -> Dict: |
|
0 commit comments