|
1 | 1 | import operator |
2 | | -from collections import defaultdict |
3 | 2 | from logging import Logger, getLogger |
4 | 3 | from pathlib import Path |
5 | | -from typing import Any, TypeVar |
| 4 | +from typing import Any, TypeVar, get_origin |
6 | 5 |
|
7 | 6 | import requests |
8 | 7 | from cachetools import TTLCache, cachedmethod |
|
17 | 16 | T = TypeVar("T", str, bytes, dict[Any, Any]) |
18 | 17 |
|
19 | 18 |
|
20 | | -return_type_to_mime_type: dict[type, ValidAcceptHeaders] = defaultdict( |
21 | | - lambda: ValidAcceptHeaders.PLAIN_TEXT, |
22 | | - { |
23 | | - dict[Any, Any]: ValidAcceptHeaders.JSON, |
24 | | - str: ValidAcceptHeaders.PLAIN_TEXT, |
25 | | - bytes: ValidAcceptHeaders.RAW_BYTES, |
26 | | - }, |
27 | | -) |
| 19 | +class TypeConversionException(Exception): ... |
28 | 20 |
|
29 | 21 |
|
30 | | -class TypeConversionException(Exception): ... |
| 22 | +def _get_mime_type(requested_return_type: type[T]) -> ValidAcceptHeaders: |
| 23 | + # Get correct mapping for typed dict or plain dict |
| 24 | + if get_origin(requested_return_type) is dict or requested_return_type is dict: |
| 25 | + return ValidAcceptHeaders.JSON |
| 26 | + elif requested_return_type is bytes: |
| 27 | + return ValidAcceptHeaders.RAW_BYTES |
| 28 | + return ValidAcceptHeaders.PLAIN_TEXT |
31 | 29 |
|
32 | 30 |
|
33 | 31 | class ConfigServer: |
@@ -143,22 +141,24 @@ def get_file_contents( |
143 | 141 | Get contents of a file from the config server in the format specified. |
144 | 142 | Optionally look for cached result before making request. |
145 | 143 |
|
146 | | - Current supported return types are: str, bytes, dict[str, str]. This option will |
147 | | - determine how the server attempts to decode the file |
| 144 | + Current supported return types are: str, bytes, dict. This option will |
| 145 | + determine how the server attempts to decode the file. Note that only untyped |
| 146 | + dictionaries are currently supported |
148 | 147 |
|
149 | 148 | Args: |
150 | 149 | file_path: Path to the file. |
151 | | - requested_response_format: Specify how to parse the response. |
152 | | - desired_return_type: If true, make a request and store response in cache, |
| 150 | + desired_return_type: Specify how to parse the response. |
| 151 | + reset_cached_result: If true, make a request and store response in cache, |
153 | 152 | otherwise look for cached response before making |
154 | 153 | new request |
155 | 154 | Returns: |
156 | 155 | The file contents, in the format specified. |
157 | 156 | """ |
158 | 157 | file_path = Path(file_path) |
159 | | - accept_header = return_type_to_mime_type[desired_return_type] |
160 | 158 |
|
161 | | - return TypeAdapter(desired_return_type).validate_python( # type: ignore - to allow any dict |
| 159 | + accept_header = _get_mime_type(desired_return_type) |
| 160 | + |
| 161 | + return TypeAdapter(desired_return_type).validate_python( |
162 | 162 | self._get( |
163 | 163 | ENDPOINTS.CONFIG, |
164 | 164 | accept_header, |
|
0 commit comments