Skip to content

Commit

Permalink
Make session type pluggable
Browse files Browse the repository at this point in the history
Update readme
  • Loading branch information
bgrams committed Jun 12, 2021
1 parent f590062 commit c162f7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,26 @@ the extremely likely circumstance that these two clocks are even slightly out of
pip install fredio
```

### Development Status
Alpha. Breaking changes should be expected for minor releases until v1, so please pin versions!

### Examples

#### Standard synchronous usage

```python
"""Pipeline to process series batches on-demand"""

import fredio

# Pass an api_key here, or set as FRED_API_KEY environment variable
# This will also start a background Task for rate limiting
client = fredio.configure()

# open documentation for the /fred/series endpoint in the default browser
# Open documentation for the /fred/series endpoint in the default browser
client.series.observations.docs.open()

# create a data pipeline to request US GDP data from the /series/observations
# endpoint, clean the results, and write a csv to the local filesystem
# Request US GDP data from the /series/observations endpoint, clean
# the results, and write a csv to the local filesystem
(client.series.observations
.get_pandas(
series_id="GDP",
Expand All @@ -62,6 +66,8 @@ the main configuration function. The request Session will queue all successful H
in the form of `(name, response)`, where `name` corresponds to the final path in the URL endpoint.

```python
"""Pipeline to process series updates in near-real time"""

import asyncio
import datetime
import fredio
Expand Down Expand Up @@ -120,3 +126,6 @@ if __name__ == "__main__":
with fredio.configure(enable_events=True) as fred:
asyncio.run(main(fred))
```

### Related Projects
[fred-fdw](https://github.com/bgrams/fred-fdw) - a PostgreSQL Foreign Data Wrapper for FRED
10 changes: 7 additions & 3 deletions fredio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import os
import webbrowser
from datetime import datetime
from typing import Optional, TYPE_CHECKING
from typing import Optional, Type, TYPE_CHECKING

from aiohttp import ClientSession, ClientResponse, ClientResponseError
from yarl import URL
Expand Down Expand Up @@ -53,7 +53,11 @@ def setdefault(obj, key, value):

return instance

def __init__(self, api_key: str, **session_kws):
def __init__(self,
api_key: str,
*,
session_cls: Type[ClientSession] = ClientSession,
**session_kws):
"""
:param api_key: FRED API key
:param session_kws: Keyword arguments passed to ClientSession
Expand All @@ -66,7 +70,7 @@ def __init__(self, api_key: str, **session_kws):

# Lazy instantiation to be called within an async function
# ClientSession is cached
self._session_cls = ClientSession
self._session_cls = session_cls
self._session_kws = frozenset(session_kws.items())
self._session: Optional[ClientSession] = None

Expand Down

0 comments on commit c162f7f

Please sign in to comment.