Skip to content

Commit

Permalink
Add pluggable client-level dump
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Aug 29, 2024
1 parent a3be32e commit 4871f2c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.8

* Add experimental pluggable client-level dump function

## 2.0.7

* Preserve null values for resource in save() with fields passed
Expand Down
2 changes: 1 addition & 1 deletion fhirpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .lib import AsyncFHIRClient, SyncFHIRClient

__title__ = "fhir-py"
__version__ = "2.0.7"
__version__ = "2.0.8"
__author__ = "beda.software"
__license__ = "None"
__copyright__ = "Copyright 2024 beda.software"
Expand Down
15 changes: 9 additions & 6 deletions fhirpy/base/lib_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import warnings
from abc import ABC
from collections.abc import AsyncGenerator
from collections.abc import AsyncGenerator, Callable
from typing import Any, Generic, Literal, TypeVar, Union, cast, overload

import aiohttp
Expand All @@ -29,8 +29,10 @@ def __init__(
authorization: Union[str, None] = None,
extra_headers: Union[dict, None] = None,
aiohttp_config: Union[dict, None] = None,
dump: Callable[[Any], Any] = lambda x: x,
):
self.aiohttp_config = aiohttp_config or {}
self.dump = dump

super().__init__(url, authorization, extra_headers)

Expand Down Expand Up @@ -109,7 +111,7 @@ async def save(
# _as_dict is a private api used internally
_as_dict: bool = False,
) -> Union[TResource, Any]:
data = serialize(resource, drop_dict_null_values=fields is None)
data = serialize(self.dump(resource), drop_dict_null_values=fields is None)
if fields:
if not resource.id:
raise TypeError("Resource `id` is required for update operation")
Expand Down Expand Up @@ -169,7 +171,7 @@ async def patch(
response_data = await self._do_request(
"patch",
f"{resource_type}/{resource_id}",
data=serialize(kwargs, drop_dict_null_values=False),
data=serialize(self.dump(kwargs), drop_dict_null_values=False),
)

if custom_resource_class:
Expand Down Expand Up @@ -442,7 +444,7 @@ async def get_or_create(self, resource: TResource) -> tuple[TResource, bool]:
response_data, status_code = await self.client._do_request(
"post",
self.resource_type,
serialize(resource),
serialize(self.client.dump(resource)),
self.params,
returning_status=True,
)
Expand All @@ -455,7 +457,7 @@ async def update(self, resource: TResource) -> tuple[TResource, bool]:
response_data, status_code = await self.client._do_request(
"put",
self.resource_type,
serialize(resource),
serialize(self.client.dump(resource)),
self.params,
returning_status=True,
)
Expand All @@ -470,7 +472,8 @@ async def patch(self, _resource: Any = None, **kwargs) -> TResource:
stacklevel=2,
)
data = serialize(
_resource if _resource is not None else kwargs, drop_dict_null_values=False
self.client.dump(_resource if _resource is not None else kwargs),
drop_dict_null_values=False,
)
response_data = await self.client._do_request(
"PATCH", self.resource_type, data, self.params
Expand Down
15 changes: 9 additions & 6 deletions fhirpy/base/lib_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import warnings
from abc import ABC
from collections.abc import Generator
from collections.abc import Callable, Generator
from typing import Any, Generic, Literal, TypeVar, Union, cast, overload

import requests
Expand All @@ -29,8 +29,10 @@ def __init__(
authorization: Union[str, None] = None,
extra_headers: Union[dict, None] = None,
requests_config: Union[dict, None] = None,
dump: Callable[[Any], Any] = lambda x: x,
):
self.requests_config = requests_config or {}
self.dump = dump

super().__init__(url, authorization, extra_headers)

Expand Down Expand Up @@ -109,7 +111,7 @@ def save(
# _as_dict is a private api used internally
_as_dict: bool = False,
) -> Union[TResource, Any]:
data = serialize(resource, drop_dict_null_values=fields is None)
data = serialize(self.dump(resource), drop_dict_null_values=fields is None)
if fields:
if not resource.id:
raise TypeError("Resource `id` is required for update operation")
Expand Down Expand Up @@ -165,7 +167,7 @@ def patch(
response_data = self._do_request(
"patch",
f"{resource_type}/{resource_id}",
data=serialize(kwargs, drop_dict_null_values=False),
data=serialize(self.dump(kwargs), drop_dict_null_values=False),
)

if custom_resource_class:
Expand Down Expand Up @@ -440,7 +442,7 @@ def get_or_create(self, resource: TResource) -> tuple[TResource, int]:
response_data, status_code = self.client._do_request(
"post",
self.resource_type,
serialize(resource),
serialize(self.client.dump(resource)),
self.params,
returning_status=True,
)
Expand All @@ -453,7 +455,7 @@ def update(self, resource: TResource) -> tuple[TResource, int]:
response_data, status_code = self.client._do_request(
"put",
self.resource_type,
serialize(resource),
serialize(self.client.dump(resource)),
self.params,
returning_status=True,
)
Expand All @@ -470,7 +472,8 @@ def patch(self, _resource: Any = None, **kwargs) -> TResource:
)

data = serialize(
_resource if _resource is not None else kwargs, drop_dict_null_values=False
self.client.dump(_resource if _resource is not None else kwargs),
drop_dict_null_values=False,
)
response_data = self.client._do_request("patch", self.resource_type, data, self.params)
return self._dict_to_resource(response_data)
Expand Down
1 change: 1 addition & 0 deletions fhirpy/base/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ def is_local(self):

def serialize(resource: Any, drop_dict_null_values=True) -> dict:
# TODO: make serialization pluggable
# TODO: add empty dict/array cleanup

def convert_fn(item):
if isinstance(item, BaseResource):
Expand Down

0 comments on commit 4871f2c

Please sign in to comment.