|
4 | 4 | from copy import copy |
5 | 5 | from dataclasses import dataclass, field, fields, is_dataclass |
6 | 6 | from string import Formatter |
7 | | -from typing import Any, Dict, List, Optional, Type, TypeVar, Union, cast |
| 7 | +from typing import Any, Dict, List, Literal, Optional, Tuple, Type, TypeVar, Union, cast |
8 | 8 |
|
9 | 9 | from catalystwan.abc import RequestAdapterInterface, ResponseInterface, SessionInterface |
10 | 10 | from catalystwan.abc.types import HTTP_METHOD, JSON |
@@ -221,5 +221,37 @@ class ModelReturn: |
221 | 221 | ) |
222 | 222 | return valid_models[0].model |
223 | 223 |
|
| 224 | + def param_checker(self, required_params: List[Tuple[Any, Type]], excluded_params: List[Any]): |
| 225 | + for param in excluded_params: |
| 226 | + if param is not None: |
| 227 | + return False |
| 228 | + for param_value, expected_type in required_params: |
| 229 | + if param_value is None: |
| 230 | + return False |
| 231 | + origin = get_origin(expected_type) |
| 232 | + if origin is Any: |
| 233 | + continue |
| 234 | + elif origin is None: |
| 235 | + if type(param_value) is not expected_type: |
| 236 | + return False |
| 237 | + elif origin is Literal: |
| 238 | + if param_value not in get_args(expected_type): |
| 239 | + return False |
| 240 | + # This part assumes List and Unions are not overly complex, allowing get_args to flatten the list of types |
| 241 | + elif origin is list: |
| 242 | + if type(param_value) is not list: |
| 243 | + return False |
| 244 | + args = get_args(expected_type) |
| 245 | + if Any in args: |
| 246 | + continue |
| 247 | + if type(param_value) not in args: |
| 248 | + return False |
| 249 | + elif origin is Union: |
| 250 | + if type(param_value) not in get_args(expected_type): |
| 251 | + return False |
| 252 | + else: |
| 253 | + continue |
| 254 | + return True |
| 255 | + |
224 | 256 | def __copy__(self) -> RequestAdapter: |
225 | 257 | return RequestAdapter(session=copy(self.session), logger=self.logger) |
0 commit comments