-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Align method serialization in Python (#1824)
* Align method serialization in Python; Fix TimelockUnlockConditionDto * Fix test_output_id * Don't use dangerous default value [] * Remove early return, update comment * Update bindings/python/iota_sdk/common.py Co-authored-by: /alex/ <[email protected]> --------- Co-authored-by: /alex/ <[email protected]> Co-authored-by: Thibault Martinez <[email protected]>
- Loading branch information
1 parent
032f179
commit 9c8956a
Showing
12 changed files
with
121 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Copyright 2024 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import json | ||
from iota_sdk import call_client_method | ||
from iota_sdk.common import custom_encoder | ||
|
||
|
||
def _call_client_method_routine(func): | ||
"""The routine of dump json string and call call_client_method(). | ||
""" | ||
def wrapper(*args, **kwargs): | ||
message = custom_encoder(func, *args, **kwargs) | ||
# Send message to the Rust library | ||
response = call_client_method(args[0].handle, message) | ||
|
||
json_response = json.loads(response) | ||
|
||
if "type" in json_response: | ||
if json_response["type"] == "error" or json_response["type"] == "panic": | ||
raise ClientError(json_response['payload']) | ||
|
||
if "payload" in json_response: | ||
return json_response['payload'] | ||
return response | ||
return wrapper | ||
|
||
|
||
class ClientError(Exception): | ||
"""A client error.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2024 IOTA Stiftung | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import json | ||
from json import dumps, JSONEncoder | ||
from enum import Enum | ||
import humps | ||
|
||
|
||
def custom_encoder(func, *args, **kwargs): | ||
"""Converts the parameters to a JSON string and removes None values. | ||
""" | ||
class MyEncoder(JSONEncoder): | ||
"""Custom encoder | ||
""" | ||
# pylint: disable=too-many-return-statements | ||
|
||
def default(self, o): | ||
to_dict_method = getattr(o, "to_dict", None) | ||
if callable(to_dict_method): | ||
return o.to_dict() | ||
if isinstance(o, str): | ||
return o | ||
if isinstance(o, Enum): | ||
return o.__dict__ | ||
if isinstance(o, dict): | ||
return o | ||
if hasattr(o, "__dict__"): | ||
obj_dict = o.__dict__ | ||
items_method = getattr(self, "items", None) | ||
if callable(items_method): | ||
for k, v in obj_dict.items(): | ||
obj_dict[k] = dumps(v, cls=MyEncoder) | ||
return obj_dict | ||
return o | ||
message = func(*args, **kwargs) | ||
for k, v in message.items(): | ||
if not isinstance(v, str): | ||
message[k] = json.loads(dumps(v, cls=MyEncoder)) | ||
|
||
def remove_none(obj): | ||
if isinstance(obj, (list, tuple, set)): | ||
return type(obj)(remove_none(x) for x in obj if x is not None) | ||
if isinstance(obj, dict): | ||
return type(obj)((remove_none(k), remove_none(v)) | ||
for k, v in obj.items() if k is not None and v is not None) | ||
return obj | ||
|
||
message_null_filtered = remove_none(message) | ||
message = dumps(humps.camelize(message_null_filtered)) | ||
return message |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.