|
1 | 1 | import operator |
2 | 2 | from logging import Logger, getLogger |
3 | 3 | from pathlib import Path |
4 | | -from typing import Any, TypeVar, get_origin |
| 4 | +from typing import Any, TypeVar, get_origin, overload |
5 | 5 |
|
6 | 6 | import requests |
7 | 7 | from cachetools import TTLCache, cachedmethod |
|
13 | 13 |
|
14 | 14 | from .constants import ENDPOINTS |
15 | 15 |
|
16 | | -T = TypeVar("T", str, bytes, dict[Any, Any], BaseModel) |
| 16 | +T = TypeVar("T", bound=BaseModel) |
17 | 17 |
|
18 | 18 |
|
19 | 19 | class TypeConversionException(Exception): ... |
20 | 20 |
|
21 | 21 |
|
22 | | -def _get_mime_type(requested_return_type: type[T]) -> ValidAcceptHeaders: |
| 22 | +def _get_mime_type( |
| 23 | + requested_return_type: type[str] |
| 24 | + | type[bytes] |
| 25 | + | type[dict[str, Any]] |
| 26 | + | type[BaseModel], |
| 27 | +) -> ValidAcceptHeaders: |
23 | 28 | # Get correct mapping for typed dict or plain dict |
24 | 29 | if ( |
25 | 30 | get_origin(requested_return_type) is dict |
@@ -135,12 +140,44 @@ def _get( |
135 | 140 |
|
136 | 141 | return content |
137 | 142 |
|
| 143 | + @overload |
138 | 144 | def get_file_contents( |
139 | 145 | self, |
140 | | - file_path: Path | str, |
141 | | - desired_return_type: type[T] = str, |
| 146 | + file_path: str | Path, |
| 147 | + desired_return_type: type[str] = str, |
142 | 148 | reset_cached_result: bool = False, |
143 | | - ) -> T: |
| 149 | + ) -> str: ... |
| 150 | + |
| 151 | + @overload |
| 152 | + def get_file_contents( |
| 153 | + self, |
| 154 | + file_path: str | Path, |
| 155 | + desired_return_type: type[bytes], |
| 156 | + reset_cached_result: bool = False, |
| 157 | + ) -> bytes: ... |
| 158 | + |
| 159 | + @overload |
| 160 | + def get_file_contents( |
| 161 | + self, |
| 162 | + file_path: str | Path, |
| 163 | + desired_return_type: type[dict[str, Any]], |
| 164 | + reset_cached_result: bool = False, |
| 165 | + ) -> dict[str, Any]: ... |
| 166 | + |
| 167 | + @overload |
| 168 | + def get_file_contents( |
| 169 | + self, |
| 170 | + file_path: str | Path, |
| 171 | + desired_return_type: type[T], |
| 172 | + reset_cached_result: bool = False, |
| 173 | + ) -> T: ... |
| 174 | + |
| 175 | + def get_file_contents( |
| 176 | + self, |
| 177 | + file_path: str | Path, |
| 178 | + desired_return_type: type[Any] = str, |
| 179 | + reset_cached_result: bool = False, |
| 180 | + ) -> Any: |
144 | 181 | """ |
145 | 182 | Get contents of a file from the config server in the format specified. |
146 | 183 | Optionally look for cached result before making request. |
|
0 commit comments