I have the following python code:
import requests
def requestAPI(url):
return requests.get(url=url).json()
UselessFact = RequestApi("https://uselessfacts.jsph.pl/random.json?language=en")['text']
I wanted to put a try/except on the requestAPI
function so it does'nt break the code. I thought about this:
import requests
def requestAPI(url, keys):
return requests.get(url=url).json() #Here is the struggle with passing the "keys" parameter into the return
UselessFact = RequestApi("https://uselessfacts.jsph.pl/random.json?language=en", ['text'])
I could do something like:
import requests
def requestAPI(url):
try:
return requests.get(url=url).json()
except:
return False
UselessFact = RequestApi("https://uselessfacts.jsph.pl/random.json?language=en")['text'] if (condition here) else False
But i think there's a better way of doing this.