Skip to content

Commit 0edb33b

Browse files
committed
split example logic to different methods
Signed-off-by: emiliyank <e.kadiyski@gmail.com>
1 parent 5ededd8 commit 0edb33b

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

examples/tls_query_balance.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,34 +33,52 @@ def _bool_env(name: str, default: bool = False) -> bool:
3333
return value.strip().lower() in {"1", "true", "yes"}
3434

3535

36-
def main():
37-
load_dotenv()
38-
39-
network_name = os.getenv("NETWORK", "testnet")
36+
def _load_operator_credentials() -> tuple[AccountId, PrivateKey]:
37+
"""Load operator credentials from the environment."""
4038
operator_id_str = os.getenv("OPERATOR_ID")
4139
operator_key_str = os.getenv("OPERATOR_KEY")
4240

4341
if not operator_id_str or not operator_key_str:
4442
raise ValueError("OPERATOR_ID and OPERATOR_KEY must be set in the environment")
4543

44+
operator_id = AccountId.from_string(operator_id_str)
45+
operator_key = PrivateKey.from_string(operator_key_str)
46+
return operator_id, operator_key
47+
48+
49+
def setup_client() -> Client:
50+
"""Create and configure a client with TLS enabled using env settings."""
51+
network_name = os.getenv("NETWORK", "testnet")
52+
verify_certs = _bool_env("VERIFY_CERTS", True)
53+
4654
network = Network(network_name)
4755
client = Client(network)
4856

49-
# Enable TLS for consensus nodes. Mirror nodes already require TLS.
57+
# Disable TLS for consensus nodes. Mirror nodes already require TLS.
5058
client.set_transport_security(False)
51-
52-
verify_certs = _bool_env("VERIFY_CERTS", True)
5359
client.set_verify_certificates(verify_certs)
60+
return client
5461

55-
operator_id = AccountId.from_string(operator_id_str)
56-
operator_key = PrivateKey.from_string(operator_key_str)
62+
63+
def query_account_balance(client: Client, account_id: AccountId):
64+
"""Execute a CryptoGetAccountBalanceQuery for the given account."""
65+
query = CryptoGetAccountBalanceQuery().set_account_id(account_id)
66+
balance = query.execute(client)
67+
print(f"Operator account {account_id} balance: {balance.hbars.to_hbars()} hbars")
68+
69+
70+
def main():
71+
load_dotenv()
72+
73+
operator_id, operator_key = _load_operator_credentials()
74+
client = setup_client()
5775
client.set_operator(operator_id, operator_key)
5876

59-
balance = CryptoGetAccountBalanceQuery().set_account_id(operator_id).execute(client)
60-
print(f"Operator account {operator_id} balance: {balance.hbars.to_hbars()} hbars")
77+
query_account_balance(client, operator_id)
6178

6279

6380
if __name__ == "__main__":
6481
main()
6582

6683

84+

0 commit comments

Comments
 (0)