|
1 | | -"""Basic usage examples for the API SDK.""" |
| 1 | +"""Basic usage examples for the API SDK (Project info).""" |
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import os |
5 | 5 | from plane import Client, Config, SyncClient |
6 | | -from plane.models.user import UserCreate, UserUpdate |
7 | 6 |
|
8 | 7 |
|
9 | 8 | async def async_example(): |
10 | | - """Example using async client.""" |
11 | | - # Initialize client with config |
| 9 | + """Example using async client for projects.""" |
12 | 10 | config = Config( |
13 | 11 | api_key=os.getenv("MY_API_KEY", "your-api-key"), |
14 | 12 | base_url="https://api.plane.so/api/v1", |
15 | 13 | ) |
16 | 14 |
|
17 | | - # Or initialize from environment variables |
18 | | - # client = Client() # Uses MY_API_KEY env var |
19 | | - |
20 | 15 | async with Client(config) as client: |
21 | 16 | # Check API health |
22 | 17 | health = await client.health_check() |
23 | 18 | print(f"API Health: {health}") |
24 | 19 |
|
25 | | - # List users |
26 | | - result = await client.users.list(page=1, per_page=10) |
27 | | - users = result["users"] |
28 | | - pagination = result["pagination"] |
29 | | - |
| 20 | + # List workspaces |
| 21 | + ws_result = await client.workspaces.list(page=1, per_page=5) |
| 22 | + workspaces = ws_result["workspaces"] |
| 23 | + if not workspaces: |
| 24 | + print("No workspaces found.") |
| 25 | + return |
| 26 | + workspace = workspaces[0] |
| 27 | + print(f"Using workspace: {workspace.name} (slug: {workspace.slug})") |
| 28 | + |
| 29 | + # List projects in the workspace |
| 30 | + proj_result = await client.projects.list(workspace.slug, page=1, per_page=10) |
| 31 | + projects = proj_result["projects"] |
| 32 | + pagination = proj_result["pagination"] |
30 | 33 | print( |
31 | | - f"Found {len(users)} users (page {pagination.page} of {pagination.total_pages})" |
32 | | - ) |
33 | | - |
34 | | - # Get a specific user |
35 | | - if users: |
36 | | - user = await client.users.get(users[0].id) |
37 | | - print(f"User: {user.full_name} ({user.email})") |
38 | | - |
39 | | - # Create a new user |
40 | | - new_user_data = UserCreate( |
41 | | - |
42 | | - firstName="New", |
43 | | - lastName="User", |
44 | | - password="securepassword123", |
| 34 | + f"Found {len(projects)} projects (page {pagination.page} of {pagination.total_pages})" |
45 | 35 | ) |
| 36 | + for project in projects: |
| 37 | + print(f"- {project.name} (id: {project.id}, desc: {project.description})") |
46 | 38 |
|
47 | | - try: |
48 | | - new_user = await client.users.create(new_user_data) |
49 | | - print(f"Created user: {new_user.id}") |
50 | | - |
51 | | - # Update the user |
52 | | - update_data = UserUpdate(firstName="Updated") |
53 | | - updated_user = await client.users.update(new_user.id, update_data) |
54 | | - print(f"Updated user: {updated_user.full_name}") |
55 | | - |
56 | | - # Search users |
57 | | - search_results = await client.users.search("Updated", limit=5) |
58 | | - print(f"Search found {len(search_results)} users") |
59 | | - |
60 | | - # Delete the user |
61 | | - await client.users.delete(new_user.id) |
62 | | - print("User deleted") |
63 | | - |
64 | | - except Exception as e: |
65 | | - print(f"Error: {e}") |
| 39 | + # Get a specific project (first one) |
| 40 | + if projects: |
| 41 | + project = await client.projects.get(workspace.slug, projects[0].id) |
| 42 | + print( |
| 43 | + f"Project details: {project.name} (id: {project.id}) - {project.description}" |
| 44 | + ) |
66 | 45 |
|
67 | 46 |
|
68 | 47 | def sync_example(): |
69 | | - """Example using synchronous client.""" |
| 48 | + """Example using synchronous client for projects.""" |
70 | 49 | config = Config( |
71 | 50 | api_key=os.getenv("MY_API_KEY", "your-api-key"), |
72 | | - base_url="https://api.example.com/v1", |
| 51 | + base_url="https://api.plane.so/api/v1", |
73 | 52 | ) |
74 | 53 |
|
75 | 54 | with SyncClient(config) as client: |
76 | 55 | # Check API health |
77 | 56 | health = client.health_check() |
78 | 57 | print(f"API Health: {health}") |
79 | 58 |
|
80 | | - # List users |
81 | | - result = client.users.list(page=1, per_page=5) |
82 | | - users = result["users"] |
83 | | - |
84 | | - print(f"Found {len(users)} users") |
85 | | - |
86 | | - # Create and manage a user |
87 | | - new_user_data = UserCreate( |
88 | | - |
89 | | - firstName="Sync", |
90 | | - lastName="User", |
91 | | - password="securepassword123", |
| 59 | + # List workspaces |
| 60 | + ws_result = client.workspaces.list(page=1, per_page=5) |
| 61 | + workspaces = ws_result["workspaces"] |
| 62 | + if not workspaces: |
| 63 | + print("No workspaces found.") |
| 64 | + return |
| 65 | + workspace = workspaces[0] |
| 66 | + print(f"Using workspace: {workspace.name} (slug: {workspace.slug})") |
| 67 | + |
| 68 | + # List projects in the workspace |
| 69 | + proj_result = client.projects.list(workspace.slug, page=1, per_page=10) |
| 70 | + projects = proj_result["projects"] |
| 71 | + pagination = proj_result["pagination"] |
| 72 | + print( |
| 73 | + f"Found {len(projects)} projects (page {pagination.page} of {pagination.total_pages})" |
92 | 74 | ) |
93 | | - |
94 | | - try: |
95 | | - new_user = client.users.create(new_user_data) |
96 | | - print(f"Created user: {new_user.full_name}") |
97 | | - |
98 | | - # Clean up |
99 | | - client.users.delete(new_user.id) |
100 | | - print("User deleted") |
101 | | - |
102 | | - except Exception as e: |
103 | | - print(f"Error: {e}") |
| 75 | + for project in projects: |
| 76 | + print(f"- {project.name} (id: {project.id}, desc: {project.description})") |
| 77 | + |
| 78 | + # Get a specific project (first one) |
| 79 | + if projects: |
| 80 | + project = client.projects.get(workspace.slug, projects[0].id) |
| 81 | + print( |
| 82 | + f"Project details: {project.name} (id: {project.id}) - {project.description}" |
| 83 | + ) |
104 | 84 |
|
105 | 85 |
|
106 | 86 | if __name__ == "__main__": |
|
0 commit comments