|
| 1 | +"""Documentation examples for Account and Basins page. |
| 2 | +
|
| 3 | +These snippets are extracted by the docs build script. |
| 4 | +
|
| 5 | +Run with: python examples/docs/account_and_basins.py |
| 6 | +Requires: S2_ACCESS_TOKEN, S2_BASIN environment variables |
| 7 | +
|
| 8 | +Note: These examples create resources with hardcoded names. |
| 9 | +They may fail on repeated runs if resources already exist. |
| 10 | +""" |
| 11 | + |
| 12 | +import asyncio |
| 13 | +import os |
| 14 | +from datetime import datetime, timezone |
| 15 | + |
| 16 | +from s2_sdk import ( |
| 17 | + S2, |
| 18 | + AccessTokenScope, |
| 19 | + OperationGroupPermissions, |
| 20 | + Permission, |
| 21 | + PrefixMatch, |
| 22 | +) |
| 23 | + |
| 24 | +access_token = os.environ["S2_ACCESS_TOKEN"] |
| 25 | +basin_name = os.environ["S2_BASIN"] |
| 26 | + |
| 27 | + |
| 28 | +async def main(): |
| 29 | + async with S2(access_token) as client: |
| 30 | + # ANCHOR: basin-operations |
| 31 | + # List basins |
| 32 | + basins = await client.list_basins() |
| 33 | + |
| 34 | + # Create a basin |
| 35 | + await client.create_basin("my-events") |
| 36 | + |
| 37 | + # Get configuration |
| 38 | + config = await client.get_basin_config("my-events") |
| 39 | + |
| 40 | + # Delete |
| 41 | + await client.delete_basin("my-events") |
| 42 | + # ANCHOR_END: basin-operations |
| 43 | + print(f"Basins: {len(basins.items)} found, config: {config}") |
| 44 | + |
| 45 | + basin = client.basin(basin_name) |
| 46 | + |
| 47 | + # ANCHOR: stream-operations |
| 48 | + # List streams |
| 49 | + streams = await basin.list_streams(prefix="user-") |
| 50 | + |
| 51 | + # Create a stream |
| 52 | + await basin.create_stream( |
| 53 | + "user-actions", |
| 54 | + # config=StreamConfig(...) # optional |
| 55 | + ) |
| 56 | + |
| 57 | + # Get configuration |
| 58 | + config = await basin.get_stream_config("user-actions") |
| 59 | + |
| 60 | + # Delete |
| 61 | + await basin.delete_stream("user-actions") |
| 62 | + # ANCHOR_END: stream-operations |
| 63 | + print(f"Streams: {len(streams.items)} found, config: {config}") |
| 64 | + |
| 65 | + # ANCHOR: access-token-basic |
| 66 | + # List tokens (returns metadata, not the secret) |
| 67 | + tokens = await client.list_access_tokens() |
| 68 | + |
| 69 | + # Issue a token scoped to streams under "users/1234/" |
| 70 | + issued_token = await client.issue_access_token( |
| 71 | + "user-1234-rw-token", |
| 72 | + scope=AccessTokenScope( |
| 73 | + basins=PrefixMatch(""), # all basins |
| 74 | + streams=PrefixMatch("users/1234/"), |
| 75 | + op_groups=OperationGroupPermissions( |
| 76 | + stream=Permission.READ_WRITE, |
| 77 | + ), |
| 78 | + ), |
| 79 | + expires_at=datetime(2027, 1, 1, tzinfo=timezone.utc), |
| 80 | + ) |
| 81 | + |
| 82 | + # Revoke a token |
| 83 | + await client.revoke_access_token("user-1234-rw-token") |
| 84 | + # ANCHOR_END: access-token-basic |
| 85 | + print(f"Tokens: {len(tokens.items)} found, issued: {bool(issued_token)}") |
| 86 | + |
| 87 | + # ANCHOR: pagination |
| 88 | + # Iterate through all streams with automatic pagination |
| 89 | + async for stream in basin.list_all_streams(): |
| 90 | + print(stream.name) |
| 91 | + # ANCHOR_END: pagination |
| 92 | + |
| 93 | + # ANCHOR: pagination-filtering |
| 94 | + # List streams with a prefix filter |
| 95 | + async for stream in basin.list_all_streams(prefix="events/"): |
| 96 | + print(stream.name) |
| 97 | + # ANCHOR_END: pagination-filtering |
| 98 | + |
| 99 | + # ANCHOR: pagination-deleted |
| 100 | + # Include streams that are being deleted |
| 101 | + async for stream in basin.list_all_streams(include_deleted=True): |
| 102 | + print(stream.name, stream.deleted_at) |
| 103 | + # ANCHOR_END: pagination-deleted |
| 104 | + |
| 105 | + print("Account and basins examples completed") |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + asyncio.run(main()) |
0 commit comments