Skip to content

Commit c2e62b2

Browse files
authored
Merge pull request #78 from 1Password/sdk-core/2024-07-26-384c9aa2
SDK core 384c9aa2
2 parents 1a35861 + 4011d0a commit c2e62b2

File tree

17 files changed

+178
-48
lines changed

17 files changed

+178
-48
lines changed

example/example.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from onepassword import *
44

5+
56
async def main():
67
# Gets your service account token from the OP_SERVICE_ACCOUNT_TOKEN environment variable.
78
token = os.getenv("OP_SERVICE_ACCOUNT_TOKEN")
@@ -19,8 +20,7 @@ async def main():
1920
print(value)
2021

2122
# Create an Item and add it to your vault.
22-
to_create = Item(
23-
id="",
23+
to_create = ItemCreateParams(
2424
title="MyName",
2525
category="Login",
2626
vault_id="q73bqltug6xoegr3wkk2zkenoq",
@@ -31,29 +31,48 @@ async def main():
3131
field_type="Text",
3232
section_id=None,
3333
value="mynameisjeff",
34+
details=None,
3435
),
3536
ItemField(
3637
id="password",
3738
title="password",
3839
field_type="Concealed",
3940
section_id=None,
4041
value="jeff",
42+
details=None,
43+
),
44+
ItemField(
45+
id="onetimepassword",
46+
title="one-time-password",
47+
field_type="Totp",
48+
section_id="totpsection",
49+
value="otpauth://totp/my-example-otp?secret=jncrjgbdjnrncbjsr&issuer=1Password",
50+
details=None,
4151
),
4252
],
43-
sections=[ItemSection(id="", title="")],
53+
sections=[ItemSection(id="", title=""), ItemSection(id="totpsection", title="")],
4454
)
4555
created_item = await client.items.create(to_create)
4656

4757
print(dict(created_item))
4858

59+
# Fetch a totp code from the item
60+
for f in created_item.fields:
61+
if f.field_type == "Totp":
62+
if f.details.content.error_message is not None:
63+
print(f.details.content.error_message)
64+
else:
65+
print(f.details.content.code)
66+
67+
4968
# Retrieve an item from your vault.
5069
item = await client.items.get(created_item.vault_id, created_item.id)
5170

5271
print(dict(item))
5372

5473
# Update a field in your item
5574
item.fields[0].value = "new_value"
56-
updated_item = await client.items.update(item)
75+
updated_item = await client.items.put(item)
5776

5877
print(dict(updated_item))
5978

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def get_shared_library_data_to_include():
3131
platform_to_lib = {
3232
"Darwin": "libop_uniffi_core.dylib",
3333
"Linux": "libop_uniffi_core.so",
34-
"Windows": "op_uniffi_core.dll"
34+
"Windows": "op_uniffi_core.dll",
3535
}
3636
c_shared_library_file_name = platform_to_lib.get(platform.system(), "")
3737
c_shared_library_file_name = os.path.join(include_path, c_shared_library_file_name)

src/onepassword/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
# AUTO-GENERATED
22
from .client import Client
33
from .defaults import DEFAULT_INTEGRATION_NAME, DEFAULT_INTEGRATION_VERSION
4-
from .types import * # noqa F403
4+
from .types import * # noqa F403
55
from .secrets import Secrets
66
from .items import Items
77

8-
import sys
8+
9+
import sys
910
import inspect
1011
import typing
1112

@@ -19,5 +20,8 @@
1920

2021
for name, obj in inspect.getmembers(sys.modules["onepassword.types"]):
2122
# Add all classes and instances of typing.Literal defined in types.py.
22-
if (inspect.isclass(obj) and inspect.getmodule(obj) == sys.modules["onepassword.types"]) or type(eval(name)) == typing._LiteralGenericAlias:
23+
if (
24+
inspect.isclass(obj)
25+
and inspect.getmodule(obj) == sys.modules["onepassword.types"]
26+
) or type(eval(name)) == typing._LiteralGenericAlias:
2327
__all__.append(name)

src/onepassword/client.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@
77

88

99
class Client:
10-
secrets: Secrets
11-
items: Items
12-
10+
secrets: Secrets
11+
items: Items
1312

