Skip to content

Commit c9c2d27

Browse files
authored
refactor: update example client setup to use Client.from_env() (#2093)
Signed-off-by: ParasSalonia <parassalonia22@gmail.com> Signed-off-by: Paras Salonia <parassalonia22@gmail.com>
1 parent 2bde066 commit c9c2d27

44 files changed

Lines changed: 231 additions & 589 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.

examples/account/account_allowance_approve_transaction_hbar.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
from dotenv import load_dotenv
3838

39-
from hiero_sdk_python import AccountId, Client, Hbar, Network, PrivateKey, TransactionId
39+
from hiero_sdk_python import AccountId, Client, Hbar, PrivateKey, TransactionId
4040
from hiero_sdk_python.account.account_allowance_approve_transaction import (
4141
AccountAllowanceApproveTransaction,
4242
)
@@ -50,16 +50,10 @@
5050

5151

5252
def setup_client() -> Client:
53-
"""Initialize and set up the client with operator account using env vars."""
54-
network = Network(network_name)
55-
print(f"Connecting to Hedera {network_name} network!")
56-
client = Client(network)
57-
58-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
59-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
60-
client.set_operator(operator_id, operator_key)
53+
"""Setup Client."""
54+
client = Client.from_env()
55+
print(f"Network: {client.network.network}")
6156
print(f"Client set up with operator id {client.operator_account_id}")
62-
6357
return client
6458

6559

examples/account/account_allowance_approve_transaction_nft.py

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
AccountId,
4040
Client,
4141
Hbar,
42-
Network,
4342
NftId,
4443
PrivateKey,
4544
ResponseCode,
@@ -58,25 +57,16 @@
5857
network_name = os.getenv("NETWORK", "testnet").lower()
5958

6059

61-
def setup_client():
62-
"""Initialize and set up the client with operator account."""
63-
if os.getenv("OPERATOR_ID") is None or os.getenv("OPERATOR_KEY") is None:
64-
print("Environment variables OPERATOR_ID and OPERATOR_KEY must be set")
65-
sys.exit(1)
66-
67-
network = Network(network_name)
68-
print(f"Connecting to Hedera {network_name} network!")
69-
client = Client(network)
60+
def setup_client() -> tuple[Client, AccountId, PrivateKey]:
61+
"""Setup Client."""
62+
client = Client.from_env()
7063

71-
operator_id_str = os.getenv("OPERATOR_ID")
72-
operator_key_str = os.getenv("OPERATOR_KEY")
73-
assert operator_id_str is not None and operator_key_str is not None
64+
operator_id = client.operator_account_id
65+
operator_key = client.operator_private_key
7466

75-
operator_id = AccountId.from_string(operator_id_str)
76-
operator_key = PrivateKey.from_string(operator_key_str)
77-
78-
client.set_operator(operator_id, operator_key)
67+
print(f"Network: {client.network.network}")
7968
print(f"Client setup for NFT Owner (Operator): {client.operator_account_id}")
69+
8070
return client, operator_id, operator_key
8171

8272

examples/account/account_allowance_delete_transaction_hbar.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from dotenv import load_dotenv
77

8-
from hiero_sdk_python import AccountId, Client, Hbar, Network, PrivateKey, TransactionId
8+
from hiero_sdk_python import AccountId, Client, Hbar, PrivateKey, TransactionId
99
from hiero_sdk_python.account.account_allowance_approve_transaction import (
1010
AccountAllowanceApproveTransaction,
1111
)
@@ -19,16 +19,10 @@
1919

2020

2121
def setup_client() -> Client:
22-
"""Initialize and set up the client with operator account using env vars."""
23-
network = Network(network_name)
24-
print(f"Connecting to Hedera {network_name} network!")
25-
client = Client(network)
26-
27-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
28-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
29-
client.set_operator(operator_id, operator_key)
22+
"""Setup Client."""
23+
client = Client.from_env()
24+
print(f"Network: {client.network.network}")
3025
print(f"Client set up with operator id {client.operator_account_id}")
31-
3226
return client
3327

3428

examples/account/account_allowance_delete_transaction_nft.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
AccountId,
2525
Client,
2626
Hbar,
27-
Network,
2827
NftId,
2928
PrivateKey,
3029
ResponseCode,
@@ -43,25 +42,14 @@
4342
network_name = os.getenv("NETWORK", "testnet").lower()
4443

4544

46-
def setup_client():
47-
"""Initialize and set up the client with operator account."""
48-
if os.getenv("OPERATOR_ID") is None or os.getenv("OPERATOR_KEY") is None:
49-
print("Environment variables OPERATOR_ID and OPERATOR_KEY must be set")
50-
sys.exit(1)
51-
52-
network = Network(network_name)
53-
print(f"Connecting to Hedera {network_name} network!")
54-
client = Client(network)
55-
56-
operator_id_str = os.getenv("OPERATOR_ID")
57-
operator_key_str = os.getenv("OPERATOR_KEY")
58-
59-
assert operator_id_str is not None and operator_key_str is not None
45+
def setup_client() -> tuple[Client, AccountId, PrivateKey]:
46+
"""Setup Client."""
47+
client = Client.from_env()
6048

61-
operator_id = AccountId.from_string(operator_id_str)
62-
operator_key = PrivateKey.from_string(operator_key_str)
49+
operator_id = client.operator_account_id
50+
operator_key = client.operator_private_key
6351

64-
client.set_operator(operator_id, operator_key)
52+
print(f"Network: {client.network.network}")
6553
print(f"Client setup for NFT Owner (Operator): {client.operator_account_id}")
6654

6755
return client, operator_id, operator_key

examples/account/account_create_transaction_create_with_alias.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,9 @@
3131

3232
def setup_client() -> Client:
3333
"""Initialize client from environment variables."""
34-
try:
35-
client = Client.from_env()
36-
print(f"Client set up with operator id {client.operator_account_id}")
37-
return client
38-
39-
except Exception:
40-
print("Error: Please check OPERATOR_ID, OPERATOR_KEY, and NETWORK in your .env file.")
41-
sys.exit(1)
34+
client = Client.from_env()
35+
print(f"Client set up with operator id {client.operator_account_id}")
36+
return client
4237

4338

4439
def generate_main_and_alias_keys() -> tuple[PrivateKey, PrivateKey]:

examples/account/account_create_transaction_evm_alias.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,9 @@
3535

3636
def setup_client() -> Client:
3737
"""Initialize client from environment variables."""
38-
try:
39-
client = Client.from_env()
40-
print(f"Client set up with operator id {client.operator_account_id}")
41-
return client
42-
43-
except Exception:
44-
print("Error: Please check OPERATOR_ID, OPERATOR_KEY, and NETWORK in your .env file.")
45-
sys.exit(1)
38+
client = Client.from_env()
39+
print(f"Client set up with operator id {client.operator_account_id}")
40+
return client
4641

4742

4843
def generate_alias_key() -> tuple[PrivateKey, PublicKey, EvmAddress]:

examples/account/account_create_transaction_with_fallback_alias.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,9 @@
3131

3232
def setup_client() -> Client:
3333
"""Initialize client from environment variables."""
34-
try:
35-
client = Client.from_env()
36-
print(f"Client set up with operator id {client.operator_account_id}")
37-
return client
38-
except Exception:
39-
print("Error: Please check OPERATOR_ID, OPERATOR_KEY, and NETWORK in your .env file.")
40-
sys.exit(1)
34+
client = Client.from_env()
35+
print(f"Client set up with operator id {client.operator_account_id}")
36+
return client
4137

4238

4339
def generate_fallback_key() -> PrivateKey:

examples/account/account_records_query.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414

1515
from hiero_sdk_python import (
1616
AccountCreateTransaction,
17-
AccountId,
1817
Client,
1918
Hbar,
20-
Network,
2119
PrivateKey,
2220
ResponseCode,
2321
)
@@ -30,17 +28,11 @@
3028
network_name = os.getenv("NETWORK", "testnet").lower()
3129

3230

33-
def setup_client():
34-
"""Initialize and set up the client with operator account."""
35-
network = Network(network_name)
36-
print(f"Connecting to Hedera {network_name} network!")
37-
client = Client(network)
38-
39-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID", ""))
40-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY", ""))
41-
client.set_operator(operator_id, operator_key)
31+
def setup_client() -> Client:
32+
"""Setup Client."""
33+
client = Client.from_env()
34+
print(f"Network: {client.network.network}")
4235
print(f"Client set up with operator id {client.operator_account_id}")
43-
4436
return client
4537

4638

examples/client/client.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,10 @@ def setup_network():
3535
return network
3636

3737

38-
def setup_client(network):
39-
"""Create and initialize the client with the network."""
40-
print("\nStep 2: Create the client with the network")
41-
client = Client(network)
42-
43-
print(f" - Client initialized with network: {client.network.network}")
38+
def setup_client() -> Client:
39+
"""Setup Client."""
40+
client = Client.from_env()
41+
print(f"Network: {client.network.network}")
4442
return client
4543

4644

@@ -88,8 +86,8 @@ def display_available_nodes(client):
8886
def demonstrate_manual_setup():
8987
"""Run the detailed, step-by-step setup."""
9088
print("\n--- [ Method 1: Manual Setup] ---")
91-
network = setup_network()
92-
client = setup_client(network)
89+
setup_network()
90+
client = setup_client()
9391
setup_operator(client)
9492
display_client_configuration(client)
9593
display_available_nodes(client)

examples/consensus/topic_message_submit_chunked_transaction.py

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@
66
python examples/consensus/topic_message_submit_chunked.py
77
"""
88

9-
import os
109
import sys
1110

1211
from dotenv import load_dotenv
1312

1413
from hiero_sdk_python import (
15-
AccountId,
1614
Client,
17-
Network,
18-
PrivateKey,
1915
ResponseCode,
2016
TopicCreateTransaction,
2117
TopicInfoQuery,
@@ -54,21 +50,12 @@
5450
load_dotenv()
5551

5652

57-
def setup_client():
58-
"""Set up and configure a Hedera client for testnet operations."""
59-
network_name = os.getenv("NETWORK", "testnet").lower()
60-
61-
print(f"Connecting to Hedera {network_name} network!")
62-
53+
def setup_client() -> Client:
54+
"""Setup Client."""
6355
try:
64-
network = Network(network_name)
65-
client = Client(network)
66-
67-
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID"))
68-
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY"))
69-
70-
client.set_operator(operator_id, operator_key)
71-
print(f"Client initialized with operator: {operator_id}")
56+
client = Client.from_env()
57+
print(f"Network: {client.network.network}")
58+
print(f"Client initialized with operator: {client.operator_account_id}")
7259
return client
7360
except Exception as e:
7461
print(f"Failed to set up client: {e}")

0 commit comments

Comments
 (0)