Skip to content

Commit 1343f43

Browse files
authored
feat: swappable http client (#102)
1 parent e7707ea commit 1343f43

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

stream_chat/async_chat/client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ def __init__(
4949
connector=aiohttp.TCPConnector(keepalive_timeout=59.0),
5050
)
5151

52+
def set_http_session(self, session: aiohttp.ClientSession) -> None:
53+
"""
54+
You can use your own `aiohttp.ClientSession` instance. This instance
55+
will be used for underlying HTTP requests.
56+
Make sure you set up a `base_url` for the session.
57+
"""
58+
self.session = session
59+
5260
async def _parse_response(self, response: aiohttp.ClientResponse) -> StreamResponse:
5361
text = await response.text()
5462
try:

stream_chat/client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ def __init__(
3636
self.session.mount("http://", requests.adapters.HTTPAdapter(max_retries=1))
3737
self.session.mount("https://", requests.adapters.HTTPAdapter(max_retries=1))
3838

39+
def set_http_session(self, session: requests.Session) -> None:
40+
"""
41+
You can use your own `requests.Session` instance. This instance
42+
will be used for underlying HTTP requests.
43+
"""
44+
self.session = session
45+
3946
def _parse_response(self, response: requests.Response) -> StreamResponse:
4047
try:
4148
parsed_result = json.loads(response.text) if response.text else {}

stream_chat/tests/async_chat/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import sys
34
import time
45
import uuid
@@ -7,6 +8,7 @@
78
from operator import itemgetter
89
from typing import Dict, List
910

11+
import aiohttp
1012
import jwt
1113
import pytest
1214

@@ -716,3 +718,18 @@ async def test_stream_response(self, client: StreamChatAsync):
716718
assert rate_limit.limit > 0
717719
assert rate_limit.remaining > 0
718720
assert type(rate_limit.reset) is datetime
721+
722+
async def test_swap_http_client(self):
723+
client = StreamChatAsync(
724+
api_key=os.environ["STREAM_KEY"], api_secret=os.environ["STREAM_SECRET"]
725+
)
726+
727+
client.set_http_session(aiohttp.ClientSession(base_url="https://getstream.io"))
728+
with pytest.raises(StreamAPIException):
729+
await client.get_app_settings()
730+
731+
client.set_http_session(
732+
aiohttp.ClientSession(base_url="https://chat.stream-io-api.com")
733+
)
734+
resp = await client.get_app_settings()
735+
assert resp.status_code() == 200

stream_chat/tests/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import sys
34
import time
45
import uuid
@@ -9,6 +10,7 @@
910

1011
import jwt
1112
import pytest
13+
import requests
1214

1315
from stream_chat import StreamChat
1416
from stream_chat.base.exceptions import StreamAPIException
@@ -689,3 +691,18 @@ def test_stream_response(self, client: StreamChat):
689691
assert rate_limit.limit > 0
690692
assert rate_limit.remaining > 0
691693
assert type(rate_limit.reset) is datetime
694+
695+
def test_swap_http_client(self):
696+
client = StreamChat(
697+
api_key=os.environ["STREAM_KEY"], api_secret=os.environ["STREAM_SECRET"]
698+
)
699+
700+
session = requests.Session()
701+
session.proxies = {"https": "https://getstream.io"}
702+
client.set_http_session(session)
703+
with pytest.raises(requests.exceptions.ProxyError):
704+
client.get_app_settings()
705+
706+
client.set_http_session(requests.Session())
707+
resp = client.get_app_settings()
708+
assert resp.status_code() == 200

0 commit comments

Comments
 (0)