Skip to content

Commit 2a741f6

Browse files
committed
Added example README. Edited example.py to resolve secret reference with parameters of created item. example.py reads vault id from env var. Call to run example.py added to pipeline
1 parent a7b8bbc commit 2a741f6

File tree

3 files changed

+41
-7
lines changed

3 files changed

+41
-7
lines changed

.github/workflows/validate.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,12 @@ jobs:
3535
pip install pytest-asyncio &&
3636
pip install pydantic &&
3737
python -m pytest src/onepassword/test_client.py
38-
38+
- name: Example Test
39+
env:
40+
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TEST_SERVICE_ACCOUNT_TOKEN }}
41+
OP_VAULT_ID: ${{ secrets.TEST_SERVICE_ACCOUNT_VAULT_ID }}
42+
run: |
43+
python example/example.py
3944
lint:
4045
name: Lint
4146
runs-on: ubuntu-latest
@@ -97,6 +102,16 @@ jobs:
97102
- run: |
98103
echo "Integration tests completed successfully!"
99104
105+
- name: Example Test
106+
env:
107+
OP_SERVICE_ACCOUNT_TOKEN: ${{ secrets.TEST_SERVICE_ACCOUNT_TOKEN }}
108+
OP_VAULT_ID: ${{ secrets.TEST_SERVICE_ACCOUNT_VAULT_ID }}
109+
run: |
110+
python example/example.py
111+
112+
- run: |
113+
echo "Example file ran successfully!"
114+
100115
# Update check run called "integration-fork" on the forked PR
101116
- uses: actions/github-script@v6
102117
id: update-check-run

example/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Examples
2+
3+
This folder contains a code snippet that demonstrates how to use the 1Password Python SDK to retrieve a secret from 1Password and export it as an environment variable.
4+
5+
## Prerequisites
6+
7+
1. Clone the repository and follow the steps to [get started](https://github.com/1Password/onepassword-sdk-python/blob/main/README.md).
8+
2. Make sure to export a valid service account token. For example:
9+
```bash
10+
export OP_SERVICE_ACCOUNT_TOKEN="<your token>"
11+
```
12+
3. Make sure to export a valid vault uuid. For example:
13+
```bash
14+
export OP_VAULT_ID="<your vault uuid>"
15+
```

example/example.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,16 @@ async def main():
4040
print(error)
4141
# [developer-docs.sdk.python.validate-secret-reference]-end
4242

43-
# [developer-docs.sdk.python.resolve-secret]-start
44-
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
45-
value = await client.secrets.resolve("op://vault/item/field")
46-
print(value)
47-
# [developer-docs.sdk.python.resolve-secret]-end
43+
vault_id= os.getenv("OP_VAULT_ID")
44+
if vault_id is None:
45+
raise Exception("OP_VAULT_ID environment variable is not set")
4846

4947
# [developer-docs.sdk.python.create-item]-start
5048
# Create an Item and add it to your vault.
5149
to_create = ItemCreateParams(
5250
title="MyName",
5351
category=ItemCategory.LOGIN,
54-
vault_id="7turaasywpymt3jecxoxk5roli",
52+
vault_id=vault_id,
5553
fields=[
5654
ItemField(
5755
id="username",
@@ -91,6 +89,12 @@ async def main():
9189

9290
print(dict(created_item))
9391

92+
# [developer-docs.sdk.python.resolve-secret]-start
93+
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
94+
value = await client.secrets.resolve(f"op://{created_item.vault_id}/{created_item.id}/username")
95+
print(value)
96+
# [developer-docs.sdk.python.resolve-secret]-end
97+
9498
# [developer-docs.sdk.python.resolve-totp-code]-start
9599
# Retrieves a secret from 1Password. Takes a secret reference as input and returns the secret to which it points.
96100
code = await client.secrets.resolve(

0 commit comments

Comments
 (0)