Skip to content

Commit

Permalink
Refactor api url formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Kirkegaard committed Feb 28, 2019
1 parent 2ac1ebb commit aef4cb2
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions mpr/data/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

date_format = "%m-%d-%Y"

base_url = 'https://mpr.datamart.ams.usda.gov/ws/report/v1/hogs/{report}?\
filter={{"filters":[{{"fieldName":"Report date","operatorType":"BETWEEN","values":["{start_date}", "{end_date}"]}}]}}'
base_url = 'https://mpr.datamart.ams.usda.gov/ws/report/v1/hogs'
report_url = lambda report: f'{base_url}/{report}'
date_filter = lambda start, end: f'{{"fieldName":"Report date","operatorType":"BETWEEN","values":["{start}","{end}"]}}'
request_url = lambda report, start, end: f'{report_url(report)}?filter={{"filters":[{date_filter(start, end)}]}}'


def get_optional(attr: Attributes, key: str) -> Optional[T]:
Expand Down Expand Up @@ -56,10 +58,7 @@ def filter_sections(records: Iterator[Attributes], *args: str) -> Iterator[Itera


async def fetch(report: Report, start_date: date, end_date=date.today()) -> Iterator[Attributes]:
url = base_url.format(
report=report.value,
start_date=start_date.strftime(date_format),
end_date=end_date.strftime(date_format))
url = request_url(report=report.value, start=start_date.strftime(date_format), end=end_date.strftime(date_format))

async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
Expand Down

0 comments on commit aef4cb2

Please sign in to comment.