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

Commit 7a419c4

Browse files
committed
fix cuttly tests and api response
1 parent 8f34386 commit 7a419c4

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

pyshorteners/shorteners/cuttly.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,23 @@ def short(self, url):
4545
"API response is invalid ,could not be decoded"
4646
)
4747

48-
status = response.json()["url"]["status"]
48+
try:
49+
status = data["url"]["status"]
50+
except KeyError:
51+
raise BadAPIResponseException(
52+
"API response does not have the required field: status"
53+
)
54+
4955
if status == self.STATUS_INVALID:
5056
"""According to the API Docs when a status code of 4 is returned with
5157
json an Invalid API Key is provided"""
5258
raise BadAPIResponseException("Invalid API Key")
5359

54-
return data["url"]["shortLink"]
60+
try:
61+
short_link = data["url"]["shortLink"]
62+
except KeyError:
63+
raise BadAPIResponseException(
64+
"API response does not have the required field: shortLink"
65+
)
66+
67+
return short_link

tests/test_cuttly.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def test_cuttly_short_method():
1818
# mock responses
1919
params = urlencode({"key": cuttly.api_key, "short": url})
2020
mock_url = f"{cuttly.api_url}?{params}"
21-
res = json.dumps({"status": 1, "url": {"shortLink": shorted_url}})
21+
res = json.dumps({"url": {"status": 1, "shortLink": shorted_url}})
2222
responses.add(responses.GET, mock_url, status=200, body=res, match_querystring=True)
2323

2424
shorten_result = cuttly.short(url)

0 commit comments

Comments
 (0)