Skip to content

Commit e4f0d3f

Browse files
authored
Merge pull request #129 from cryoem-uoft/fiona/expose-encoders
feat(api): Expose JSON encoders
2 parents 749dd5b + 14a2d72 commit e4f0d3f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

cryosparc/api.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import warnings
55
from contextlib import contextmanager
66
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
88

99
import httpx
1010

@@ -36,6 +36,9 @@ class APINamespace:
3636
methods under http://master:39004/pipelines/
3737
"""
3838

39+
_json_encoder: ClassVar[Callable] = api_encode
40+
_json_decoder: ClassVar[Callable] = api_default
41+
3942
_client: httpx.Client
4043

4144
def __init__(self, http_client: httpx.Client):
@@ -74,14 +77,14 @@ def _construct_request(self, _path: str, _schema, *args, **kwargs) -> Tuple[str,
7477
_path = _path.replace("{%s}" % param_name, _uriencode(param))
7578
elif param_in == "query" and param_name in kwargs and (value := kwargs.pop(param_name)) is not None:
7679
# query param must be in kwargs
77-
query_params[param_name] = api_encode(value)
80+
query_params[param_name] = APINamespace._json_encoder(value)
7881
elif (
7982
param_in == "header"
8083
and (header_name := param_name.replace("-", "_")) in kwargs
8184
and (value := kwargs.pop(header_name)) is not None
8285
):
8386
# header must be in kwargs
84-
headers[param_name] = api_encode(value)
87+
headers[param_name] = APINamespace._json_encoder(value)
8588
elif param_in == "header" and param_name in client_headers:
8689
pass # in default headers, no action required
8790
elif param_schema["required"]:
@@ -110,10 +113,10 @@ def _construct_request(self, _path: str, _schema, *args, **kwargs) -> Tuple[str,
110113
headers["Content-Type"] = "application/json"
111114
if args:
112115
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)
114117
elif body_name in kwargs:
115118
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)
117120
elif content_schema.get("required", False):
118121
raise TypeError(f"[API] {func_name}() missing required argument: {body_name}")
119122
elif "application/x-www-form-urlencoded" in content_schema:

0 commit comments

Comments
 (0)