Skip to content

Commit 1bf98e5

Browse files
authored
Add Environment read functionality (#210)
Enable users to programatically read secrets from 1Password Environments.
1 parent ea3b917 commit 1bf98e5

11 files changed

Lines changed: 86 additions & 0 deletions

File tree

example/desktop_app.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,15 @@ async def main():
133133
print(group)
134134
# [developer-docs.sdk.python.get-group]-end
135135

136+
environment_id = os.environ.get("OP_ENVIRONMENT_ID")
137+
if environment_id is not None:
138+
# [developer-docs.sdk.python.get-environment-variables]-start
139+
# Read variables from a 1Password Environment
140+
environment = await client.environments.get_variables(environment_id)
141+
for variable in environment.variables:
142+
print(f"{variable.name}: {variable.value} (masked: {variable.masked})")
143+
# [developer-docs.sdk.python.get-environment-variables]-end
144+
136145

137146
if __name__ == "__main__":
138147
asyncio.run(main())

example/example.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,15 @@ async def main():
201201
await client.items.delete(created_item.vault_id, updated_item.id)
202202
# [developer-docs.sdk.python.delete-item]-end
203203

204+
environment_id = os.environ.get("OP_ENVIRONMENT_ID")
205+
if environment_id is not None:
206+
# [developer-docs.sdk.python.get-environment-variables]-start
207+
# Read variables from a 1Password Environment
208+
environment = await client.environments.get_variables(environment_id)
209+
for variable in environment.variables:
210+
print(f"{variable.name}: {variable.value} (masked: {variable.masked})")
211+
# [developer-docs.sdk.python.get-environment-variables]-end
212+
204213

205214
async def archive_item(client: Client, vault_id: str, item_id: str):
206215
# [developer-docs.sdk.python.archive-item]-start

src/onepassword/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .secrets import Secrets
88
from .items import Items
99
from .vaults import Vaults
10+
from .environments import Environments
1011
from .groups import Groups
1112

1213

@@ -19,6 +20,7 @@
1920
"Secrets",
2021
"Items",
2122
"Vaults",
23+
"Environments",
2224
"Groups",
2325
"DesktopAuth",
2426
"DEFAULT_INTEGRATION_NAME",

src/onepassword/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
from .secrets import Secrets
99
from .items import Items
1010
from .vaults import Vaults
11+
from .environments import Environments
1112
from .groups import Groups
1213

1314

1415
class Client:
1516
secrets: Secrets
1617
items: Items
1718
vaults: Vaults
19+
environments: Environments
1820
groups: Groups
1921

2022
@classmethod
@@ -41,6 +43,7 @@ async def authenticate(
4143
authenticated_client.secrets = Secrets(inner_client)
4244
authenticated_client.items = Items(inner_client)
4345
authenticated_client.vaults = Vaults(inner_client)
46+
authenticated_client.environments = Environments(inner_client)
4447
authenticated_client.groups = Groups(inner_client)
4548
authenticated_client._finalizer = weakref.finalize(
4649
cls, core.release_client, client_id

src/onepassword/environments.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Code generated by op-codegen - DO NO EDIT MANUALLY
2+
3+
from .core import InnerClient
4+
from pydantic import TypeAdapter
5+
from .types import GetVariablesResponse
6+
7+
8+
class Environments:
9+
"""
10+
The Environments API holds all the operations the SDK client can perform on 1Password Environments.
11+
"""
12+
13+
def __init__(self, inner_client: InnerClient):
14+
self.inner_client = inner_client
15+
16+
async def get_variables(self, environment_id: str) -> GetVariablesResponse:
17+
"""
18+
Get environment variables belonging to an Environment.
19+
"""
20+
response = await self.inner_client.invoke(
21+
{
22+
"invocation": {
23+
"clientId": self.inner_client.client_id,
24+
"parameters": {
25+
"name": "EnvironmentsGetVariables",
26+
"parameters": {"environment_id": environment_id},
27+
},
28+
}
29+
}
30+
)
31+
32+
response = TypeAdapter(GetVariablesResponse).validate_json(response)
33+
return response
38.2 KB
Binary file not shown.
71.8 KB
Binary file not shown.
36.7 KB
Binary file not shown.
40.1 KB
Binary file not shown.
42 KB
Binary file not shown.

0 commit comments

Comments
 (0)