vnoelifant / charity-finder

3 stars 0 forks source link

Switch from xml to json #6

Closed bbelderbos closed 1 year ago

bbelderbos commented 1 year ago

XML argh haha, you definitely want JSON

(Pdb) response.json()
{'themes': {'theme': [{'id': 'animals', 'name': 'Animal Welfare'}, {'id': 'children', 'name': 'Child Protection'}, {'id': 'climate', 'name': 'Climate Action'}, {'id': 'democ', 'name': 'Peace and Reconciliation'}, {'id': 'disaster', 'name': 'Disaster Response'}, {'id': 'ecdev', 'name': 'Economic Growth'}, {'id': 'edu', 'name': 'Education'}, {'id': 'env', 'name': 'Ecosystem Restoration'}, {'id': 'gender', 'name': 'Gender Equality'}, {'id': 'health', 'name': 'Physical Health'}, {'id': 'human', 'name': 'Ending Human Trafficking'}, {'id': 'rights', 'name': 'Justice and Human Rights'}, {'id': 'sport', 'name': 'Sport'}, {'id': 'tech', 'name': 'Digital Literacy'}, {'id': 'hunger', 'name': 'Food Security'}, {'id': 'art', 'name': 'Arts and Culture'}, {'id': 'lgbtq', 'name': 'LGBTQIA+ Equality'}, {'id': 'covid-19', 'name': 'COVID-19'}, {'id': 'water', 'name': 'Clean Water'}, {'id': 'disability', 'name': 'Disability Rights'}, {'id': 'endabuse', 'name': 'Ending Abuse'}, {'id': 'mentalhealth', 'name': 'Mental Health'}, {'id': 'justice', 'name': 'Racial Justice'}, {'id': 'refugee', 'name': 'Refugee Rights'}, {'id': 'reproductive', 'name': 'Reproductive Health'}, {'id': 'housing', 'name': 'Safe Housing'}, {'id': 'agriculture', 'name': 'Sustainable Agriculture'}, {'id': 'wildlife', 'name': 'Wildlife Conservation'}]}}

Got it working with this change:

headers = {'Accept': 'application/json'}
response = requests.get(url, params=params, headers=headers)

If Google and found the tutorial: https://www.globalgiving.org/api/how-to-use/tutorial/

Then followed this:

Responses may be formatted as XML 'Accept: application/xml' or JSON 'Accept: application/json'.

bbelderbos commented 1 year ago

First check was the endpoint docs btw: https://www.globalgiving.org/api/methods/get-themes/

When I saw this I knew it should be possible:

image

Btw Content-Type might also work: https://www.globalgiving.org/api/how-to-use/conventions/ but headers = {'Content-Type': 'application/json'} by itself did not work for me!

(Pdb) response.json()
*** requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
bbelderbos commented 1 year ago

Done here: https://github.com/vnoelifant/charity-finder/pull/7/files - also removed xml2dict as dependency.