11# [developer-docs.sdk.python.sdk-import]-start
22from onepassword import *
33import asyncio
4+ import os
45
56
67async def main ():
8+ vault_id = os .environ .get ("OP_VAULT_ID" )
9+ if vault_id is None :
10+ raise Exception ("OP_VAULT_ID is required" )
11+
712 # [developer-docs.sdk.python.client-initialization]-start
813 # Connects to the 1Password desktop app.
914 client = await Client .authenticate (
1015 auth = DesktopAuth (
11- account_name = "YouAccountNameAsShownInDesktopApp " # Set to your 1Password account name.
16+ account_name = "Happy Company " # Set to your 1Password account name.
1217 ),
1318 # Set the following to your own integration name and version.
1419 integration_name = "My 1Password Integration" ,
1520 integration_version = "v1.0.0" ,
1621 )
17-
22+
1823 # [developer-docs.sdk.python.list-vaults]-start
1924 vaults = await client .vaults .list ()
2025 for vault in vaults :
2126 print (vault )
2227 # [developer-docs.sdk.python.list-vaults]-end
2328
2429 # [developer-docs.sdk.python.list-items]-start
25- overviews = await client .items .list (vault_id = vaults [ 0 ]. id )
30+ overviews = await client .items .list (vault_id )
2631 for overview in overviews :
2732 print (overview .title )
2833 # [developer-docs.sdk.python.list-items]-end
2934
35+ # Vault get overview
36+ vaultOverview = await client .vaults .get_overview (vault_id )
37+ print (vaultOverview )
38+
39+ # Vault get details
40+ vault = await client .vaults .get (vaultOverview .id , VaultGetParams (accessors = False ))
41+ print (vault )
42+
43+ items_to_create = []
44+ for i in range (1 , 4 ):
45+ items_to_create .append (ItemCreateParams (
46+ title = "My Login Item {}" .format (i ),
47+ category = ItemCategory .LOGIN ,
48+ vault_id = vault .id ,
49+ fields = [
50+ ItemField (
51+ id = "username" ,
52+ title = "username" ,
53+ field_type = ItemFieldType .TEXT ,
54+ value = "mynameisjeff" ,
55+ ),
56+ ItemField (
57+ id = "password" ,
58+ title = "password" ,
59+ field_type = ItemFieldType .CONCEALED ,
60+ value = "jeff" ,
61+ ),
62+ ItemField (
63+ id = "onetimepassword" ,
64+ title = "one-time-password" ,
65+ field_type = ItemFieldType .TOTP ,
66+ section_id = "totpsection" ,
67+ value = "otpauth://totp/my-example-otp?secret=jncrjgbdjnrncbjsr&issuer=1Password" ,
68+ ),
69+ ],
70+ sections = [
71+ ItemSection (
72+ id = "" , title = "" ),
73+ ItemSection (
74+ id = "totpsection" , title = "" ),
75+ ],
76+ tags = [
77+ "test tag 1" , "test tag 2" ],
78+ websites = [
79+ Website (
80+ label = "my custom website" ,
81+ url = "https://example.com" ,
82+ autofill_behavior = AutofillBehavior .NEVER ,
83+ )
84+ ],
85+ ))
86+
87+ # Batch item create
88+ batchCreateResponse = await client .items .create_all (vault .id , items_to_create )
89+
90+ item_ids = []
91+ for res in batchCreateResponse .individual_responses :
92+ if res .content is not None :
93+ print ('Created item "{}" ({})' .format (
94+ res .content .title , res .content .id ))
95+ item_ids .append (res .content .id )
96+ elif res .error is not None :
97+ print ("[Batch create] Something went wrong: {}" .format (res .error ))
98+
99+ # Batch item get
100+ batchGetReponse = await client .items .get_all (vault .id , item_ids )
101+ for res in batchGetReponse .individual_responses :
102+ if res .content is not None :
103+ print ('Obtained item "{}" ({})' .format (
104+ res .content .title , res .content .id ))
105+ elif res .error is not None :
106+ print ("[Batch get] Something went wrong: {}" .format (res .error ))
107+
108+ # Batch item delete
109+ batchDeleteResponse = await client .items .delete_all (vault .id , item_ids )
110+ for id , res in batchDeleteResponse .individual_responses .items ():
111+ if res .error is not None :
112+ print ("[Batch delete] Something went wrong: {}" .format (res .error ))
113+ else :
114+ print ("Deleted item {}" .format (id ))
115+
30116
31117if __name__ == "__main__" :
32- asyncio .run (main ())
118+ asyncio .run (main ())
0 commit comments