I'm struggling to handle an error that Outbrain API server doesn't return. What I do is send a GET request to the server with the country name and it returns a JSON response which contains the desired ID value, but when I enter the country name with a typo the JSON response is empty instead of a JSON with the error message (if I'm not mistaken). So I'm trying to handle the error myself but it's not working. Here is my code (where csv[row][2] = United Kinjdom):
r = requests.get('https://api.outbrain.com/amplify/v0.1/locations/search?term=' + csv[row][2] + '&limit=7', headers=headers)
try:
print(r.json())
data = r.json()
error()
except:
print(r.text)
data = r.text
error()
for results in data:
if results['geoType']:
if results['geoType'] == 'Country' and results['name'] == csv[row][2]:
print(results['id'])
countryId = results['id']
else:
logText = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) + ": Country name error"
c.execute("INSERT INTO log VALUES (?)", [logText])
conn.commit()
os._exit(1)
The first print returns "[]" and the code doesn't seem to get to the "else" statement so I can add the error to the log DB.
This the server response when the country is spelled correctly:
[{'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'},
{'id': '7477c333ed4e1895b040efe45c30c816', 'geoType': 'Region', 'name': 'Darlington', 'canonicalName': 'Darlington, United Kingdom', 'code': 'D1', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}},
{'id': 'd9a9732283ec131e7fa422843449baa4', 'geoType': 'Region', 'name': 'Bath and North East Somerset', 'canonicalName': 'Bath and North East Somerset, United Kingdom', 'code': 'A4', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}},
{'id': '92b5f5ef7a5a25e60263e365982be9ad', 'geoType': 'Region', 'name': 'Northumberland', 'canonicalName': 'Northumberland, United Kingdom', 'code': 'J6', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}},
{'id': '3a83dddcd55e92aaab4b142c7858892d', 'geoType': 'Region', 'name': 'Vale of Glamorgan', 'canonicalName': 'Vale of Glamorgan, United Kingdom', 'code': 'Z3', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}},
{'id': '915c6d5814a9826e34e1a5c0a423797a', 'geoType': 'Region', 'name': 'Walsall', 'canonicalName': 'Walsall, United Kingdom', 'code': 'O8', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}},
{'id': 'e96bcc2de342e74e491d3b6ab95cfedc', 'geoType': 'Region', 'name': 'Tameside', 'canonicalName': 'Tameside, United Kingdom', 'code': 'O1', 'parent': {'id': 'c20768bb56a25c22299c38801e935c3a', 'geoType': 'Country', 'name': 'United Kingdom', 'canonicalName': 'United Kingdom', 'code': 'GB'}}]