Skip to content

Commit

Permalink
Restore case conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
oeway committed Aug 16, 2024
1 parent 89395c4 commit d4c7ba4
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 12 deletions.
4 changes: 2 additions & 2 deletions javascript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion javascript/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hypha-rpc",
"version": "0.20.21-post1",
"version": "0.20.22",
"description": "Hypha RPC client for connecting to Hypha server for data management and AI model serving.",
"main": "index.js",
"types": "index.d.ts",
Expand Down
2 changes: 0 additions & 2 deletions javascript/src/websocket-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ export async function login(config) {

async function webrtcGetService(wm, rtc_service_id, query, config) {
config = config || {};
config.case_conversion = config.case_conversion || "camel";
const webrtc = config.webrtc;
const webrtc_config = config.webrtc_config;
if (config.webrtc !== undefined) delete config.webrtc;
Expand Down Expand Up @@ -620,7 +619,6 @@ export async function connectToServer(config) {
const _getService = wm.getService;
wm.getService = (query, config) => {
config = config || {};
config.case_conversion = config.case_conversion || "camel";
return _getService(query, config);
};
wm.getService.__schema__ = _getService.__schema__;
Expand Down
8 changes: 8 additions & 0 deletions javascript/tests/websocket_client_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ describe("RPC", async () => {
expect(await svc.echo("hello")).to.equal("hello");
// the function will be converted to camel case
expect(await svc.myFunc(2, 3)).to.equal(5);

const svc2 = await server.getService("echo-service-rtc");
expect(await svc2.my_func(2, 3)).to.equal(5);

const svc3 = await server.getService("echo-service-rtc", {
case_conversion: "snake",
});
expect(await svc3.my_func(2, 3)).to.equal(5);
}).timeout(20000);

it("should login to the server", async () => {
Expand Down
2 changes: 1 addition & 1 deletion python/hypha_rpc/VERSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "0.20.21.post3"
"version": "0.20.22"
}
2 changes: 1 addition & 1 deletion python/hypha_rpc/rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ async def get_remote_service(self, service_uri=None, config=None):
"""Get a remote service."""
config = config or {}
timeout = config.get("timeout", self._method_timeout)
case_conversion = config.get("case_conversion", None)
case_conversion = config.get("case_conversion")
if service_uri is None and self._connection.manager_id:
service_uri = "*/" + self._connection.manager_id
elif ":" not in service_uri:
Expand Down
2 changes: 1 addition & 1 deletion python/hypha_rpc/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def generate_password(length=50):
def to_camel_case(snake_str):
# Check if the string is already in camelCase
if "_" not in snake_str:
return snake_str
return snake_str[0].lower() + snake_str[1:]
# Convert from snake_case to camelCase
components = snake_str.split("_")
return components[0] + "".join(x.title() for x in components[1:])
Expand Down
2 changes: 0 additions & 2 deletions python/hypha_rpc/websocket_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,6 @@ def connect_to_server(config):

async def webrtc_get_service(wm, rtc_service_id, query, config=None):
config = config or {}
config["case_conversion"] = config.get("case_conversion", "snake")
webrtc = config.get("webrtc")
webrtc_config = config.get("webrtc_config")
if "webrtc" in config:
Expand Down Expand Up @@ -673,7 +672,6 @@ async def handle_disconnect(message):

def get_service(query, config=None):
config = config or {}
config["case_conversion"] = config.get("case_conversion", "snake")
return _get_service(query, config)

get_service.__schema__ = wm.get_service.__schema__
Expand Down
2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "hypha_rpc"
version = "0.20.21.post3"
version = "0.20.22"
description = "Hypha RPC client for connecting to Hypha server for data management and AI model serving"
readme = "README.md"
requires-python = ">=3.6"
Expand Down
36 changes: 35 additions & 1 deletion python/tests/test_websocket_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,40 @@ def hello(name, key=12, context=None):
assert svc.hello.__name__ == hello.__name__


@pytest.mark.asyncio
async def test_case_conversion(websocket_server):
"""Test case conversion."""
ws = await connect_to_server({"name": "my plugin", "server_url": WS_SERVER_URL})
await ws.export(ImJoyPlugin(ws))

def hello(name, key=12, context=None):
"""Say hello."""
print("Hello " + name)
return "Hello " + name

info = await ws.register_service(
{
"name": "Hello World",
"id": "hello-world",
"description": "hello world service",
"config": {
"visibility": "protected",
"run_in_executor": True,
},
"HelloWorld": hello,
}
)

svc = await ws.get_service(info.id)
assert await svc.HelloWorld("world") == "Hello world"

svc = await ws.get_service(info.id, {"case_conversion": "camel"})
assert await svc.helloWorld("world") == "Hello world"

svc = await ws.get_service(info.id, {"case_conversion": "snake"})
assert await svc.hello_world("world") == "Hello world"


@pytest.mark.asyncio
async def test_reconnect_to_server(websocket_server):
"""Test reconnecting to the server."""
Expand All @@ -263,7 +297,7 @@ async def test_reconnect_to_server(websocket_server):
}
)
# simulate abnormal close
await ws.rpc._connection._websocket.close(1010)
await ws.rpc._connection._websocket.close(1002)
# will trigger reconnect
svc = await ws.get_service("hello-world")
assert await svc.hello("world") == "hello world"
Expand Down

0 comments on commit d4c7ba4

Please sign in to comment.