Skip to content

Commit

Permalink
Rename AbstractResource client to __client__ #59
Browse files Browse the repository at this point in the history
  • Loading branch information
ruscoder committed Aug 3, 2024
1 parent 51c8f96 commit 91a34e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
18 changes: 10 additions & 8 deletions fhirpy/base/lib_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,16 @@ class AsyncResource(Generic[TAsyncClient], BaseResource[TAsyncClient], ABC):
async def save(
self, fields: Union[list[str], None] = None, search_params: Union[dict, None] = None
):
response_data = await self.client.save(
response_data = await self.__client__.save(
self, fields=fields, _search_params=search_params, _as_dict=True
)

if response_data:
resource_type = self.resource_type
super(BaseResource, self).clear()
super(BaseResource, self).update(**self.client.resource(resource_type, **response_data))
super(BaseResource, self).update(
**self.__client__.resource(resource_type, **response_data)
)

async def create(self, **kwargs):
await self.save(search_params=kwargs)
Expand All @@ -245,18 +247,18 @@ async def patch(self, **kwargs):
async def delete(self):
if not self.id:
raise TypeError("Resource `id` is required for delete operation")
return await self.client.delete(self)
return await self.__client__.delete(self)

async def refresh(self):
data = await self.client._do_request("get", self._get_path())
data = await self.__client__._do_request("get", self._get_path())
super(BaseResource, self).clear()
super(BaseResource, self).update(**data)

async def to_resource(self):
return super().to_resource()

async def is_valid(self, raise_exception=False):
data = await self.client._do_request(
data = await self.__client__._do_request(
"post", f"{self.resource_type}/$validate", data=self.serialize()
)
if any(issue["severity"] in ["fatal", "error"] for issue in data["issue"]):
Expand All @@ -272,7 +274,7 @@ async def execute(
data: Union[dict, None] = None,
params: Union[dict, None] = None,
) -> Any:
return await self.client._do_request(
return await self.__client__._do_request(
method,
f"{self._get_path()}/{operation}",
data=data,
Expand All @@ -288,13 +290,13 @@ async def to_resource(self):
"""
if not self.is_local:
raise ResourceNotFound("Can not resolve not local resource")
resource_data = await self.client._do_request("get", f"{self.resource_type}/{self.id}")
resource_data = await self.__client__._do_request("get", f"{self.resource_type}/{self.id}")
return self._dict_to_resource(resource_data)

async def execute(self, operation, method="post", **kwargs):
if not self.is_local:
raise ResourceNotFound("Can not execute on not local resource")
return await self.client._do_request(
return await self.__client__._do_request(
method,
f"{self.resource_type}/{self.id}/{operation}",
**kwargs,
Expand Down
20 changes: 12 additions & 8 deletions fhirpy/base/lib_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,15 @@ def _fetch_resource(self, path, params=None):

class SyncResource(Generic[TSyncClient], BaseResource[TSyncClient], ABC):
def save(self, fields: Union[list[str], None] = None, search_params: Union[dict, None] = None):
response_data = self.client.save(self, fields, _search_params=search_params, _as_dict=True)
response_data = self.__client__.save(
self, fields, _search_params=search_params, _as_dict=True
)
if response_data:
resource_type = self.resource_type
super(BaseResource, self).clear()
super(BaseResource, self).update(**self.client.resource(resource_type, **response_data))
super(BaseResource, self).update(
**self.__client__.resource(resource_type, **response_data)
)

def create(self, **kwargs):
self.save(search_params=kwargs)
Expand All @@ -235,15 +239,15 @@ def patch(self, **kwargs):
def delete(self):
if not self.id:
raise TypeError("Resource `id` is required for delete operation")
return self.client.delete(self)
return self.__client__.delete(self)

def refresh(self):
data = self.client._do_request("get", self._get_path())
data = self.__client__._do_request("get", self._get_path())
super(BaseResource, self).clear()
super(BaseResource, self).update(**data)

def is_valid(self, raise_exception=False):
data = self.client._do_request(
data = self.__client__._do_request(
"post", f"{self.resource_type}/$validate", data=self.serialize()
)
if any(issue["severity"] in ["fatal", "error"] for issue in data["issue"]):
Expand All @@ -259,7 +263,7 @@ def execute(
data: Union[dict, None] = None,
params: Union[dict, None] = None,
) -> Any:
return self.client._do_request(
return self.__client__._do_request(
method,
f"{self._get_path()}/{operation}",
data=data,
Expand All @@ -275,13 +279,13 @@ def to_resource(self):
"""
if not self.is_local:
raise ResourceNotFound("Can not resolve not local resource")
resource_data = self.client._do_request("get", f"{self.resource_type}/{self.id}")
resource_data = self.__client__._do_request("get", f"{self.resource_type}/{self.id}")
return self._dict_to_resource(resource_data)

def execute(self, operation, method="post", **kwargs):
if not self.is_local:
raise ResourceNotFound("Can not execute on not local resource")
return self.client._do_request(
return self.__client__._do_request(
method,
f"{self.resource_type}/{self.id}/{operation}",
**kwargs,
Expand Down
12 changes: 6 additions & 6 deletions fhirpy/base/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@


class AbstractResource(Generic[TClient], dict, ABC):
client: TClient
__client__: TClient

def __init__(self, client: TClient, **kwargs):
self.client = client
self.__client__ = client

super().__init__(**kwargs)

Expand All @@ -32,7 +32,7 @@ def __getattr__(self, key):
return self[key]

def __setattr__(self, key, value):
reserved_keys = ["client"]
reserved_keys = ["__client__"]
if key in reserved_keys:
super().__setattr__(key, value)
else:
Expand Down Expand Up @@ -141,7 +141,7 @@ def to_reference(self, **kwargs):
if not self.reference:
raise ResourceNotFound("Can not get reference to unsaved resource without id")

return self.client.reference(reference=self.reference, **kwargs)
return self.__client__.reference(reference=self.reference, **kwargs)

@abstractmethod
def is_reference(self, value):
Expand Down Expand Up @@ -216,10 +216,10 @@ def to_reference(self, **kwargs):
"""
Returns Reference instance for this reference
"""
return self.client.reference(reference=self.reference, **kwargs)
return self.__client__.reference(reference=self.reference, **kwargs)

def _dict_to_resource(self, data):
return self.client.resource(data["resourceType"], **data)
return self.__client__.resource(data["resourceType"], **data)

@property
@abstractmethod
Expand Down

0 comments on commit 91a34e2

Please sign in to comment.