Skip to content
This repository has been archived by the owner on Jun 7, 2022. It is now read-only.

Commit

Permalink
fix cuttly tests and api response
Browse files Browse the repository at this point in the history
  • Loading branch information
ellisonleao committed Mar 19, 2020
1 parent 8f34386 commit 7a419c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
17 changes: 15 additions & 2 deletions pyshorteners/shorteners/cuttly.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,23 @@ def short(self, url):
"API response is invalid ,could not be decoded"
)

status = response.json()["url"]["status"]
try:
status = data["url"]["status"]
except KeyError:
raise BadAPIResponseException(
"API response does not have the required field: status"
)

if status == self.STATUS_INVALID:
"""According to the API Docs when a status code of 4 is returned with
json an Invalid API Key is provided"""
raise BadAPIResponseException("Invalid API Key")

return data["url"]["shortLink"]
try:
short_link = data["url"]["shortLink"]
except KeyError:
raise BadAPIResponseException(
"API response does not have the required field: shortLink"
)

return short_link
2 changes: 1 addition & 1 deletion tests/test_cuttly.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_cuttly_short_method():
# mock responses
params = urlencode({"key": cuttly.api_key, "short": url})
mock_url = f"{cuttly.api_url}?{params}"
res = json.dumps({"status": 1, "url": {"shortLink": shorted_url}})
res = json.dumps({"url": {"status": 1, "shortLink": shorted_url}})
responses.add(responses.GET, mock_url, status=200, body=res, match_querystring=True)

shorten_result = cuttly.short(url)
Expand Down

0 comments on commit 7a419c4

Please sign in to comment.