Print alchemer api error response before failing (#5287)
This commit is contained in:
Родитель
46567ebb7f
Коммит
3e34be8db0
|
@ -9,6 +9,7 @@ import click
|
|||
import pytz
|
||||
import requests
|
||||
from google.cloud import bigquery
|
||||
from requests import HTTPError
|
||||
|
||||
|
||||
def utc_date_to_eastern_string(date_string):
|
||||
|
@ -86,8 +87,7 @@ def get_survey_data(survey_id, date_string, token, secret):
|
|||
f"&filter[operator][1]=<"
|
||||
f"&filter[value][1]={end_date}"
|
||||
)
|
||||
resp = requests.get(url)
|
||||
resp.raise_for_status()
|
||||
resp = _get_request(url)
|
||||
survey = resp.json()
|
||||
|
||||
# if the result set is large, we'll have to page through them to get all data
|
||||
|
@ -99,12 +99,22 @@ def get_survey_data(survey_id, date_string, token, secret):
|
|||
|
||||
for page in range(2, total_pages + 1):
|
||||
print(f"fetching page {page}")
|
||||
resp = requests.get(url + f"&page={page}")
|
||||
resp.raise_for_status()
|
||||
resp = _get_request(url + f"&page={page}")
|
||||
ret = ret + construct_data(resp.json(), date_string)
|
||||
return ret
|
||||
|
||||
|
||||
def _get_request(url):
|
||||
"""Make get request and print response if there is an exception."""
|
||||
resp = requests.get(url)
|
||||
try:
|
||||
resp.raise_for_status()
|
||||
except HTTPError:
|
||||
print(f"Error response: {resp.text}")
|
||||
raise
|
||||
return resp
|
||||
|
||||
|
||||
def response_schema():
|
||||
"""Get the schema for the response object from disk."""
|
||||
path = Path(__file__).parent / "response.schema.json"
|
||||
|
|
|
@ -5,6 +5,7 @@ import pytest
|
|||
import requests
|
||||
from click.testing import CliRunner
|
||||
from google.cloud import bigquery
|
||||
from requests import HTTPError
|
||||
|
||||
from bigquery_etl.alchemer.survey import (
|
||||
construct_data,
|
||||
|
@ -243,24 +244,44 @@ def testing_table_id(testing_dataset):
|
|||
yield table_id
|
||||
|
||||
|
||||
class MockResponse:
|
||||
@staticmethod
|
||||
def raise_for_status():
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def json():
|
||||
return EXAMPLE_RESPONSE
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
return str(EXAMPLE_RESPONSE)
|
||||
|
||||
|
||||
class MockErrorResponse(MockResponse):
|
||||
@staticmethod
|
||||
def raise_for_status():
|
||||
raise HTTPError()
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def patch_api_requests(monkeypatch):
|
||||
# Note: this does not test iterating over multiple pages
|
||||
class MockResponse:
|
||||
@staticmethod
|
||||
def raise_for_status():
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def json():
|
||||
return EXAMPLE_RESPONSE
|
||||
|
||||
def mock_get(*args, **kwargs):
|
||||
return MockResponse()
|
||||
|
||||
monkeypatch.setattr(requests, "get", mock_get)
|
||||
|
||||
|
||||
@pytest.fixture()
|
||||
def patch_api_requests_error(monkeypatch):
|
||||
# Note: this does not test iterating over multiple pages
|
||||
def mock_get(*args, **kwargs):
|
||||
return MockErrorResponse()
|
||||
|
||||
monkeypatch.setattr(requests, "get", mock_get)
|
||||
|
||||
|
||||
def test_utc_date_to_eastern_time():
|
||||
# UTC-5 during standard time: https://en.wikipedia.org/wiki/Eastern_Time_Zone
|
||||
assert utc_date_to_eastern_string("2021-01-05") == "2021-01-04+19:00:00"
|
||||
|
@ -311,6 +332,13 @@ def test_get_survey_data(patch_api_requests):
|
|||
)
|
||||
|
||||
|
||||
def test_get_survey_data_error(patch_api_requests_error):
|
||||
"""Test that the wrapper correctly reraises the HTTPError."""
|
||||
pytest.raises(
|
||||
HTTPError, get_survey_data, "555555", SUBMISSION_DATE, "token", "secret"
|
||||
)
|
||||
|
||||
|
||||
def test_response_schema():
|
||||
# ensure that there aren't any exceptions
|
||||
assert response_schema()
|
||||
|
|
Загрузка…
Ссылка в новой задаче