Skip to content

Commit 05c8967

Browse files
committed
handle different kinds of facets
1 parent 19036e0 commit 05c8967

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# myeia
22

3-
[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.3.5&x2=0)](https://badge.fury.io/py/myeia)
3+
[![PyPI version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=py&r=r&ts=1683906897&type=6e&v=0.3.6&x2=0)](https://badge.fury.io/py/myeia)
44
[![License: MIT](https://img.shields.io/badge/License-MIT-red.svg)](https://github.com/philsv/myeia/blob/main/LICENSE)
55
[![Weekly Downloads](https://static.pepy.tech/personalized-badge/myeia?period=week&units=international_system&left_color=grey&right_color=blue&left_text=downloads/week)](https://pepy.tech/project/myeia)
66
[![Monthly Downloads](https://static.pepy.tech/personalized-badge/myeia?period=month&units=international_system&left_color=grey&right_color=blue&left_text=downloads/month)](https://pepy.tech/project/myeia)

myeia/api.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22
import warnings
33
from dataclasses import dataclass, field
44
from datetime import datetime, timedelta
5+
import logging
56
from typing import List, Optional, Tuple, Union
67

78
import pandas as pd
89
import requests
910
from dotenv import load_dotenv
11+
from requests.exceptions import JSONDecodeError
1012

1113
try:
1214
from pandas.errors import SettingWithCopyWarning
@@ -17,14 +19,23 @@
1719

1820
load_dotenv()
1921

22+
logging.basicConfig(
23+
level=logging.INFO, format="%(asctime)s - %(message)s", datefmt="%d-%b-%y %H:%M:%S"
24+
)
25+
26+
2027

2128
def get_json_response(url: str, headers: dict) -> pd.DataFrame:
2229
"""
2330
Helper function to get JSON response from API.
2431
"""
2532
response = requests.get(url, headers=headers)
2633
response.raise_for_status()
27-
json_response = response.json()
34+
try:
35+
json_response = response.json()
36+
except JSONDecodeError as e:
37+
logging.error(f"Response: {response.text}")
38+
raise ValueError(f"Error decoding JSON response ({e}). Data might be incomplete or missing. Chunking your request might help.") from e
2839
return pd.DataFrame(json_response["response"]["data"])
2940

3041

@@ -154,8 +165,8 @@ def get_series_via_route(
154165

155166
if facet == "series":
156167
df = base_df[["period", "value", "series-description", "series"]]
157-
elif facet == "seriesId":
158-
df = base_df[["period", "value", "seriesDescription", "seriesId"]]
168+
else:
169+
df = base_df[["period", "value", "seriesDescription", facet]]
159170

160171
df.reset_index(drop=True, inplace=True)
161172
df.rename(columns={df.columns[1]: df[df.columns[2]][0]}, inplace=True)

myeia/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.3.5"
1+
__version__ = "0.3.6"

tests/test_myeia.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def test_get_series(series_id, start_date, end_date):
3333
("petroleum/crd/crpdn", "MCRFPP52", "monthly", "series", None, None),
3434
("petroleum/crd/crpdn", ["MCRFPP51", "MCRFPP52"], "monthly", "series", None, None),
3535
("petroleum/crd/crpdn", ["MCRFPP51", "MCRFPP52"], "monthly", "series", "2021-01-01", "2021-11-31"),
36+
("total-energy", "PATWPUS", "monthly", "msn", "2020-01-01", None),
3637
],
3738
)
3839
def test_get_series_via_route(route, series, frequency, facet, start_date, end_date):

0 commit comments

Comments
 (0)