Skip to content

Commit 33a4d2b

Browse files
authored
Add async client (based on aiohttp instead of requests) (#41)
1 parent 93ab395 commit 33a4d2b

File tree

20 files changed

+2077
-102
lines changed

20 files changed

+2077
-102
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
STREAM_KEY ?= NOT_EXIST
2+
STREAM_SECRET ?= NOT_EXIST
3+
4+
# These targets are not files
5+
.PHONY: help check test
6+
7+
help: ## Display this help message
8+
@echo "Please use \`make <target>\` where <target> is one of"
9+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; \
10+
{printf "\033[36m%-40s\033[0m %s\n", $$1, $$2}'
11+
12+
lint: ## Run linters
13+
black --check stream_chat
14+
pycodestyle --ignore=E501,E225,W293 stream_chat
15+
16+
test: ## Run tests
17+
STREAM_KEY=$(STREAM_KEY) STREAM_SECRET=$(STREAM_SECRET) python setup.py test
18+
19+
check: lint test ## Run linters + tests

README.md

Lines changed: 65 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,77 @@ pip install stream-chat
3636

3737
### Quickstart
3838

39+
#### Sync
40+
3941
```python
40-
chat = StreamChat(api_key="STREAM_KEY", api_secret="STREAM_SECRET")
42+
from stream_chat import StreamChat
43+
44+
45+
def main():
46+
chat = StreamChat(api_key="STREAM_KEY", api_secret="STREAM_SECRET")
47+
48+
# add a user
49+
chat.update_user({"id": "chuck", "name": "Chuck"})
4150

42-
# add a user
43-
chat.update_user({"id": "chuck", "name": "Chuck"})
51+
# create a channel about kung-fu
52+
channel = chat.channel("messaging", "kung-fu")
53+
channel.create("chuck")
4454

45-
# create a channel about kung-fu
46-
channel = chat.channel("messaging", "kung-fu")
47-
channel.create("chuck")
55+
# add a first message to the channel
56+
channel.send_message({"text": "AMA about kung-fu"}, "chuck")
4857

49-
# add a first message to the channel
50-
channel.send_message({"text": "AMA about kung-fu"}, "chuck")
58+
59+
if __name__ == '__main__':
60+
main()
61+
62+
```
63+
64+
#### Async
65+
66+
```python
67+
import asyncio
68+
from stream_chat import StreamChatAsync
69+
70+
71+
async def main():
72+
async with StreamChatAsync(api_key="STREAM_KEY", api_secret="STREAM_SECRET") as chat:
73+
# add a user
74+
await chat.update_user({"id": "chuck", "name": "Chuck"})
75+
76+
# create a channel about kung-fu
77+
channel = chat.channel("messaging", "kung-fu")
78+
await channel.create("chuck")
79+
80+
# add a first message to the channel
81+
await channel.send_message({"text": "AMA about kung-fu"}, "chuck")
82+
83+
84+
if __name__ == '__main__':
85+
loop = asyncio.get_event_loop()
86+
try:
87+
loop.run_until_complete(main())
88+
finally:
89+
loop.run_until_complete(loop.shutdown_asyncgens())
90+
loop.close()
5191

5292
```
5393

5494
### Contributing
5595

96+
Install pytest and pytest-asyncio
97+
98+
```
99+
pip install pytest
100+
pip install pytest-asyncio
101+
```
102+
56103
First, make sure you can run the test suite. Tests are run via py.test
57104

58105
```bash
59-
STREAM_KEY=my_api_key STREAM_SECRET=my_api_secret py.test stream_chat/ -v
106+
export STREAM_KEY=my_api_key
107+
export STREAM_SECRET=my_api_secret
108+
109+
make test
60110
```
61111

62112
Install black and pycodestyle
@@ -66,6 +116,12 @@ pip install black
66116
pip install pycodestyle
67117
```
68118

119+
Run linters
120+
121+
```bash
122+
make lint
123+
```
124+
69125

70126
### Releasing a new version
71127

setup.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
from setuptools import find_packages, setup
44
from setuptools.command.test import test as TestCommand
55

6-
install_requires = ["pycryptodomex>=3.8.1,<4", "requests>=2.22.0,<3", "pyjwt==1.7.1"]
6+
install_requires = [
7+
"pycryptodomex>=3.8.1,<4",
8+
"requests>=2.22.0,<3",
9+
"aiohttp>=3.6",
10+
"aiofile>=3.1,<4",
11+
"pyjwt==1.7.1",
12+
]
713
long_description = open("README.md", "r").read()
8-
tests_require = ["pytest"]
14+
tests_require = ["pytest", "pytest-asyncio"]
915

1016
about = {}
1117
with open("stream_chat/__pkg__.py") as fp:

stream_chat/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .client import StreamChat
2+
from .async_chat import StreamChatAsync

stream_chat/async_chat/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .client import StreamChatAsync

0 commit comments

Comments
 (0)