Skip to content

Commit b951654

Browse files
committed
refactor: default to df output
* remove format_type, always classic * if only_classic, return classic, else df
1 parent c1ffe1c commit b951654

File tree

3 files changed

+10
-46
lines changed

3 files changed

+10
-46
lines changed

epidatpy/_model.py

+2-16
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,6 @@ def __str__(self) -> str:
8686
return f"{format_date(self.start)}-{format_date(self.end)}"
8787

8888

89-
class EpiDataFormatType(str, Enum):
90-
"""
91-
possible formatting options for API calls
92-
"""
93-
94-
json = "json"
95-
classic = "classic"
96-
97-
9889
class InvalidArgumentException(Exception):
9990
"""
10091
exception for an invalid argument
@@ -174,41 +165,36 @@ def _verify_parameters(self) -> None:
174165

175166
def _formatted_parameters(
176167
self,
177-
format_type: Optional[EpiDataFormatType] = None,
178168
fields: Optional[Sequence[str]] = None,
179169
) -> Mapping[str, str]:
180170
"""
181171
format this call into a [URL, Params] tuple
182172
"""
183173
all_params = dict(self._params)
184-
if format_type and format_type != EpiDataFormatType.classic:
185-
all_params["format"] = format_type
186174
if fields:
187175
all_params["fields"] = fields
188176
return {k: format_list(v) for k, v in all_params.items() if v is not None}
189177

190178
def request_arguments(
191179
self,
192-
format_type: Optional[EpiDataFormatType] = None,
193180
fields: Optional[Sequence[str]] = None,
194181
) -> Tuple[str, Mapping[str, str]]:
195182
"""
196183
format this call into a [URL, Params] tuple
197184
"""
198-
formatted_params = self._formatted_parameters(format_type, fields)
185+
formatted_params = self._formatted_parameters(fields)
199186
full_url = add_endpoint_to_url(self._base_url, self._endpoint)
200187
return full_url, formatted_params
201188

202189
def request_url(
203190
self,
204-
format_type: Optional[EpiDataFormatType] = None,
205191
fields: Optional[Sequence[str]] = None,
206192
) -> str:
207193
"""
208194
format this call into a full HTTP request url with encoded parameters
209195
"""
210196
self._verify_parameters()
211-
u, p = self.request_arguments(format_type, fields)
197+
u, p = self.request_arguments(fields)
212198
query = urlencode(p)
213199
if query:
214200
return f"{u}?{query}"

epidatpy/request.py

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from datetime import date
21
from typing import (
32
Any,
43
Dict,
@@ -24,7 +23,6 @@
2423
AEpiDataCall,
2524
EpidataFieldInfo,
2625
EpidataFieldType,
27-
EpiDataFormatType,
2826
EpiDataResponse,
2927
EpiRange,
3028
EpiRangeParam,
@@ -87,11 +85,10 @@ def with_session(self, session: Session) -> "EpiDataCall":
8785

8886
def _call(
8987
self,
90-
format_type: Optional[EpiDataFormatType] = None,
9188
fields: Optional[Sequence[str]] = None,
9289
stream: bool = False,
9390
) -> Response:
94-
url, params = self.request_arguments(format_type, fields)
91+
url, params = self.request_arguments(fields)
9592
return _request_with_retry(url, params, self._session, stream)
9693

9794
def classic(
@@ -102,7 +99,7 @@ def classic(
10299
"""Request and parse epidata in CLASSIC message format."""
103100
self._verify_parameters()
104101
try:
105-
response = self._call(None, fields)
102+
response = self._call(fields)
106103
r = cast(EpiDataResponse, response.json())
107104
epidata = r.get("epidata")
108105
if epidata and isinstance(epidata, list) and len(epidata) > 0 and isinstance(epidata[0], dict):
@@ -115,25 +112,11 @@ def __call__(
115112
self,
116113
fields: Optional[Sequence[str]] = None,
117114
disable_date_parsing: Optional[bool] = False,
118-
) -> EpiDataResponse:
119-
"""Request and parse epidata in CLASSIC message format."""
120-
return self.classic(fields, disable_date_parsing=disable_date_parsing)
121-
122-
def json(
123-
self,
124-
fields: Optional[Sequence[str]] = None,
125-
disable_date_parsing: Optional[bool] = False,
126-
) -> List[Mapping[str, Union[str, int, float, date, None]]]:
127-
"""Request and parse epidata in JSON format"""
115+
) -> Union[EpiDataResponse, DataFrame]:
116+
"""Request and parse epidata in df message format."""
128117
if self.only_supports_classic:
129-
raise OnlySupportsClassicFormatException()
130-
self._verify_parameters()
131-
response = self._call(EpiDataFormatType.json, fields)
132-
response.raise_for_status()
133-
return [
134-
self._parse_row(row, disable_date_parsing=disable_date_parsing)
135-
for row in cast(List[Mapping[str, Union[str, int, float, None]]], response.json())
136-
]
118+
return self.classic(fields, disable_date_parsing=disable_date_parsing)
119+
return self.df(fields, disable_date_parsing=disable_date_parsing)
137120

138121
def df(
139122
self,
@@ -144,7 +127,8 @@ def df(
144127
if self.only_supports_classic:
145128
raise OnlySupportsClassicFormatException()
146129
self._verify_parameters()
147-
rows = self.json(fields, disable_date_parsing=disable_date_parsing)
130+
json = self.classic(fields, disable_date_parsing=disable_date_parsing)
131+
rows = json.get("epidata", [])
148132
pred = fields_to_predicate(fields)
149133
columns: List[str] = [info.name for info in self.meta if pred(info.name)]
150134
df = DataFrame(rows, columns=columns or None)

smoke_test.py

-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
classic = apicall.classic()
1212
print(classic)
1313

14-
data = apicall.json()
15-
print(data[0])
16-
1714
df = apicall.df()
1815
print(df.columns)
1916
print(df.dtypes)
@@ -47,9 +44,6 @@
4744
classic = apicall.classic()
4845
print(classic)
4946

50-
data = apicall.json()
51-
print(data[0])
52-
5347
df = apicall.df()
5448
print(df.columns)
5549
print(df.dtypes)

0 commit comments

Comments
 (0)