1+ """Basic usage of the stackcoin SDK."""
2+
13import asyncio
24import os
3- from stackcoin_python import AuthenticatedClient
4- from stackcoin_python .models import (
5- CreateRequestParams ,
6- BalanceResponse ,
7- CreateRequestResponse ,
8- RequestsResponse ,
9- TransactionsResponse ,
10- UsersResponse ,
11- )
12- from stackcoin_python .api .default import (
13- stackcoin_self_balance ,
14- stackcoin_create_request ,
15- stackcoin_users ,
16- stackcoin_requests ,
17- stackcoin_transactions ,
18- )
19-
20-
21- async def main (token , base_url ):
22- client = AuthenticatedClient (base_url = base_url , token = token )
23-
24- async with client as client :
25- print ("Getting balance" )
26-
27- my_balance = await stackcoin_self_balance .asyncio (client = client )
28-
29- if not isinstance (my_balance , BalanceResponse ):
30- raise Exception ("Failed to get balance" )
31-
32- print (f"Logged in as { my_balance .username } with balance { my_balance .balance } " )
33-
34- print ("Creating request" )
35-
36- request = await stackcoin_create_request .asyncio (
37- client = client ,
38- user_id = 2 ,
39- body = CreateRequestParams (
40- amount = 100 ,
41- label = "pay up buddy" ,
42- ),
43- )
44-
45- if not isinstance (request , CreateRequestResponse ):
46- raise Exception ("Failed to create request" )
475
48- print (
49- f"Created request { request .request_id } to { request .responder .username } with amount { request .amount } "
50- )
6+ import stackcoin
517
52- print ("Getting requests" )
538
54- requests = await stackcoin_requests .asyncio (client = client )
9+ async def main (token : str , base_url : str ):
10+ async with stackcoin .Client (base_url = base_url , token = token ) as client :
11+ # Who am I?
12+ me = await client .get_me ()
13+ print (f"Logged in as { me .username } with balance { me .balance } STK" )
5514
56- if not isinstance (requests , RequestsResponse ):
57- raise Exception ("Failed to get requests" )
15+ # Create a request
16+ req = await client .create_request (to_user_id = 2 , amount = 100 , label = "pay up buddy" )
17+ print (f"Created request #{ req .request_id } to { req .responder .username } for { req .amount } STK" )
5818
59- if not isinstance (requests .requests , list ):
60- raise Exception ("Failed to get requests" )
19+ # List pending requests
20+ requests = await client .get_requests ()
21+ print (f"\n { len (requests )} request(s):" )
22+ for r in requests :
23+ print (f" #{ r .id } -> { r .responder .username } : { r .amount } STK ({ r .status } )" )
6124
62- for request in requests .requests :
63- print (
64- f"Request { request .id } to { request .responder .username } with amount { request .amount } "
65- )
25+ # List recent transactions
26+ transactions = await client .get_transactions ()
27+ print (f"\n { len (transactions )} transaction(s):" )
28+ for txn in transactions :
29+ print (f" #{ txn .id } { txn .from_ .username } -> { txn .to .username } : { txn .amount } STK" )
6630
67- print ("Getting transactions" )
68-
69- transactions = await stackcoin_transactions .asyncio (client = client )
70-
71- if not isinstance (transactions , TransactionsResponse ):
72- raise Exception ("Failed to get transactions" )
73-
74- if not isinstance (transactions .transactions , list ):
75- raise Exception ("Failed to get transactions" )
76-
77- for transaction in transactions .transactions :
78- print (
79- f"Transaction { transaction .id } from { transaction .from_ .username } to { transaction .to .username } with amount { transaction .amount } "
80- )
81-
82- print ("Getting users" )
83-
84- users = await stackcoin_users .asyncio (client = client )
85-
86- if not isinstance (users , UsersResponse ):
87- raise Exception ("Failed to get users" )
88-
89- if not isinstance (users .users , list ):
90- raise Exception ("Failed to get users" )
91-
92- for user in users .users :
93- print (f"User { user .id } { user .username } " )
31+ # List users
32+ users = await client .get_users ()
33+ print (f"\n { len (users )} user(s):" )
34+ for user in users :
35+ print (f" #{ user .id } { user .username } ({ user .balance } STK)" )
9436
9537
9638if __name__ == "__main__" :
@@ -102,5 +44,4 @@ async def main(token, base_url):
10244 exit (1 )
10345
10446 base_url = os .getenv ("STACKCOIN_BASE_URL" , "https://stackcoin.world" )
105-
10647 asyncio .run (main (token , base_url ))
0 commit comments