Skip to content

Commit

Permalink
Merge pull request #69 from xplpc/refact-async-code-and-tests
Browse files Browse the repository at this point in the history
refactoring async code, docs and tests
  • Loading branch information
paulocoutinhox committed Jan 2, 2024
2 parents e8b79bd + 33ed489 commit b2be254
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 44 deletions.
22 changes: 2 additions & 20 deletions docs/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Execute on terminal:

python3 xplpc.py python-format

## Synchronous and Asynchronous call
## Syntax sugar

You can use synchronous call:

Expand Down Expand Up @@ -104,25 +104,7 @@ request = Request(
],
)

response = await Client.async_call(request)
print(response)
```

## Syntax sugar

You can use `async_call` to execute the method as async function, example:

```python
request = Request(
"sample.login",
[
Param("username", "paulo"),
Param("password", "123456"),
Param("remember", True),
],
)

response = await Client.async_call(request)
response = await Client.call_async(request)
print(response)
```

Expand Down
6 changes: 3 additions & 3 deletions flutter/plugin/lib/client/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class Client {
);
});
} catch (e) {
Log.e("[Client : call] Error: $e");
Log.e("[Client : callFromString] Error: $e");
callback?.call("");
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ class Client {
return completer.future;
}

static Future<String> callFromStringAsync(
static Future<String> callAsyncFromString(
String requestData,
) async {
Completer<String> completer = Completer();
Expand All @@ -128,7 +128,7 @@ class Client {
nativeData.length,
);
} catch (e) {
Log.e("[Client : callFromStringAsync] Error: $e");
Log.e("[Client : callAsyncFromString] Error: $e");

if (!completer.isCompleted) {
completer.completeError(e);
Expand Down
2 changes: 1 addition & 1 deletion flutter/plugin/test/client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ void main() {
],
);

String response = await Client.callFromStringAsync(request.data());
String response = await Client.callAsyncFromString(request.data());
expect("{\"r\":\"100%\"}", response);
});
});
Expand Down
2 changes: 1 addition & 1 deletion python/lib/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include src/xplpc/lib/**/*
include src/xplpc/**/*
20 changes: 10 additions & 10 deletions python/lib/src/xplpc/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def callback(response):
)
)
except Exception as e:
log.error(f"[Client : call] Error: {e}")
log.error(f"[Client : SyncCall] Error: {e}")

CallbackList().add(self.key, callback)
PlatformProxy().native_call_proxy(self.key, self.request.data())
Expand All @@ -50,7 +50,7 @@ def callback(response):
try:
self.response_data = response
except Exception as e:
log.error(f"[Client : call_from_string] Error: {e}")
log.error(f"[Client : SyncCallFromString] Error: {e}")

CallbackList().add(self.key, callback)
PlatformProxy().native_call_proxy(self.key, self.request_data)
Expand Down Expand Up @@ -80,7 +80,7 @@ def callback(response):
self.future.set_result, self.response
)
except Exception as e:
log.error(f"[Client : async_call] Error: {e}")
log.error(f"[Client : AsyncCall] Error: {e}")
if not self.future.done():
self.loop.call_soon_threadsafe(self.future.set_exception, e)

Expand All @@ -93,7 +93,7 @@ def callback(response):
self.request.data(),
)
except Exception as e:
log.error(f"[Client : async_call] Error: {e}")
log.error(f"[Client : AsyncCall] Error: {e}")
if not self.future.done():
self.loop.call_soon_threadsafe(self.future.set_exception, e)

Expand Down Expand Up @@ -121,7 +121,7 @@ def callback(response):
self.future.set_result, self.response
)
except Exception as e:
log.error(f"[Client : async_call_from_string] Error: {e}")
log.error(f"[Client : AsyncCallFromString] Error: {e}")
if not self.future.done():
self.loop.call_soon_threadsafe(self.future.set_exception, e)

Expand All @@ -134,7 +134,7 @@ def callback(response):
self.request_data,
)
except Exception as e:
log.error(f"[Client : async_call_from_string] Error: {e}")
log.error(f"[Client : AsyncCallFromString] Error: {e}")
if not self.future.done():
self.loop.call_soon_threadsafe(self.future.set_exception, e)

Expand Down Expand Up @@ -162,7 +162,7 @@ def internal_callback(response):
if self.callback:
self.callback(result)
except Exception as e:
log.error(f"[Client : thread_call] Error: {e}")
log.error(f"[Client : ThreadCall] Error: {e}")
if self.callback:
self.callback(None, e)

Expand All @@ -182,7 +182,7 @@ def internal_callback(response):
if self.callback:
self.callback(response)
except Exception as e:
log.error(f"[Client : thread_call_from_string] Error: {e}")
log.error(f"[Client : ThreadCallFromString] Error: {e}")
if self.callback:
self.callback(None, e)

Expand All @@ -198,13 +198,13 @@ def call_from_string(request_data: str):
return Client.SyncCallFromString(request_data).run()

@staticmethod
async def async_call(request: Request, class_type=None, loop=None):
async def call_async(request: Request, class_type=None, loop=None):
async_call_instance = Client.AsyncCall(request, class_type, loop)
async with async_call_instance as response:
return response

@staticmethod
async def async_call_from_string(request_data: str, loop=None):
async def call_async_from_string(request_data: str, loop=None):
async_call_instance = Client.AsyncCallFromString(request_data, loop)
async with async_call_instance as response:
return response
Expand Down
4 changes: 2 additions & 2 deletions python/sample/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ async def do_battery_click_action(self):
Param("suffix", "%"),
],
)
response = await Client.async_call(request)
response = await Client.call_async(request)
self.battery_response.text = f"Response: {response}"

def on_login_button_clicked(self, instance):
Expand All @@ -215,7 +215,7 @@ async def do_login_click_action(self):
Param("remember", remember),
],
)
response = await Client.async_call(request)
response = await Client.call_async(request)
self.login_response.text = f"Response: {response}"


Expand Down
14 changes: 7 additions & 7 deletions python/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ async def test_battery_level_async():
],
)

response = await Client.async_call(request)
response = await Client.call_async(request)
assert response == "100%"


Expand Down Expand Up @@ -145,7 +145,7 @@ async def test_login_async():
],
)

response = await Client.async_call(request)
response = await Client.call_async(request)
assert response == "LOGGED-WITH-REMEMBER"


Expand Down Expand Up @@ -184,7 +184,7 @@ async def test_reverse_async():

request = Request("sample.reverse")

response = await Client.async_call(request)
response = await Client.call_async(request)
assert response == "response-is-ok"


Expand Down Expand Up @@ -271,7 +271,7 @@ async def test_grayscale_image_with_dataView_async():
],
)

response = await Client.async_call(request)
response = await Client.call_async(request)
assert response == "OK"

data = ByteArrayHelper.create_from_data_view(data_view)
Expand Down Expand Up @@ -334,7 +334,7 @@ def test_data_view():
async def test_data_view_async():
# get data view
request = Request("sample.dataview")
data_view = await Client.async_call(request, DataView)
data_view = await Client.call_async(request, DataView)

# check that data view is not None
assert data_view is not None
Expand All @@ -358,7 +358,7 @@ async def test_data_view_async():
],
)

response2 = await Client.async_call(request2)
response2 = await Client.call_async(request2)
assert response2 == "OK"

processed_data = ByteArrayHelper.create_from_data_view(data_view2)
Expand Down Expand Up @@ -410,6 +410,6 @@ async def test_battery_level_from_string_async():
],
)

response = await Client.async_call_from_string(request.data())
response = await Client.call_async_from_string(request.data())

assert '{"r": "100%"}' == response

0 comments on commit b2be254

Please sign in to comment.