Skip to content

Commit

Permalink
Add api.export
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Aug 21, 2024
1 parent d7fc9ac commit 252e6cc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
25 changes: 25 additions & 0 deletions python/hypha_rpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Provide hypha-rpc to connecting to Hypha server."""

import shortuuid

from .rpc import RPC
from .utils import ObjectProxy
from .sync import connect_to_server as connect_to_server_sync
from .sync import get_remote_service as get_remote_service_sync
from .sync import get_rtc_service as get_rtc_service_sync
Expand All @@ -14,7 +17,29 @@
setup_local_client,
)


class API(ObjectProxy):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._registry = {}

def list(self):
return list(self._registry.keys())

def get(self, name):
return self._registry.get(name)

def export(self, obj, config=None):
config = config or {}
name = config.get("name", shortuuid.uuid())
self._registry[name] = obj


# An placeholder object for the API
api = API()

__all__ = [
"api",
"RPC",
"login",
"connect_to_server",
Expand Down
7 changes: 7 additions & 0 deletions python/tests/test_websocket_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,13 @@ def test_connect_to_server_sync(websocket_server):
info = login.start()
assert "key" in info

@pytest.mark.asyncio
async def test_export_api(websocket_server):
"""Test exporting API."""
from hypha_rpc import api
api.export({"hello": lambda x: "hello " + x})
api.export({"hello": lambda x: "hello " + x}, {"name": "hello2"})
assert "hello2" in api.list()

@pytest.mark.asyncio
async def test_connect_to_server(websocket_server):
Expand Down

0 comments on commit 252e6cc

Please sign in to comment.