Skip to content

Commit 3876c3e

Browse files
clickingbuttonsDarcy-Linde
andauthored
Websockets (#153)
* fix releasing (7) * initial working * checkpoint * sub/unsub while connected * handle special topic.* case * Websocket Models (#152) * websocket test scaffolding * cleanup * don't lint 3.7 * don't unit test 3.7 * lint * better parsing status messages * fix docs * cleanup * fix doc * lint Co-authored-by: Darcy Linde <[email protected]>
1 parent 6cb7498 commit 3876c3e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1023
-28
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
python-version: ['3.7', '3.8', '3.9', '3.10']
17+
python-version: ['3.8', '3.9', '3.10']
1818
name: Lint ${{ matrix.python-version }}
1919
steps:
2020
- uses: actions/checkout@v3

.github/workflows/release.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ jobs:
2323
run: poetry config pypi-token.pypi ${{ secrets.POETRY_HTTP_BASIC_PYPI_PASSWORD }}
2424
- name: Install pypi deps
2525
run: poetry install
26+
- name: Get tag
27+
id: tag
28+
uses: dawidd6/action-get-tag@v1
2629
- name: Version according to tag
27-
run: poetry version $(git describe --tags)
30+
run: poetry version ${{ steps.tag.outputs.tag }}
2831
- name: Build
2932
run: poetry build
3033
- name: Publish to PyPi

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jobs:
1414
strategy:
1515
fail-fast: false
1616
matrix:
17-
python-version: ['3.7', '3.8', '3.9', '3.10']
18-
name: Lint ${{ matrix.python-version }}
17+
python-version: ['3.8', '3.9', '3.10']
18+
name: Unit test ${{ matrix.python-version }}
1919
steps:
2020
- uses: actions/checkout@v3
2121
- name: Setup Python

Makefile

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ YELLOW := $(shell tput -Txterm setaf 3)
66
WHITE := $(shell tput -Txterm setaf 7)
77
RESET := $(shell tput -Txterm sgr0)
88

9-
.PHONY: help lint style static test
9+
.PHONY: help lint style static test test_rest test_websocket
1010

1111
## Show help
1212
help:
@@ -22,16 +22,21 @@ help:
2222

2323
## Check code style
2424
style:
25-
poetry run black $(if $(CI),--check,) polygon tests
25+
poetry run black $(if $(CI),--check,) polygon test_*
2626

2727
## Check static types
2828
static:
29-
poetry run mypy polygon tests
29+
poetry run mypy polygon test_*
3030

3131
## Check code style and static types
3232
lint: style static
3333

3434
## Run unit tests
35-
test:
36-
poetry run python -m unittest discover -s tests
35+
test_rest:
36+
poetry run python -m unittest discover -s test_rest
37+
38+
test_websocket:
39+
poetry run python -m unittest discover -s test_websocket
40+
41+
test: test_rest test_websocket
3742

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,20 @@ from polygon import RESTClient
4141
client = RESTClient() # Uses POLYGON_API_KEY env var. Can optionally supply your key.
4242
response = client.get_aggs("AAPL", 1, "day", "2005-04-01", "2005-04-04", raw=True)
4343
```
44+
45+
### Streaming websockets
46+
47+
```python
48+
from polygon import WebSocketClient
49+
from polygon.websocket.models import Market, Feed, WebSocketMessage
50+
import asyncio
51+
52+
client = WebSocketClient(market=Market.Stocks, feed=Feed.RealTime) # Uses POLYGON_API_KEY env var. Can optionally supply your key.
53+
client.subscribe('T.AAPL')
54+
55+
def handle_msg(msg: WebSocketMessage):
56+
print(msg)
57+
58+
asyncio.run(client.connect(handle_msg))
59+
```
60+

docs/source/Getting-Started.rst

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,18 @@ If it is a paginated :code:`list_*` response it's up to you to handle the "next_
173173
Websocket client usage
174174
----------------------
175175

176-
Coming soon.
176+
.. code-block:: python
177+
178+
from polygon import WebSocketClient
179+
from polygon.websocket.models import Market, Feed, WebSocketMessage
180+
from typing import List
181+
import asyncio
182+
183+
client = WebSocketClient(market=Market.Stocks, feed=Feed.RealTime) # Uses POLYGON_API_KEY env var. Can optionally supply your key.
184+
client.subscribe('T.AAPL')
185+
186+
async def handle_msg(msg: List[WebSocketMessage]):
187+
print(msg)
188+
189+
asyncio.run(client.connect(handle_msg))
190+

docs/source/WebSockets.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. _websockets_header:
2+
3+
WebSockets
4+
==========
5+
6+
===========
7+
Init client
8+
===========
9+
.. automethod:: polygon.WebSocketClient.__init__
10+
11+
============================
12+
Connect
13+
============================
14+
.. automethod:: polygon.WebSocketClient.connect
15+
16+
============================
17+
Subscribe
18+
============================
19+
.. automethod:: polygon.WebSocketClient.subscribe
20+
21+
============================
22+
Unsubscribe
23+
============================
24+
.. automethod:: polygon.WebSocketClient.unsubscribe
25+
26+
============================
27+
Close
28+
============================
29+
.. automethod:: polygon.WebSocketClient.close
30+

docs/source/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This documentation is for the Python client only. For details about the response
99

1010
Getting-Started
1111
Aggs
12+
WebSockets
1213
Snapshot
1314
Quotes
1415
Reference

poetry.lock

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

polygon/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
from .rest import RESTClient
2+
from .websocket import WebSocketClient

0 commit comments

Comments
 (0)