14-
@classmethod
15-
async def authenticate(cls, auth, integration_name, integration_version):
16-
config = new_default_config(
17-
auth=auth,
18-
integration_name=integration_name,
19-
integration_version=integration_version,
20-
)
21-
client_id = int(await _init_client(config))
13+
@classmethod
14+
async def authenticate(cls, auth, integration_name, integration_version):
15+
config = new_default_config(
16+
auth=auth,
17+
integration_name=integration_name,
18+
integration_version=integration_version,
19+
)
20+
client_id = int(await _init_client(config))
2221

23-
authenticated_client = cls()
22+
authenticated_client = cls()
2423

25-
authenticated_client.secrets = Secrets(client_id)
26-
authenticated_client.items = Items(client_id)
27-
authenticated_client._finalizer = weakref.finalize(cls, _release_client, client_id)
24+
authenticated_client.secrets = Secrets(client_id)
25+
authenticated_client.items = Items(client_id)
26+
authenticated_client._finalizer = weakref.finalize(
27+
cls, _release_client, client_id
28+
)
2829

29-
return authenticated_client
30+
return authenticated_client

src/onepassword/core.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
elif machine_arch in ["aarch64", "arm64"]:
1010
import onepassword.lib.aarch64.op_uniffi_core as core
1111
else:
12-
raise ImportError(f"Your machine's architecture is not currently supported: {machine_arch}")
12+
raise ImportError(
13+
f"Your machine's architecture is not currently supported: {machine_arch}"
14+
)
1315

1416

1517
# InitClient creates a client instance in the current core module and returns its unique ID.

src/onepassword/defaults.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from src.release.version import SDK_BUILD_NUMBER
33

44
SDK_LANGUAGE = "Python"
5-
SDK_VERSION = SDK_BUILD_NUMBER
5+
SDK_VERSION = SDK_BUILD_NUMBER
66
DEFAULT_INTEGRATION_NAME = "Unknown"
77
DEFAULT_INTEGRATION_VERSION = "Unknown"
88
DEFAULT_REQUEST_LIBRARY = "net/http"

src/onepassword/items.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
1+
# AUTO-GENERATED
12
from .core import _invoke
2-
import json
3+
from json import loads
34
from .types import Item
45

56

67
class Items:
7-
"""Contains all operations the SDK client can perform on 1Password items."""
8+
"""
9+
The Items API holds all operations the SDK client can perform on 1Password items.
10+
"""
811

912
def __init__(self, client_id):
1013
self.client_id = client_id
1114

12-
async def create(self, item):
13-
"""Create a new item"""
15+
async def create(self, params):
16+
"""
17+
Create a new item
18+
"""
1419
response = await _invoke(
1520
{
1621
"clientId": self.client_id,
1722
"invocation": {
1823
"name": "Create",
1924
"parameters": {
20-
"item": item.dict(),
25+
"params": params.dict(),
2126
},
2227
},
2328
}
2429
)
25-
result = Item(**json.loads(response))
26-
return result
30+
return Item(**loads(response))
2731

2832
async def get(self, vault_id, item_id):
29-
"""Get an item by vault and item ID"""
33+
"""
34+
Get an item by vault and item ID
35+
"""
3036
response = await _invoke(
3137
{
3238
"clientId": self.client_id,
@@ -39,27 +45,30 @@ async def get(self, vault_id, item_id):
3945
},
4046
}
4147
)
42-
result = Item(**json.loads(response))
43-
return result
48+
return Item(**loads(response))
4449

45-
async def update(self, item):
46-
"""Update an existing item. You can currently only edit text and concealed fields."""
50+
async def put(self, item):
51+
"""
52+
Update an existing item.
53+
"""
4754
response = await _invoke(
4855
{
4956
"clientId": self.client_id,
5057
"invocation": {
51-
"name": "Update",
58+
"name": "Put",
5259
"parameters": {
5360
"item": item.dict(),
5461
},
5562
},
5663
}
5764
)
58-
result = Item(**json.loads(response))
59-
return result
65+
return Item(**loads(response))
6066

6167
async def delete(self, vault_id, item_id):
62-
"""Delete an item. """
68+
"""
69+
Delete an item.
70+
"""
71+
6372
await _invoke(
6473
{
6574
"clientId": self.client_id,
-25.5 KB
Binary file not shown.
-522 KB
Binary file not shown.
-14 KB
Binary file not shown.

0 commit comments

Comments
 (0)