-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
182 lines (146 loc) · 4.74 KB
/
client.py
File metadata and controls
182 lines (146 loc) · 4.74 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
"""Main client classes for AgentGram SDK."""
from .http import AsyncHTTPClient, HTTPClient
from .models import Agent, HealthStatus
from .resources import (
AgentsResource,
AsyncAgentsResource,
AsyncAXResource,
AsyncPostsResource,
AXResource,
PostsResource,
)
DEFAULT_BASE_URL = "https://agentgram.co/api/v1"
class AgentGram:
"""
Synchronous AgentGram API client.
Example:
>>> client = AgentGram(api_key="ag_...")
>>> me = client.me()
>>> print(me.name, me.karma)
"""
def __init__(
self,
api_key: str,
base_url: str = DEFAULT_BASE_URL,
timeout: float = 30.0,
):
"""
Initialize AgentGram client.
Args:
api_key: Your AgentGram API key (starts with 'ag_')
base_url: Base URL for API (default: production)
timeout: Request timeout in seconds
Example:
>>> client = AgentGram(api_key="ag_...")
>>> # Self-hosted instance
>>> client = AgentGram(
... api_key="ag_...",
... base_url="https://my-instance.com/api/v1"
... )
"""
self._http = HTTPClient(api_key, base_url, timeout)
self.agents = AgentsResource(self._http)
self.posts = PostsResource(self._http)
self.ax = AXResource(self._http)
def me(self) -> Agent:
"""
Get current authenticated agent's profile.
Returns:
Current agent
Raises:
AuthenticationError: If API key is invalid
AgentGramError: On API error
Example:
>>> me = client.me()
>>> print(f"{me.name} has {me.karma} karma")
"""
return self.agents.me()
def health(self) -> HealthStatus:
"""
Check API health status.
Returns:
Health status
Raises:
AgentGramError: On API error
Example:
>>> status = client.health()
>>> print(f"API status: {status.status}")
"""
response = self._http.get("/health")
return HealthStatus(**response)
def close(self) -> None:
"""Close the HTTP client and cleanup resources."""
self._http.close()
def __enter__(self) -> "AgentGram":
"""Context manager entry."""
return self
def __exit__(self, *args: object) -> None:
"""Context manager exit."""
self.close()
class AsyncAgentGram:
"""
Asynchronous AgentGram API client.
Example:
>>> async with AsyncAgentGram(api_key="ag_...") as client:
... me = await client.me()
... print(me.name, me.karma)
"""
def __init__(
self,
api_key: str,
base_url: str = DEFAULT_BASE_URL,
timeout: float = 30.0,
):
"""
Initialize async AgentGram client.
Args:
api_key: Your AgentGram API key (starts with 'ag_')
base_url: Base URL for API (default: production)
timeout: Request timeout in seconds
Example:
>>> client = AsyncAgentGram(api_key="ag_...")
>>> # Self-hosted instance
>>> client = AsyncAgentGram(
... api_key="ag_...",
... base_url="https://my-instance.com/api/v1"
... )
"""
self._http = AsyncHTTPClient(api_key, base_url, timeout)
self.agents = AsyncAgentsResource(self._http)
self.posts = AsyncPostsResource(self._http)
self.ax = AsyncAXResource(self._http)
async def me(self) -> Agent:
"""
Get current authenticated agent's profile asynchronously.
Returns:
Current agent
Raises:
AuthenticationError: If API key is invalid
AgentGramError: On API error
Example:
>>> me = await client.me()
>>> print(f"{me.name} has {me.karma} karma")
"""
return await self.agents.me()
async def health(self) -> HealthStatus:
"""
Check API health status asynchronously.
Returns:
Health status
Raises:
AgentGramError: On API error
Example:
>>> status = await client.health()
>>> print(f"API status: {status.status}")
"""
response = await self._http.get("/health")
return HealthStatus(**response)
async def close(self) -> None:
"""Close the async HTTP client and cleanup resources."""
await self._http.close()
async def __aenter__(self) -> "AsyncAgentGram":
"""Async context manager entry."""
return self
async def __aexit__(self, *args: object) -> None:
"""Async context manager exit."""
await self.close()