Getting reports for all countries (profiles) in a loop or sequence #104
Unanswered
michalaleksandrowicz
asked this question in
Q&A
Replies: 1 comment
-
Sounds like I figured it out. I don't why but when I pull the the same code that request and downloads the report into a function and then loop through every country from the main script calling this function with country credentials and body as parameter, it works. Must be something about the object that it gets reset or something I guess when done through a function... hard to tell. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
I am a hobbyist trying to write a script to pull advertising reports per market from the API and then save to my database.
This library is great, helped me a lot. I managed the authentication and I can download report for single country (profile ID). I have credentials saved as a dictionary in code. I works fine.
Then I thought to go through all countries/profile_id in one app. So I created a list of of dictionaries that I loop through and I hit the block there. The first country works fine, the second always returns error:
{'code': '400','detail': 'Invalid or malformed request body provided'}
when calling the:
Reports(credentials=credentials_country2).post_report(body=json.dumps(data, indent=4))
And this happens even if I don't do it in a loop but just in a sequence within the same script.
Have been trying to fix it for a couple of days and no success. Could it be that something is preventing to authenticate to different profile_id/countries within the same script? Thanks a lot for help!
Here is the code for 2 countries in a sequence, 1 works, 2nd doesn't. If I comment out the first one the second one works so it is really puzzling:
_print('*******country 1')
result = Reports(credentials=credentials_country1).post_report(
body=json.dumps(data, indent=4))
print(result)
report_id = result.payload.get('reportId')
while result.payload.get('status') not in ["COMPLETED"]:
print(result.payload)
print('Sleeping...')
time.sleep(10)
result = Reports(credentials=credentials_country1).get_report(
reportId=report_id)
print(result)
report_url = result.payload.get('url')
res = requests.get(report_url)
decoded_str = gzip.decompress(res.content)
data = decoded_str.decode('UTF-8')
dict = json.loads(data)
print(type(data))
print(type(dict))
counter = counter+1
time.sleep(60)
print('*******country 2')
result = Reports(credentials=credentials_country2).post_report(
body=json.dumps(data, indent=4))
print(result)
report_id = result.payload.get('reportId')
while result.payload.get('status') not in ["COMPLETED"]:
print(result.payload)
print('Sleeping...')
time.sleep(10)
result = Reports(credentials=credentials_country2).get_report(
reportId=report_id)
print(result)
report_url = result.payload.get('url')
res = requests.get(report_url)
decoded_str = gzip.decompress(res.content)
data = decoded_str.decode('UTF-8')
dict = json.loads(data)__
Beta Was this translation helpful? Give feedback.
All reactions