-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_client.py
More file actions
136 lines (106 loc) · 4.3 KB
/
Copy pathasync_client.py
File metadata and controls
136 lines (106 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Asynchronous client for the Bloomy API."""
from __future__ import annotations
from typing import TYPE_CHECKING, Self
import httpx
from .configuration import Configuration
from .exceptions import ConfigurationError
if TYPE_CHECKING:
from types import TracebackType
from .operations.async_ import (
AsyncGoalOperations,
AsyncHeadlineOperations,
AsyncIssueOperations,
AsyncMeetingOperations,
AsyncScorecardOperations,
AsyncTodoOperations,
AsyncUserOperations,
)
class AsyncClient:
"""Asynchronous client for interacting with the Bloomy API.
This client provides async access to all Bloomy API operations including
users, meetings, todos, goals, headlines, issues, and scorecards.
Args:
api_key: The API key for authentication. If not provided, it will be loaded
from environment variables or configuration files.
base_url: The base URL for the API. Defaults to the production API URL.
Example:
Using the async client with context manager:
```python
import asyncio
from bloomy import AsyncClient
async def main():
async with AsyncClient(api_key="your-api-key") as client:
user = await client.user.details()
print(user.name)
asyncio.run(main())
```
Without context manager:
```python
client = AsyncClient(api_key="your-api-key")
user = await client.user.details()
await client.close()
```
"""
def __init__(
self,
api_key: str | None = None,
base_url: str = "https://app.bloomgrowth.com/api/v1",
timeout: float = 30.0,
) -> None:
"""Initialize the async Bloomy client.
Args:
api_key: The API key for authentication.
base_url: The base URL for the API.
timeout: The timeout in seconds for HTTP requests. Defaults to 30.0.
Raises:
ConfigurationError: If no API key is provided or found in configuration.
"""
config = Configuration(api_key=api_key)
if not config.api_key:
raise ConfigurationError(
"No API key provided. Set it explicitly, via BG_API_KEY "
"environment variable, or in ~/.bloomy/config.yaml configuration file."
)
self._client = httpx.AsyncClient(
base_url=base_url,
headers={
"Authorization": f"Bearer {config.api_key}",
"Content-Type": "application/json",
},
timeout=timeout,
)
# Lazy imports to avoid circular dependencies
from .operations.async_.goals import AsyncGoalOperations
from .operations.async_.headlines import AsyncHeadlineOperations
from .operations.async_.issues import AsyncIssueOperations
from .operations.async_.meetings import AsyncMeetingOperations
from .operations.async_.scorecard import AsyncScorecardOperations
from .operations.async_.todos import AsyncTodoOperations
from .operations.async_.users import AsyncUserOperations
self.user: AsyncUserOperations = AsyncUserOperations(self._client)
self.meeting: AsyncMeetingOperations = AsyncMeetingOperations(self._client)
self.todo: AsyncTodoOperations = AsyncTodoOperations(self._client)
self.goal: AsyncGoalOperations = AsyncGoalOperations(self._client)
self.headline: AsyncHeadlineOperations = AsyncHeadlineOperations(self._client)
self.issue: AsyncIssueOperations = AsyncIssueOperations(self._client)
self.scorecard: AsyncScorecardOperations = AsyncScorecardOperations(
self._client
)
async def __aenter__(self) -> Self:
"""Enter the async context manager.
Returns:
The async client instance.
"""
await self._client.__aenter__()
return self
async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_val: BaseException | None,
exc_tb: TracebackType | None,
) -> None:
"""Exit the async context manager."""
await self._client.__aexit__(exc_type, exc_val, exc_tb)
async def close(self) -> None:
"""Close the HTTP client."""
await self._client.aclose()