|
4 | 4 | import warnings |
5 | 5 | from contextlib import contextmanager |
6 | 6 | from enum import Enum |
7 | | -from typing import Any, Dict, Iterator, List, Optional, Tuple, TypedDict, Union |
| 7 | +from typing import Any, Callable, ClassVar, Dict, Iterator, List, Optional, Tuple, TypedDict, Union |
8 | 8 |
|
9 | 9 | import httpx |
10 | 10 |
|
@@ -36,6 +36,9 @@ class APINamespace: |
36 | 36 | methods under http://master:39004/pipelines/ |
37 | 37 | """ |
38 | 38 |
|
| 39 | + _json_encoder: ClassVar[Callable] = api_encode |
| 40 | + _json_decoder: ClassVar[Callable] = api_default |
| 41 | + |
39 | 42 | _client: httpx.Client |
40 | 43 |
|
41 | 44 | def __init__(self, http_client: httpx.Client): |
@@ -74,14 +77,14 @@ def _construct_request(self, _path: str, _schema, *args, **kwargs) -> Tuple[str, |
74 | 77 | _path = _path.replace("{%s}" % param_name, _uriencode(param)) |
75 | 78 | elif param_in == "query" and param_name in kwargs and (value := kwargs.pop(param_name)) is not None: |
76 | 79 | # query param must be in kwargs |
77 | | - query_params[param_name] = api_encode(value) |
| 80 | + query_params[param_name] = APINamespace._json_encoder(value) |
78 | 81 | elif ( |
79 | 82 | param_in == "header" |
80 | 83 | and (header_name := param_name.replace("-", "_")) in kwargs |
81 | 84 | and (value := kwargs.pop(header_name)) is not None |
82 | 85 | ): |
83 | 86 | # header must be in kwargs |
84 | | - headers[param_name] = api_encode(value) |
| 87 | + headers[param_name] = APINamespace._json_encoder(value) |
85 | 88 | elif param_in == "header" and param_name in client_headers: |
86 | 89 | pass # in default headers, no action required |
87 | 90 | elif param_schema["required"]: |
@@ -110,10 +113,10 @@ def _construct_request(self, _path: str, _schema, *args, **kwargs) -> Tuple[str, |
110 | 113 | headers["Content-Type"] = "application/json" |
111 | 114 | if args: |
112 | 115 | request_body, args = args[0], args[1:] |
113 | | - request_body = json.dumps(request_body, default=api_default) |
| 116 | + request_body = json.dumps(request_body, default=APINamespace._json_decoder) |
114 | 117 | elif body_name in kwargs: |
115 | 118 | request_body = kwargs.pop(body_name) |
116 | | - request_body = json.dumps(request_body, default=api_default) |
| 119 | + request_body = json.dumps(request_body, default=APINamespace._json_decoder) |
117 | 120 | elif content_schema.get("required", False): |
118 | 121 | raise TypeError(f"[API] {func_name}() missing required argument: {body_name}") |
119 | 122 | elif "application/x-www-form-urlencoded" in content_schema: |
|
0 commit comments