@@ -21,7 +21,10 @@ Instantiate and use the client with the following:
2121
2222``` python
2323from agentmail import AgentMail
24- client = AgentMail(api_key = " YOUR_API_KEY" , )
24+
25+ client = AgentMail(
26+ api_key = " YOUR_API_KEY" ,
27+ )
2528client.inboxes.create()
2629```
2730
@@ -30,11 +33,19 @@ client.inboxes.create()
3033The SDK also exports an ` async ` client so that you can make non-blocking calls to our API.
3134
3235``` python
33- from agentmail import AsyncAgentMail
3436import asyncio
35- client = AsyncAgentMail(api_key = " YOUR_API_KEY" , )
37+
38+ from agentmail import AsyncAgentMail
39+
40+ client = AsyncAgentMail(
41+ api_key = " YOUR_API_KEY" ,
42+ )
43+
44+
3645async def main () -> None :
3746 await client.inboxes.create()
47+
48+
3849asyncio.run(main())
3950```
4051
@@ -45,6 +56,7 @@ will be thrown.
4556
4657``` python
4758from agentmail.core.api_error import ApiError
59+
4860try :
4961 client.inboxes.create(... )
5062except ApiError as e:
@@ -61,7 +73,10 @@ The `.with_raw_response` property returns a "raw" client that can be used to acc
6173
6274``` python
6375from agentmail import AgentMail
64- client = AgentMail(... , )
76+
77+ client = AgentMail(
78+ ... ,
79+ )
6580response = client.inboxes.with_raw_response.create(... )
6681print (response.headers) # access the response headers
6782print (response.data) # access the underlying object
@@ -94,7 +109,12 @@ The SDK defaults to a 60 second timeout. You can configure this with a timeout o
94109``` python
95110
96111from agentmail import AgentMail
97- client = AgentMail(... , timeout = 20.0 , )
112+
113+ client = AgentMail(
114+ ... ,
115+ timeout = 20.0 ,
116+ )
117+
98118
99119# Override timeout for a specific method
100120client.inboxes.create(... , request_options = {
@@ -108,9 +128,17 @@ You can override the `httpx` client to customize it for your use-case. Some comm
108128and transports.
109129
110130``` python
111- from agentmail import AgentMail
112131import httpx
113- client = AgentMail(... , httpx_client = httpx.Client(proxies = " http://my.test.proxy.example.com" , transport = httpx.HTTPTransport(local_address = " 0.0.0.0" ), ))```
132+ from agentmail import AgentMail
133+
134+ client = AgentMail(
135+ ... ,
136+ httpx_client = httpx.Client(
137+ proxies = " http://my.test.proxy.example.com" ,
138+ transport = httpx.HTTPTransport(local_address = " 0.0.0.0" ),
139+ ),
140+ )
141+ ```
114142
115143## Contributing
116144
@@ -121,3 +149,59 @@ a proof of concept, but know that we will not be able to merge it as-is. We sugg
121149an issue first to discuss with us!
122150
123151On the other hand, contributions to the README are always very welcome!
152+ ## Websockets
153+
154+ The SDK supports both sync and async websocket connections for real-time, low-latency communication. Sockets can be created using the ` connect ` method, which returns a context manager.
155+ You can either iterate through the returned ` SocketClient ` to process messages as they arrive, or attach handlers to respond to specific events.
156+
157+ ``` python
158+
159+ # Connect to the websocket (Sync)
160+ import threading
161+
162+ from agentmail import AgentMail
163+
164+ client = AgentMail(... )
165+
166+ with client.websockets.connect(... ) as socket:
167+ # Iterate over the messages as they arrive
168+ for message in socket
169+ print (message)
170+
171+ # Or, attach handlers to specific events
172+ socket.on(EventType.OPEN , lambda _ : print (" open" ))
173+ socket.on(EventType.MESSAGE , lambda message : print (" received message" , message))
174+ socket.on(EventType.CLOSE , lambda _ : print (" close" ))
175+ socket.on(EventType.ERROR , lambda error : print (" error" , error))
176+
177+
178+ # Start the listening loop in a background thread
179+ listener_thread = threading.Thread(target = socket.start_listening, daemon = True )
180+ listener_thread.start()
181+ ```
182+
183+ ``` python
184+
185+ # Connect to the websocket (Async)
186+ import asyncio
187+
188+ from agentmail import AsyncAgentMail
189+
190+ client = AsyncAgentMail(... )
191+
192+ async with client.websockets.connect(... ) as socket:
193+ # Iterate over the messages as they arrive
194+ async for message in socket
195+ print (message)
196+
197+ # Or, attach handlers to specific events
198+ socket.on(EventType.OPEN , lambda _ : print (" open" ))
199+ socket.on(EventType.MESSAGE , lambda message : print (" received message" , message))
200+ socket.on(EventType.CLOSE , lambda _ : print (" close" ))
201+ socket.on(EventType.ERROR , lambda error : print (" error" , error))
202+
203+
204+ # Start listening for events in an asyncio task
205+ listen_task = asyncio.create_task(socket.start_listening())
206+ ```
207+
0 commit comments