Skip to content

Commit

Permalink
Lint fixes and remove obsolete status code check.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudd-O authored Feb 22, 2023
1 parent 0261e1f commit 2d4bc83
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions beanprice/sources/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,47 @@ def _requestor(*args, **kwargs):
if "headers" not in kwargs:
kwargs["headers"] = {}
# Yahoo! balks without this header.
kwargs["headers"]["User-Agent"] = "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/110.0"
kwargs["headers"]["User-Agent"] = (
"Mozilla/5.0 (X11; Linux x86_64; "
"rv:109.0) Gecko/20100101 Firefox/110.0"
)
response = requests.get(*args, **kwargs)
response.raise_for_status()
try:
response.raise_for_status()
except Exception as exc:
raise YahooError(
"HTTP status {}: {}".format(
response.status_code,
response.text(),
)
) from exc
return response


def parse_response(response: requests.models.Response) -> Dict:
"""Process as response from Yahoo.
Assumes the response code is among the OK response codes.
Raises:
YahooError: If there is an error in the response.
"""
json = response.json(parse_float=Decimal)
content = next(iter(json.values()))
if response.status_code != requests.codes.ok:
raise YahooError("Status {}: {}".format(response.status_code, content['error']))
if len(json) != 1:
raise YahooError("Invalid format in response from Yahoo; many keys: {}".format(
','.join(json.keys())))
if content['error'] is not None:
raise YahooError("Error fetching Yahoo data: {}".format(content['error']))
try:
return content['result'][0]
except IndexError:
raise YahooError("Could not destructure response: the content contains zero-length result {}".format(content['result']))
except IndexError as exc:
raise YahooError(
(
"Could not destructure response: "
"the content contains zero-length result {}"
).format(content['result'])
) from exc


# Note: Feel free to suggest more here via a PR.
Expand Down

0 comments on commit 2d4bc83

Please sign in to comment.