-
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.
Merge branch '2.0' into chore/lock-safety
- Loading branch information
Showing
17 changed files
with
457 additions
and
326 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
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
Oops, something went wrong.