Skip to content

Commit c12fb8c

Browse files
authored
Release/0.1.2 beta (#19)
* new custom return type for JsonOrXml, annotate all existing APIs * bump lib to 0.1.2-beta * use TypeVar instead of NewType * include explicit package exports for api * add new classifiers and badges to README and pyproject.toml
1 parent a8fce98 commit c12fb8c

File tree

9 files changed

+64
-41
lines changed

9 files changed

+64
-41
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![PyPI version](https://badge.fury.io/py/fred-py-api.svg)](https://badge.fury.io/py/fred-py-api)
2+
[![Supported Python Versions](https://img.shields.io/pypi/pyversions/fred-py-api?style=flat)](https://pypi.org/project/fred-py-api/)
13
[![codecov](https://codecov.io/gh/zachspar/fred-py-api/branch/main/graph/badge.svg?token=BG1948D8Y7)](https://codecov.io/gh/zachspar/fred-py-api)
24

35
# Fred Python API

pyproject.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "fred_py_api"
7-
version = "0.1.1"
7+
version = "0.1.2"
88
authors = [
99
{ name="Zachary Spar", email="[email protected]" },
1010
]
@@ -13,8 +13,14 @@ readme = "README.md"
1313
requires-python = ">=3.6"
1414
classifiers = [
1515
"Development Status :: 4 - Beta",
16+
"Intended Audience :: Developers",
17+
"Intended Audience :: Financial and Insurance Industry",
1618
"Intended Audience :: Science/Research",
17-
"Programming Language :: Python :: 3",
19+
"Programming Language :: Python :: 3.6",
20+
"Programming Language :: Python :: 3.7",
21+
"Programming Language :: Python :: 3.8",
22+
"Programming Language :: Python :: 3.9",
23+
"Programming Language :: Python :: 3.10",
1824
"License :: OSI Approved :: MIT License",
1925
"Operating System :: OS Independent",
2026
]

src/fred/api/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ class FredAPI(FredAPISeries, FredAPIReleases, FredAPICategories, FredAPISources,
1717
"""
1818

1919
pass
20+
21+
22+
__all__ = [
23+
"BaseFredAPIError",
24+
"FredAPIRequestError",
25+
"FredAPI",
26+
"FredAPICategories",
27+
"FredAPIReleases",
28+
"FredAPISeries",
29+
"FredAPISources",
30+
"FredAPITags",
31+
]

src/fred/api/_fred_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,17 @@
44
"""
55
from http import HTTPStatus
66
from os import environ
7-
from typing import Optional, Union, Dict
7+
from typing import Optional, Dict, TypeVar
88
from xml.etree import ElementTree
99

1010
import requests
1111

1212
from .exceptions import FredAPIRequestError
1313

1414

15+
JsonOrXml = TypeVar("JsonOrXml", Dict, ElementTree.Element)
16+
17+
1518
class FredClient(object):
1619
"""Base Fred API."""
1720

@@ -42,7 +45,7 @@ def get_api_key(self) -> str:
4245
"""Get API key."""
4346
return self._api_key
4447

45-
def _get(self, endpoint: str = None, payload: dict = None) -> Union[Dict, ElementTree.Element]:
48+
def _get(self, endpoint: str = None, payload: dict = None) -> JsonOrXml:
4649
"""Invoke client get request."""
4750
if not payload:
4851
payload = {}

src/fred/api/categories.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Fred API for Categories Namespace.
44
"""
5-
from ._fred_client import FredClient
5+
from ._fred_client import FredClient, JsonOrXml
66
from .._util.decorators import validate_api_args
77

88

@@ -20,7 +20,7 @@ class FredAPICategories(FredClient):
2020
"""
2121

2222
@validate_api_args("api_key", "file_type", "category_id")
23-
def get_category(self, category_id: int = None, **kwargs) -> dict:
23+
def get_category(self, category_id: int = None, **kwargs) -> JsonOrXml:
2424
"""Get category by ID. https://fred.stlouisfed.org/docs/api/fred/category.html"""
2525
return self._get(
2626
f"category",
@@ -45,7 +45,7 @@ def get_category(self, category_id: int = None, **kwargs) -> dict:
4545
"tag_names",
4646
"exclude_tag_names",
4747
)
48-
def get_category_series(self, category_id: int, **kwargs) -> dict:
48+
def get_category_series(self, category_id: int, **kwargs) -> JsonOrXml:
4949
"""Get category series by category ID. https://fred.stlouisfed.org/docs/api/fred/category_series.html"""
5050
return self._get(
5151
f"category/series",
@@ -56,7 +56,7 @@ def get_category_series(self, category_id: int, **kwargs) -> dict:
5656
)
5757

5858
@validate_api_args("api_key", "file_type", "category_id", "realtime_start", "realtime_end")
59-
def get_category_children(self, category_id: int = None, **kwargs) -> dict:
59+
def get_category_children(self, category_id: int = None, **kwargs) -> JsonOrXml:
6060
"""Get category children by category ID. https://fred.stlouisfed.org/docs/api/fred/category_children.html"""
6161
return self._get(
6262
f"category/children",
@@ -67,7 +67,7 @@ def get_category_children(self, category_id: int = None, **kwargs) -> dict:
6767
)
6868

6969
@validate_api_args("api_key", "file_type", "category_id", "realtime_start", "realtime_end")
70-
def get_category_related(self, category_id: int, **kwargs) -> dict:
70+
def get_category_related(self, category_id: int, **kwargs) -> JsonOrXml:
7171
"""Get category related by category ID. https://fred.stlouisfed.org/docs/api/fred/category_related.html"""
7272
return self._get(
7373
f"category/related",
@@ -97,7 +97,7 @@ def get_category_related(self, category_id: int, **kwargs) -> dict:
9797
"order_by",
9898
"sort_order",
9999
)
100-
def get_category_tags(self, category_id: int, **kwargs) -> dict:
100+
def get_category_tags(self, category_id: int, **kwargs) -> JsonOrXml:
101101
"""Get category tags by category ID. https://fred.stlouisfed.org/docs/api/fred/category_tags.html"""
102102
return self._get(
103103
f"category/tags",
@@ -128,7 +128,7 @@ def get_category_tags(self, category_id: int, **kwargs) -> dict:
128128
"order_by",
129129
"sort_order",
130130
)
131-
def get_category_related_tags(self, category_id: int, tag_names: str, **kwargs) -> dict:
131+
def get_category_related_tags(self, category_id: int, tag_names: str, **kwargs) -> JsonOrXml:
132132
"""Get category related tags by category ID and tag names. https://fred.stlouisfed.org/docs/api/fred/category_related_tags.html"""
133133
return self._get(
134134
f"category/related_tags",

src/fred/api/releases.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Fred API for Releases Namespace.
44
"""
5-
from ._fred_client import FredClient
5+
from ._fred_client import FredClient, JsonOrXml
66
from .._util.decorators import validate_api_args
77

88

@@ -25,7 +25,7 @@ class FredAPIReleases(FredClient):
2525
@validate_api_args(
2626
"api_key", "file_type", "realtime_start", "realtime_end", "limit", "offset", "order_by", "sort_order"
2727
)
28-
def get_releases(self, **kwargs) -> dict:
28+
def get_releases(self, **kwargs) -> JsonOrXml:
2929
"""Get releases. https://fred.stlouisfed.org/docs/api/fred/releases.html"""
3030
return self._get(
3131
f"releases",
@@ -45,7 +45,7 @@ def get_releases(self, **kwargs) -> dict:
4545
"sort_order",
4646
"include_release_dates_with_no_data",
4747
)
48-
def get_releases_dates(self, **kwargs) -> dict:
48+
def get_releases_dates(self, **kwargs) -> JsonOrXml:
4949
"""Get releases dates. https://fred.stlouisfed.org/docs/api/fred/releases_dates.html"""
5050
return self._get(
5151
f"releases/dates",
@@ -55,7 +55,7 @@ def get_releases_dates(self, **kwargs) -> dict:
5555
)
5656

5757
@validate_api_args("api_key", "release_id", "file_type", "realtime_start", "realtime_end")
58-
def get_release(self, release_id: int, **kwargs) -> dict:
58+
def get_release(self, release_id: int, **kwargs) -> JsonOrXml:
5959
"""Get release by ID. https://fred.stlouisfed.org/docs/api/fred/release.html"""
6060
return self._get(
6161
f"release",
@@ -76,7 +76,7 @@ def get_release(self, release_id: int, **kwargs) -> dict:
7676
"sort_order",
7777
"include_release_dates_with_no_data",
7878
)
79-
def get_release_dates(self, release_id: int, **kwargs) -> dict:
79+
def get_release_dates(self, release_id: int, **kwargs) -> JsonOrXml:
8080
"""Get release dates by release ID. https://fred.stlouisfed.org/docs/api/fred/release_dates.html"""
8181
return self._get(
8282
f"release/dates",
@@ -101,7 +101,7 @@ def get_release_dates(self, release_id: int, **kwargs) -> dict:
101101
"tag_names",
102102
"exclude_tag_names",
103103
)
104-
def get_release_series(self, release_id: int, **kwargs) -> dict:
104+
def get_release_series(self, release_id: int, **kwargs) -> JsonOrXml:
105105
"""Get release series by release ID. https://fred.stlouisfed.org/docs/api/fred/release_series.html"""
106106
return self._get(
107107
f"release/series",
@@ -112,7 +112,7 @@ def get_release_series(self, release_id: int, **kwargs) -> dict:
112112
)
113113

114114
@validate_api_args("api_key", "file_type", "release_id", "realtime_start", "realtime_end")
115-
def get_release_sources(self, release_id: int, **kwargs) -> dict:
115+
def get_release_sources(self, release_id: int, **kwargs) -> JsonOrXml:
116116
"""Get release sources by release ID. https://fred.stlouisfed.org/docs/api/fred/release_sources.html"""
117117
return self._get(
118118
f"release/sources",
@@ -136,7 +136,7 @@ def get_release_sources(self, release_id: int, **kwargs) -> dict:
136136
"order_by",
137137
"sort_order",
138138
)
139-
def get_release_tags(self, release_id: int, **kwargs) -> dict:
139+
def get_release_tags(self, release_id: int, **kwargs) -> JsonOrXml:
140140
"""Get release tags by release ID. https://fred.stlouisfed.org/docs/api/fred/release_tags.html"""
141141
return self._get(
142142
f"release/tags",
@@ -161,7 +161,7 @@ def get_release_tags(self, release_id: int, **kwargs) -> dict:
161161
"order_by",
162162
"sort_order",
163163
)
164-
def get_release_related_tags(self, release_id: int, tag_names: str, **kwargs) -> dict:
164+
def get_release_related_tags(self, release_id: int, tag_names: str, **kwargs) -> JsonOrXml:
165165
"""Get release related tags by release ID and tag names.
166166
https://fred.stlouisfed.org/docs/api/fred/release_related_tags.html"""
167167
return self._get(
@@ -176,7 +176,7 @@ def get_release_related_tags(self, release_id: int, tag_names: str, **kwargs) ->
176176
@validate_api_args(
177177
"api_key", "file_type", "release_id", "element_id", "include_observation_values", "observation_date"
178178
)
179-
def get_release_tables(self, release_id: int, element_id: int = None, **kwargs) -> dict:
179+
def get_release_tables(self, release_id: int, element_id: int = None, **kwargs) -> JsonOrXml:
180180
"""Get release tables by release ID and element ID.
181181
https://fred.stlouisfed.org/docs/api/fred/release_tables.html"""
182182
return self._get(

src/fred/api/series.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env python3
22
"""
3-
Fred API Series Namespace Requests.
3+
Fred API Series Namespace.
44
"""
5-
from ._fred_client import FredClient
5+
from ._fred_client import FredClient, JsonOrXml
66
from .._util.decorators import validate_api_args
77

88

@@ -24,7 +24,7 @@ class FredAPISeries(FredClient):
2424
"""
2525

2626
@validate_api_args("api_key", "file_type", "series_id", "realtime_start", "realtime_end")
27-
def get_series_categories(self, series_id: str, **kwargs) -> dict:
27+
def get_series_categories(self, series_id: str, **kwargs) -> JsonOrXml:
2828
"""Get series categories by series ID. https://fred.stlouisfed.org/docs/api/fred/series_categories.html"""
2929
return self._get(
3030
f"series/categories",
@@ -35,7 +35,7 @@ def get_series_categories(self, series_id: str, **kwargs) -> dict:
3535
)
3636

3737
@validate_api_args("api_key", "file_type", "series_id", "realtime_start", "realtime_end", "order_by", "sort_order")
38-
def get_series_tags(self, series_id: str, **kwargs) -> dict:
38+
def get_series_tags(self, series_id: str, **kwargs) -> JsonOrXml:
3939
"""Get series tags by series ID. https://fred.stlouisfed.org/docs/api/fred/series_tags.html"""
4040
return self._get(
4141
f"series/tags",
@@ -61,7 +61,7 @@ def get_series_tags(self, series_id: str, **kwargs) -> dict:
6161
"tag_names",
6262
"exclude_tag_names",
6363
)
64-
def get_series_search(self, search_text: str, search_type: str = "full_text", **kwargs) -> dict:
64+
def get_series_search(self, search_text: str, search_type: str = "full_text", **kwargs) -> JsonOrXml:
6565
"""Get series search by text. https://fred.stlouisfed.org/docs/api/fred/series_search.html"""
6666
return self._get(
6767
f"series/search",
@@ -86,7 +86,7 @@ def get_series_search(self, search_text: str, search_type: str = "full_text", **
8686
"order_by",
8787
"sort_order",
8888
)
89-
def get_series_search_tags(self, series_search_text: str, **kwargs) -> dict:
89+
def get_series_search_tags(self, series_search_text: str, **kwargs) -> JsonOrXml:
9090
"""Get series search tags by text. https://fred.stlouisfed.org/docs/api/fred/series_search_tags.html"""
9191
return self._get(
9292
f"series/search/tags",
@@ -111,7 +111,7 @@ def get_series_search_tags(self, series_search_text: str, **kwargs) -> dict:
111111
"order_by",
112112
"sort_order",
113113
)
114-
def get_series_search_related_tags(self, series_search_text: str, tag_names: str, **kwargs) -> dict:
114+
def get_series_search_related_tags(self, series_search_text: str, tag_names: str, **kwargs) -> JsonOrXml:
115115
"""Get series related tags by text. https://fred.stlouisfed.org/docs/api/fred/series_search_related_tags.html"""
116116
return self._get(
117117
f"series/search/related_tags",
@@ -139,7 +139,7 @@ def get_series_search_related_tags(self, series_search_text: str, tag_names: str
139139
"output_type",
140140
"vintage_dates",
141141
)
142-
def get_series_observations(self, series_id: str, **kwargs) -> dict:
142+
def get_series_observations(self, series_id: str, **kwargs) -> JsonOrXml:
143143
"""Get series observations by series ID. https://fred.stlouisfed.org/docs/api/fred/series_observations.html"""
144144
return self._get(
145145
f"series/observations",
@@ -160,7 +160,7 @@ def get_series_observations(self, series_id: str, **kwargs) -> dict:
160160
"start_time",
161161
"end_time",
162162
)
163-
def get_series_updates(self, **kwargs) -> dict:
163+
def get_series_updates(self, **kwargs) -> JsonOrXml:
164164
"""Get series updates. https://fred.stlouisfed.org/docs/api/fred/series_updates.html"""
165165
# TODO FIXME: Validate start_time and end_time presence if applicable.
166166
return self._get(
@@ -173,7 +173,7 @@ def get_series_updates(self, **kwargs) -> dict:
173173
@validate_api_args(
174174
"api_key", "file_type", "series_id", "realtime_start", "realtime_end", "limit", "offset", "sort_order"
175175
)
176-
def get_series_vintagedates(self, series_id: str, **kwargs) -> dict:
176+
def get_series_vintagedates(self, series_id: str, **kwargs) -> JsonOrXml:
177177
"""Get series vintagedates by series ID. https://fred.stlouisfed.org/docs/api/fred/series_vintage_dates.html"""
178178
return self._get(
179179
f"series/vintagedates",
@@ -184,7 +184,7 @@ def get_series_vintagedates(self, series_id: str, **kwargs) -> dict:
184184
)
185185

186186
@validate_api_args("api_key", "file_type", "series_id", "realtime_start", "realtime_end")
187-
def get_series_release(self, series_id: str, **kwargs) -> dict:
187+
def get_series_release(self, series_id: str, **kwargs) -> JsonOrXml:
188188
"""Get series release by series ID. https://fred.stlouisfed.org/docs/api/fred/series_release.html"""
189189
return self._get(
190190
f"series/release",
@@ -195,7 +195,7 @@ def get_series_release(self, series_id: str, **kwargs) -> dict:
195195
)
196196

197197
@validate_api_args("api_key", "file_type", "series_id", "realtime_start", "realtime_end")
198-
def get_series(self, series_id: str, **kwargs) -> dict:
198+
def get_series(self, series_id: str, **kwargs) -> JsonOrXml:
199199
"""Get series by ID. https://fred.stlouisfed.org/docs/api/fred/series.html"""
200200
return self._get(
201201
f"series",

src/fred/api/sources.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""
33
Fred API Sources Namespace.
44
"""
5-
from ._fred_client import FredClient
5+
from ._fred_client import FredClient, JsonOrXml
66
from .._util.decorators import validate_api_args
77

88

@@ -19,7 +19,7 @@ class FredAPISources(FredClient):
1919
@validate_api_args(
2020
"api_key", "file_type", "realtime_start", "realtime_end", "limit", "offset", "order_by", "sort_order"
2121
)
22-
def get_sources(self, **kwargs) -> dict:
22+
def get_sources(self, **kwargs) -> JsonOrXml:
2323
"""Get sources. https://fred.stlouisfed.org/docs/api/fred/sources.html"""
2424
return self._get(
2525
f"sources",
@@ -29,7 +29,7 @@ def get_sources(self, **kwargs) -> dict:
2929
)
3030

3131
@validate_api_args("api_key", "file_type", "source_id", "realtime_start", "realtime_end")
32-
def get_source(self, source_id: int, **kwargs) -> dict:
32+
def get_source(self, source_id: int, **kwargs) -> JsonOrXml:
3333
"""Get source by ID. https://fred.stlouisfed.org/docs/api/fred/source.html"""
3434
return self._get(
3535
f"source",
@@ -50,7 +50,7 @@ def get_source(self, source_id: int, **kwargs) -> dict:
5050
"order_by",
5151
"sort_order",
5252
)
53-
def get_source_releases(self, source_id: int, **kwargs) -> dict:
53+
def get_source_releases(self, source_id: int, **kwargs) -> JsonOrXml:
5454
"""Get source releases by source ID. https://fred.stlouisfed.org/docs/api/fred/source_releases.html"""
5555
return self._get(
5656
f"source/releases",

0 commit comments

Comments
 (0)