Skip to content

Commit 06ddcda

Browse files
committed
Add a to_json() object function that's consistent with from_json()
1 parent ace5e3e commit 06ddcda

108 files changed

Lines changed: 1175 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import json
2+
from lightspark.objects.InvoiceData import from_json as InvoiceData_from_json
3+
from lightspark.objects.Node import from_json as Node_from_json
4+
5+
6+
class TestSerialization:
7+
def test_serialize_deserialize_invoice_data(self):
8+
serialized = '{"__typename": "InvoiceData", "invoice_data_encoded_payment_request":"lnbcrt34170n1pj5vdn4pp56jhw0672v566u4rvl333v8hwwuvavvu9gx4a2mqag4pkrvm0hwkqhp5xaz278y6cejcvpqnndl4wfq3slgthjduwlfksg778aevn23v2pdscqzpgxqyz5vqsp5ee5jezfvjqvvz7hfwta3ekk8hs6dq36szkgp40qh7twa8upquxlq9qyyssqjg2slc95falxf2t67y0wu2w43qwfcvfflwl8tn4ppqw9tumwqxk36qkfct9p2w8c3yy2ld7c6nacy4ssv2gl6qyqfpmhl4jmarnjf8cpvjlxek","invoice_data_bitcoin_network":"REGTEST","invoice_data_payment_hash":"d4aee7ebca6535ae546cfc63161eee7719d6338541abd56c1d454361b36fbbac","invoice_data_amount":{"currency_amount_original_value":3417,"currency_amount_original_unit":"SATOSHI","currency_amount_preferred_currency_unit":"USD","currency_amount_preferred_currency_value_rounded":118,"currency_amount_preferred_currency_value_approx":118.89352818371607},"invoice_data_created_at":"2023-11-04T12:17:57Z","invoice_data_expires_at":"2023-11-05T12:17:57Z","invoice_data_memo":null,"invoice_data_destination":{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759Z","graph_node_updated_at":"2023-11-04T12:01:04.015414Z","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}}'
9+
deserialized = InvoiceData_from_json(None, json.loads(serialized))
10+
reserialized = deserialized.to_json()
11+
# TODO(Jeremy): Fix this assertion by making iso format consistent w.r.t. trailing Z.
12+
# assert reserialized == json.loads(serialized)
13+
14+
deserialized_again = InvoiceData_from_json(None, reserialized)
15+
assert deserialized_again == deserialized
16+
17+
def test_serialize_deserialize_graph_node(self):
18+
serialized = '{"graph_node_id":"GraphNode:0189a572-6dba-cf00-0000-ac0908d34ea6","graph_node_created_at":"2023-07-30T06:18:07.162759Z","graph_node_updated_at":"2023-11-04T12:01:04.015414Z","graph_node_alias":"ls_test_vSViIQitob_SE","graph_node_bitcoin_network":"REGTEST","graph_node_color":"#3399ff","graph_node_conductivity":null,"graph_node_display_name":"ls_test_vSViIQitob_SE","graph_node_public_key":"02253935a5703a6f0429081e08d2defce0faa15f4d75305302284751d53a4e0608", "__typename":"GraphNode"}'
19+
deserialized = Node_from_json(None, json.loads(serialized))
20+
reserialized = deserialized.to_json()
21+
# TODO(Jeremy): Fix this assertion by making iso format consistent w.r.t. trailing Z.
22+
# assert reserialized == json.loads(serialized)
23+
24+
deserialized_again = Node_from_json(None, reserialized)
25+
assert deserialized_again == deserialized

