Skip to content

Commit

Permalink
Allow custom api endpoints to be overwritten
Browse files Browse the repository at this point in the history
  • Loading branch information
madhavajay committed Feb 26, 2024
1 parent abc7f09 commit 2b3d48f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/syft/src/syft/service/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ class CustomAPIEndpoint(SyftObject):
__attr_searchable__ = ["path"]
__attr_unique__ = ["path"]

def __eq__(self, other: Any) -> bool:
if isinstance(other, CustomAPIEndpoint):
return (
self.path == other.path
and self.api_code == other.api_code
and self.signature == other.signature
and self.func_name == other.func_name
)
return self == other

def exec(self, context: AuthedServiceContext, **kwargs: Any) -> Any:
try:
inner_function = ast.parse(self.api_code).body[0]
Expand All @@ -43,7 +53,7 @@ def exec(self, context: AuthedServiceContext, **kwargs: Any) -> Any:
return context, result
except Exception as e:
print(f"Failed to run CustomAPIEndpoint Code. {e}")
return SyftError(e)
return SyftError(message=e)


def get_signature(func: Callable) -> Signature:
Expand Down
10 changes: 10 additions & 0 deletions packages/syft/src/syft/service/api/api_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ def update(
res = self.check_type(endpoint, CustomAPIEndpoint)
if res.is_err():
return res
old_endpoint = self.get_by_path(credentials=credentials, path=endpoint.path)
if old_endpoint and old_endpoint.ok():
old_endpoint = old_endpoint.ok()
old_endpoint = old_endpoint[0]

if old_endpoint == endpoint:
return Ok(endpoint)
else:
super().delete_by_uid(credentials=credentials, uid=old_endpoint.id)

result = super().set(
credentials=credentials, obj=res.ok(), ignore_duplicates=True
)
Expand Down

0 comments on commit 2b3d48f

Please sign in to comment.