python-omniture
python-omniture copied to clipboard
Expected JSON in account.py
I've been getting an error in account.py because Adobe's servers are returning an error and the library is expecting JSON
I tested its not the library and that there is currently an error on Adobe's end by using the library to export the request object and pasting it directing in API Explorer (https://marketing.adobe.com/developer/api-explorer)
When I view the HAR file, I can see the error in the response
"response": {
"_charlesStatus": "COMPLETE",
"status": 200,
"statusText": "OK",
"httpVersion": "HTTP/1.1",
"cookies": [],
"headers": [
{
"name": "Cache-Control",
"value": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0"
},
{
"name": "Content-Type",
"value": "text/html; charset=utf-8"
},
{
"name": "Date",
"value": "Mon, 08 May 2017 21:51:27 GMT"
},
{
"name": "Expires",
"value": "Thu, 19 Nov 1981 08:52:00 GMT"
},
{
"name": "Pragma",
"value": "no-cache"
},
{
"name": "Server",
"value": "Apache/2.2.15 (CentOS)"
},
{
"name": "xserver",
"value": "cpt54.da2.omniture.com"
},
{
"name": "Content-Length",
"value": "39"
},
{
"name": "Connection",
"value": "Keep-alive"
}
],
"content": {
"size": 39,
"mimeType": "text/html; charset=utf-8",
"text": "the server returned an invalid response"
},
"redirectURL": null,
"headersSize": 342,
"bodySize": 39
},
"serverIPAddress": "XXXXXXXX",
"cache": {},
"timings": {
"dns": -1,
"connect": -1,
"ssl": -1,
"send": 1,
"wait": 2261,
"receive": 0
}
While not ideal, I resolved the issue on my end by retrying a couple times if the response wasn't the expected JSON object. It's never an issue with the query...its the server returning a bad response that usually succeeds after a retry. I have a ticket open with Adobe and I see related mentions of the issue here (https://marketing.adobe.com/developer/forum/reporting/api-return-error-500). Hopefully Adobe's fixes the issue, but it might be worth adding an exception handler here, in some form.
for i in range(0,10):
while True:
try:
self.log.info("Request: %s.%s Parameters: %s", api, method, query)
response = requests.post(
self.endpoint,
params={'method': api + '.' + method},
data=json.dumps(query),
headers=self._build_token()
)
self.log.debug("Response for %s.%s:%s", api, method, response.text)
json_response = response.json()
if type(json_response) == dict:
self.log.debug("Error Code %s", json_response.get('error'))
if json_response.get('error') == 'report_not_ready':
raise reports.ReportNotReadyError(json_response)
elif json_response.get('error') != None:
raise reports.InvalidReportError(json_response)
else:
return json_response
else:
return json_response
except ValueError:
print("Error from Adobe, try again")
self.log.debug("Error from Adobe, try again")
continue
break
Here's the reply I got from Adobe
"Depending upon the number of requests being sent to the queue, it might fail randomly. While I understand the behavior would not be very optimal when a CRON Job does the task, is there a way that an error check routine can be added to this and repeat the request on failure , depending upon the response received?"