completed api.py so that client.py works

This commit is contained in:
Pat Wick 2024-04-04 21:11:32 -04:00
parent 1c2ee8c4a0
commit b00517a8b0
2 changed files with 17 additions and 3 deletions

Binary file not shown.

20
api.py
View File

@ -36,16 +36,30 @@ class RiddleAPI:
def get_riddle(self, riddle_id):
"Fetches a single riddle from the server"
route = "/show"
raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?")
params = {'id': riddle_id}
response = requests.get(self.server_url + route, json=params)
if response.ok:
return response.json()
else:
raise APIError(response.json()['errors'])
#raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?")
def get_random_riddle(self):
"Fetches all riddles from the server and then randomly returns one"
raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
riddles = self.get_all_riddles()
return choice(riddles)
#raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
def add_riddle(self, question, answer):
"Adds a new riddle to the server"
route = "/new"
raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")
params = {'question': question, 'answer': answer}
response = requests.post(self.server_url + route, json=params)
if response.ok:
return response.json()
else:
raise APIError(response.json()['errors'])
#raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")