lightspark/objects/Account.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,6 +1762,15 @@ def get_wallets(
17621762
connection = json["entity"]["wallets"]
17631763
return AccountToWalletsConnection_from_json(self.requester, connection)
17641764

1765+
def to_json(self) -> Mapping[str, Any]:
1766+
return {
1767+
"__typename": "Account",
1768+
"account_id": self.id,
1769+
"account_created_at": self.created_at.isoformat(),
1770+
"account_updated_at": self.updated_at.isoformat(),
1771+
"account_name": self.name,
1772+
}
1773+
17651774

17661775
FRAGMENT = """
17671776
fragment AccountFragment on Account {

lightspark/objects/AccountToApiTokensConnection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class AccountToApiTokensConnection(Connection):
2626
"""The API tokens for the current page of this connection."""
2727
typename: str
2828

29+
def to_json(self) -> Mapping[str, Any]:
30+
return {
31+
"__typename": "AccountToApiTokensConnection",
32+
"account_to_api_tokens_connection_count": self.count,
33+
"account_to_api_tokens_connection_page_info": self.page_info.to_json(),
34+
"account_to_api_tokens_connection_entities": [
35+
e.to_json() for e in self.entities
36+
],
37+
}
38+
2939

3040
FRAGMENT = """
3141
fragment AccountToApiTokensConnectionFragment on AccountToApiTokensConnection {

lightspark/objects/AccountToChannelsConnection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ class AccountToChannelsConnection:
1919
entities: List[Channel]
2020
"""The channels for the current page of this connection."""
2121

22+
def to_json(self) -> Mapping[str, Any]:
23+
return {
24+
"account_to_channels_connection_count": self.count,
25+
"account_to_channels_connection_entities": [
26+
e.to_json() for e in self.entities
27+
],
28+
}
29+
2230

2331
FRAGMENT = """
2432
fragment AccountToChannelsConnectionFragment on AccountToChannelsConnection {

lightspark/objects/AccountToNodesConnection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class AccountToNodesConnection(Connection):
2828
"""The nodes for the current page of this connection."""
2929
typename: str
3030

31+
def to_json(self) -> Mapping[str, Any]:
32+
return {
33+
"__typename": "AccountToNodesConnection",
34+
"account_to_nodes_connection_count": self.count,
35+
"account_to_nodes_connection_page_info": self.page_info.to_json(),
36+
"account_to_nodes_connection_entities": [
37+
e.to_json() for e in self.entities
38+
],
39+
}
40+
3141

3242
FRAGMENT = """
3343
fragment AccountToNodesConnectionFragment on AccountToNodesConnection {

lightspark/objects/AccountToPaymentRequestsConnection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class AccountToPaymentRequestsConnection(Connection):
2626
"""The payment requests for the current page of this connection."""
2727
typename: str
2828

29+
def to_json(self) -> Mapping[str, Any]:
30+
return {
31+
"__typename": "AccountToPaymentRequestsConnection",
32+
"account_to_payment_requests_connection_count": self.count,
33+
"account_to_payment_requests_connection_page_info": self.page_info.to_json(),
34+
"account_to_payment_requests_connection_entities": [
35+
e.to_json() for e in self.entities
36+
],
37+
}
38+
2939

3040
FRAGMENT = """
3141
fragment AccountToPaymentRequestsConnectionFragment on AccountToPaymentRequestsConnection {

lightspark/objects/AccountToTransactionsConnection.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,25 @@ class AccountToTransactionsConnection(Connection):
3737
"""The transactions for the current page of this connection."""
3838
typename: str
3939

40+
def to_json(self) -> Mapping[str, Any]:
41+
return {
42+
"__typename": "AccountToTransactionsConnection",
43+
"account_to_transactions_connection_count": self.count,
44+
"account_to_transactions_connection_page_info": self.page_info.to_json(),
45+
"account_to_transactions_connection_profit_loss": self.profit_loss.to_json()
46+
if self.profit_loss
47+
else None,
48+
"account_to_transactions_connection_average_fee_earned": self.average_fee_earned.to_json()
49+
if self.average_fee_earned
50+
else None,
51+
"account_to_transactions_connection_total_amount_transacted": self.total_amount_transacted.to_json()
52+
if self.total_amount_transacted
53+
else None,
54+
"account_to_transactions_connection_entities": [
55+
e.to_json() for e in self.entities
56+
],
57+
}
58+
4059

4160
FRAGMENT = """
4261
fragment AccountToTransactionsConnectionFragment on AccountToTransactionsConnection {

lightspark/objects/AccountToWalletsConnection.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ class AccountToWalletsConnection(Connection):
2626
"""The wallets for the current page of this connection."""
2727
typename: str
2828

29+
def to_json(self) -> Mapping[str, Any]:
30+
return {
31+
"__typename": "AccountToWalletsConnection",
32+
"account_to_wallets_connection_count": self.count,
33+
"account_to_wallets_connection_page_info": self.page_info.to_json(),
34+
"account_to_wallets_connection_entities": [
35+
e.to_json() for e in self.entities
36+
],
37+
}
38+
2939

3040
FRAGMENT = """
3141
fragment AccountToWalletsConnectionFragment on AccountToWalletsConnection {

lightspark/objects/ApiToken.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ class ApiToken(Entity):
3636
"""A list of permissions granted to the token."""
3737
typename: str
3838

39+
def to_json(self) -> Mapping[str, Any]:
40+
return {
41+
"__typename": "ApiToken",
42+
"api_token_id": self.id,
43+
"api_token_created_at": self.created_at.isoformat(),
44+
"api_token_updated_at": self.updated_at.isoformat(),
45+
"api_token_client_id": self.client_id,
46+
"api_token_name": self.name,
47+
"api_token_permissions": [e.to_json() for e in self.permissions],
48+
}
49+
3950

4051
FRAGMENT = """
4152
fragment ApiTokenFragment on ApiToken {

lightspark/objects/Balances.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ class Balances:
3030
3131
It represents the amount currently available to withdraw and is usually equal to the `owned_balance` but it does not include in-flight operations (which would likely succeed and therefore likely make your withdrawal fail)."""
3232

33+
def to_json(self) -> Mapping[str, Any]:
34+
return {
35+
"balances_owned_balance": self.owned_balance.to_json(),
36+
"balances_available_to_send_balance": self.available_to_send_balance.to_json(),
37+
"balances_available_to_withdraw_balance": self.available_to_withdraw_balance.to_json(),
38+
}
39+
3340

3441
FRAGMENT = """
3542
fragment BalancesFragment on Balances {

0 commit comments

Comments
 (0)