44This module provides consistent JSON response formatting across all btcli commands.
55All JSON outputs should use these utilities to ensure schema compliance.
66
7- Standard Response Format:
7+ Standard Transaction Response Format:
88{
9- "success": bool, # Required: Whether the operation succeeded
10- "data ": {...} , # Optional: Command-specific response data
11- "error ": str # Optional: Error message if success=False
9+ "success": bool, # Required: Whether the operation succeeded
10+ "message ": str | None , # Optional: Human-readable message
11+ "extrinsic_identifier ": str | None # Optional: Block-extrinsic ID (e.g., "12345-2")
1212}
1313
14- For transaction responses, data should include :
14+ Standard Data Response Format :
1515{
16- "extrinsic_hash": str, # The transaction hash
17- "block_hash": str # The block containing the transaction
16+ "success": bool, # Required: Whether the operation succeeded
17+ "data": {...}, # Optional: Command-specific response data
18+ "error": str # Optional: Error message if success=False
1819}
1920"""
2021
2122import json
2223from typing import Any , Optional , Union
2324from rich .console import Console
2425
25- # JSON console for outputting JSON responses
26+ # JSON console for outputting JSON responses (matches existing btcli pattern)
2627json_console = Console ()
2728
2829
30+ # =============================================================================
31+ # Transaction Response Utilities (for commands that submit extrinsics)
32+ # =============================================================================
33+
34+ def transaction_response (
35+ success : bool ,
36+ message : Optional [str ] = None ,
37+ extrinsic_identifier : Optional [str ] = None ,
38+ ) -> dict [str , Any ]:
39+ """
40+ Create a standardized transaction response dictionary.
41+
42+ Args:
43+ success: Whether the transaction succeeded
44+ message: Human-readable status message
45+ extrinsic_identifier: The extrinsic ID (e.g., "12345678-2")
46+
47+ Returns:
48+ Dictionary with standardized transaction format
49+ """
50+ return {
51+ "success" : success ,
52+ "message" : message ,
53+ "extrinsic_identifier" : extrinsic_identifier ,
54+ }
55+
56+
57+ def print_transaction_response (
58+ success : bool ,
59+ message : Optional [str ] = None ,
60+ extrinsic_identifier : Optional [str ] = None ,
61+ ) -> None :
62+ """
63+ Print a standardized transaction response as JSON.
64+
65+ Args:
66+ success: Whether the transaction succeeded
67+ message: Human-readable status message
68+ extrinsic_identifier: The extrinsic ID (e.g., "12345678-2")
69+ """
70+ json_console .print_json (data = transaction_response (success , message , extrinsic_identifier ))
71+
72+
73+ class TransactionResult :
74+ """
75+ Helper class for building transaction responses.
76+
77+ Provides a clean interface for transaction commands that need to
78+ build up response data before printing.
79+ """
80+
81+ def __init__ (
82+ self ,
83+ success : bool ,
84+ message : Optional [str ] = None ,
85+ extrinsic_identifier : Optional [str ] = None ,
86+ ):
87+ self .success = success
88+ self .message = message
89+ self .extrinsic_identifier = extrinsic_identifier
90+
91+ def as_dict (self ) -> dict [str , Any ]:
92+ """Return the response as a dictionary."""
93+ return transaction_response (
94+ self .success ,
95+ self .message ,
96+ self .extrinsic_identifier ,
97+ )
98+
99+ def print (self ) -> None :
100+ """Print the response as JSON."""
101+ json_console .print_json (data = self .as_dict ())
102+
103+
104+ class MultiTransactionResult :
105+ """
106+ Helper class for commands that process multiple transactions.
107+
108+ Builds a keyed dictionary of transaction results.
109+ """
110+
111+ def __init__ (self ):
112+ self ._results : dict [str , TransactionResult ] = {}
113+
114+ def add (
115+ self ,
116+ key : str ,
117+ success : bool ,
118+ message : Optional [str ] = None ,
119+ extrinsic_identifier : Optional [str ] = None ,
120+ ) -> None :
121+ """Add a transaction result with the given key."""
122+ self ._results [key ] = TransactionResult (success , message , extrinsic_identifier )
123+
124+ def add_result (self , key : str , result : TransactionResult ) -> None :
125+ """Add an existing TransactionResult with the given key."""
126+ self ._results [key ] = result
127+
128+ def as_dict (self ) -> dict [str , dict [str , Any ]]:
129+ """Return all results as a dictionary."""
130+ return {k : v .as_dict () for k , v in self ._results .items ()}
131+
132+ def print (self ) -> None :
133+ """Print all results as JSON."""
134+ json_console .print_json (data = self .as_dict ())
135+
136+
137+ # =============================================================================
138+ # Data Response Utilities (for commands that return data, not transactions)
139+ # =============================================================================
140+
29141def json_response (
30142 success : bool ,
31143 data : Optional [Any ] = None ,
32144 error : Optional [str ] = None ,
33145) -> str :
34146 """
35- Create a standardized JSON response string.
147+ Create a standardized JSON response string for data queries .
36148
37149 Args:
38150 success: Whether the operation succeeded
@@ -62,7 +174,7 @@ def json_response(
62174
63175def json_success (data : Any ) -> str :
64176 """
65- Create a successful JSON response.
177+ Create a successful JSON response string .
66178
67179 Args:
68180 data: Response data to include
@@ -75,7 +187,7 @@ def json_success(data: Any) -> str:
75187
76188def json_error (error : str , data : Optional [Any ] = None ) -> str :
77189 """
78- Create an error JSON response.
190+ Create an error JSON response string .
79191
80192 Args:
81193 error: Error message describing what went wrong
@@ -87,43 +199,9 @@ def json_error(error: str, data: Optional[Any] = None) -> str:
87199 return json_response (success = False , data = data , error = error )
88200
89201
90- def json_transaction (
91- success : bool ,
92- extrinsic_hash : Optional [str ] = None ,
93- block_hash : Optional [str ] = None ,
94- error : Optional [str ] = None ,
95- ** extra_data : Any ,
96- ) -> str :
97- """
98- Create a standardized transaction response.
99-
100- Args:
101- success: Whether the transaction succeeded
102- extrinsic_hash: The transaction/extrinsic hash
103- block_hash: The block hash containing the transaction
104- error: Error message if transaction failed
105- **extra_data: Additional transaction-specific data
106-
107- Returns:
108- JSON string with transaction details
109- """
110- data : dict [str , Any ] = {}
111-
112- if extrinsic_hash is not None :
113- data ["extrinsic_hash" ] = extrinsic_hash
114-
115- if block_hash is not None :
116- data ["block_hash" ] = block_hash
117-
118- # Add any extra data
119- data .update (extra_data )
120-
121- return json_response (success = success , data = data if data else None , error = error )
122-
123-
124202def print_json (response : str ) -> None :
125203 """
126- Print a JSON response to the console.
204+ Print a JSON string response to the console.
127205
128206 Args:
129207 response: JSON string to print
@@ -152,6 +230,20 @@ def print_json_error(error: str, data: Optional[Any] = None) -> None:
152230 print_json (json_error (error , data ))
153231
154232
233+ def print_json_data (data : Any ) -> None :
234+ """
235+ Print data directly as JSON (for simple data responses).
236+
237+ Args:
238+ data: Data to print as JSON
239+ """
240+ json_console .print_json (data = data )
241+
242+
243+ # =============================================================================
244+ # Serialization Utilities
245+ # =============================================================================
246+
155247def serialize_balance (balance : Any ) -> dict [str , Union [int , float ]]:
156248 """
157249 Serialize a Balance object to a consistent dictionary format.
0 commit comments