Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logging and refactor responses #69

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 21 additions & 28 deletions pynws/raw_data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""Functions to retrieve raw data."""
from datetime import datetime
import logging

from pynws.const import API_ACCEPT, API_USER
import pynws.urls

_LOGGER = logging.getLogger(__name__)


def get_header(userid):
"""Get header.
Expand All @@ -13,6 +16,17 @@ def get_header(userid):
return {"accept": API_ACCEPT, "User-Agent": API_USER.format(userid)}


async def _make_request(websession, url, header, params=None):
"""Make request."""
async with websession.get(url, headers=header, params=params) as res:
_LOGGER.debug("Request for %s returned code: %s", url, res.status)
_LOGGER.debug("Request for %s returned header: %s", url, res.headers)
res.raise_for_status()
obs = await res.json()
_LOGGER.debug("Request for %s returned data: %s", url, obs)
return obs


async def raw_stations_observations(station, websession, userid, limit=0, start=None):
"""Get observation response from station"""
params = {}
Expand All @@ -26,67 +40,46 @@ async def raw_stations_observations(station, websession, userid, limit=0, start=

url = pynws.urls.stations_observations_url(station)
header = get_header(userid)
async with websession.get(url, headers=header, params=params) as res:
res.raise_for_status()
obs = await res.json()
return obs
return await _make_request(websession, url, header, params)


async def raw_points_stations(lat, lon, websession, userid):
"""Get list of stations for lat/lon"""
url = pynws.urls.points_stations_url(lat, lon)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)


async def raw_points(lat, lon, websession, userid):
"""Return griddata response."""
url = pynws.urls.points_url(lat, lon)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)


async def raw_forecast_all(wfo, x, y, websession, userid):
"""Return griddata response."""
url = pynws.urls.forecast_all_url(wfo, x, y)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)


async def raw_gridpoints_forecast(wfo, x, y, websession, userid):
"""Return griddata response."""
url = pynws.urls.gridpoints_forecast_url(wfo, x, y)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)


async def raw_gridpoints_forecast_hourly(wfo, x, y, websession, userid):
"""Return griddata response."""
url = pynws.urls.gridpoints_forecast_hourly_url(wfo, x, y)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)


async def raw_alerts_active_zone(zone, websession, userid):
"""Return griddata response."""
url = pynws.urls.alerts_active_zone_url(zone)
header = get_header(userid)
async with websession.get(url, headers=header) as res:
res.raise_for_status()
jres = await res.json()
return jres
return await _make_request(websession, url, header)
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
log_level = DEBUG