Skip to content

Commit b352436

Browse files
authored
Make aiohttp ClientSession optional (bachya#150)
1 parent 7f2151d commit b352436

21 files changed

Lines changed: 223 additions & 177 deletions

‎docs/advanced.rst‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ when it comes time to re-authenticate, simply:
3939
.. code:: python
4040
4141
simplisafe = await simplipy.API.login_via_token(
42-
"<REFRESH TOKEN>", websession
42+
"<REFRESH TOKEN>", session=session
4343
)
4444
4545
During usage, ``simplipy`` will automatically refresh the access token as needed.
@@ -48,7 +48,7 @@ At any point, the "dirtiness" of the token can be checked:
4848
.. code:: python
4949
5050
simplisafe = await simplipy.API.login_via_token(
51-
"<REFRESH TOKEN>", websession
51+
"<REFRESH TOKEN>", session=session
5252
)
5353
5454
# Assuming the access token was automatically refreshed:

‎docs/system.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ To get all SimpliSafe™ systems associated with an account:
2525
2626
async def main() -> None:
2727
"""Create the aiohttp session and run."""
28-
async with ClientSession() as websession:
28+
async with ClientSession() as session:
2929
simplisafe = await simplipy.API.login_via_credentials(
30-
"<EMAIL>", "<PASSWORD>", websession
30+
"<EMAIL>", "<PASSWORD>", session=session
3131
)
3232
3333
# Get a dict of systems with the system ID as the key:
3434
systems = await simplisafe.get_systems()
3535
# >>> {"1234abc": <simplipy.system.SystemV2 object>, ...}
3636
3737
38-
asyncio.get_event_loop().run_until_complete(main())
38+
asyncio.run(main())
3939
4040
Core Properties
4141
---------------

‎docs/usage.rst‎

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ Please note that only Interactive plans can access sensor values and set the
3636
system state; using the API with a Standard plan will be limited to retrieving
3737
the current system state.
3838

39-
Scaffolding
40-
-----------
39+
Connection Pooling
40+
------------------
4141

42-
Installing ``simplipy`` also includes ``aiohttp``, which is the engine that powers
43-
the entire library. All interactions with ``simplipy`` start within an
44-
``aiohttp`` ``ClientSession``:
42+
By default, the :meth:`API <simplipy.api.API>` object creates a new connection to
43+
SimpliSafe™ with each coroutine. If you are calling a large number of coroutines (or
44+
merely want to squeeze out every second of runtime savings possible), an
45+
``aiohttp ClientSession`` can be supplied when logging into the API (via credentials or
46+
token) to achieve connection pooling:
4547

4648
.. code:: python
4749
@@ -53,10 +55,14 @@ the entire library. All interactions with ``simplipy`` start within an
5355
5456
async def main() -> None:
5557
"""Create the aiohttp session and run."""
56-
async with ClientSession() as websession:
57-
# YOUR CODE HERE
58+
async with ClientSession() as session:
59+
simplisafe = await API.login_via_credentials(
60+
"<EMAIL>", "<PASSWORD>", session=session
61+
)
5862
63+
# ...
5964
60-
asyncio.get_event_loop().run_until_complete(main())
6165
62-
Every example within this documentation assumes the use of the above scaffolding.
66+
asyncio.run(main())
67+
68+
Every example in this documentation uses this pattern.

‎docs/websocket.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ events from a user's SimpliSafe™ system. This websocket can be accessed via th
88
.. code:: python
99
1010
simplisafe = await simplipy.API.login_via_credentials(
11-
"<EMAIL>", "<PASSWORD>", websession
11+
"<EMAIL>", "<PASSWORD>", session=session
1212
)
1313
1414
simplisafe.websocket

‎examples/test_events.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
async def main() -> None:
1717
"""Create the aiohttp session and run the example."""
18-
async with ClientSession() as websession:
18+
async with ClientSession() as session:
1919
logging.basicConfig(level=logging.INFO)
2020

2121
try:
2222
simplisafe = await API.login_via_credentials(
23-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
23+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
2424
)
2525
systems = await simplisafe.get_systems()
2626
for system in systems.values():
@@ -31,4 +31,4 @@ async def main() -> None:
3131
_LOGGER.error(err)
3232

3333

34-
asyncio.get_event_loop().run_until_complete(main())
34+
asyncio.run(main())

‎examples/test_locks.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
async def main() -> None:
1818
"""Create the aiohttp session and run the example."""
19-
async with ClientSession() as websession:
19+
async with ClientSession() as session:
2020
logging.basicConfig(level=logging.INFO)
2121

2222
try:
2323
simplisafe = await API.login_via_credentials(
24-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
24+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
2525
)
2626
systems = await simplisafe.get_systems()
2727
for system in systems.values():
@@ -45,4 +45,4 @@ async def main() -> None:
4545
_LOGGER.error(err)
4646

4747

48-
asyncio.get_event_loop().run_until_complete(main())
48+
asyncio.run(main())

‎examples/test_sensor_properties.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
async def main() -> None:
1717
"""Create the aiohttp session and run the example."""
18-
async with ClientSession() as websession:
18+
async with ClientSession() as session:
1919
logging.basicConfig(level=logging.INFO)
2020

2121
try:
2222
simplisafe = await API.login_via_credentials(
23-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
23+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
2424
)
2525
systems = await simplisafe.get_systems()
2626
for system_id, system in systems.items():
@@ -45,4 +45,4 @@ async def main() -> None:
4545
_LOGGER.error(err)
4646

4747

48-
asyncio.get_event_loop().run_until_complete(main())
48+
asyncio.run(main())

‎examples/test_sensor_types.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
async def main() -> None:
1717
"""Create the aiohttp session and run the example."""
18-
async with ClientSession() as websession:
18+
async with ClientSession() as session:
1919
logging.basicConfig(level=logging.INFO)
2020

2121
try:
2222
simplisafe = await API.login_via_credentials(
23-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
23+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
2424
)
2525
systems = await simplisafe.get_systems()
2626
for system in systems.values():
@@ -36,4 +36,4 @@ async def main() -> None:
3636
_LOGGER.error(err)
3737

3838

39-
asyncio.get_event_loop().run_until_complete(main())
39+
asyncio.run(main())

‎examples/test_system_state.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
async def main() -> None:
1717
"""Create the aiohttp session and run the example."""
18-
async with ClientSession() as websession:
18+
async with ClientSession() as session:
1919
logging.basicConfig(level=logging.INFO)
2020

2121
try:
2222
simplisafe = await API.login_via_credentials(
23-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
23+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
2424
)
2525
systems = await simplisafe.get_systems()
2626
for system in systems.values():
@@ -31,4 +31,4 @@ async def main() -> None:
3131
_LOGGER.error(err)
3232

3333

34-
asyncio.get_event_loop().run_until_complete(main())
34+
asyncio.run(main())

‎examples/test_websocket.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ def print_hello():
3030

3131
async def main() -> None:
3232
"""Create the aiohttp session and run the example."""
33-
async with ClientSession() as websession:
33+
async with ClientSession() as session:
3434
logging.basicConfig(level=logging.INFO)
3535

3636
try:
3737
simplisafe = await API.login_via_credentials(
38-
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, websession
38+
SIMPLISAFE_EMAIL, SIMPLISAFE_PASSWORD, session=session
3939
)
4040

4141
simplisafe.websocket.on_connect(print_hello)
@@ -57,4 +57,4 @@ async def main() -> None:
5757
_LOGGER.error(err)
5858

5959

60-
asyncio.get_event_loop().run_until_complete(main())
60+
asyncio.run(main())

0 commit comments

Comments
 (0)