Skip to content

Commit

Permalink
Add debug logging and refactor responses (#69)
Browse files Browse the repository at this point in the history
* refactor and add debug logging

* add logging to pytest config
  • Loading branch information
MatthewFlamm committed Mar 3, 2022
1 parent 639c8f0 commit 7428f0a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
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

0 comments on commit 7428f0a

Please sign in to comment.