Skip to content

Commit 5553718

Browse files
committed
Add param_checker
1 parent 32a7ac9 commit 5553718

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

packages/catalystwan-core/src/catalystwan/core/request_adapter.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from copy import copy
55
from dataclasses import dataclass, field, fields, is_dataclass
66
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
88

99
from catalystwan.abc import RequestAdapterInterface, ResponseInterface, SessionInterface
1010
from catalystwan.abc.types import HTTP_METHOD, JSON
@@ -221,5 +221,37 @@ class ModelReturn:
221221
)
222222
return valid_models[0].model
223223

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+
224256
def __copy__(self) -> RequestAdapter:
225257
return RequestAdapter(session=copy(self.session), logger=self.logger)

0 commit comments

Comments
 (0)