diff --git a/pyshorteners/shorteners/cuttly.py b/pyshorteners/shorteners/cuttly.py index 20c5e43..2669001 100644 --- a/pyshorteners/shorteners/cuttly.py +++ b/pyshorteners/shorteners/cuttly.py @@ -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 diff --git a/tests/test_cuttly.py b/tests/test_cuttly.py index 9389a9a..083d1ab 100644 --- a/tests/test_cuttly.py +++ b/tests/test_cuttly.py @@ -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)