This commit is contained in:
njmason22
2026-02-13 20:49:53 -05:00
parent 9f954f5372
commit e1cd7b7c87
3 changed files with 96 additions and 68 deletions

22
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?")
riddle_list = self.get_all_riddles() # calls another method and assigns it to a variable
#print(f"DEBUG: {choice(riddle_list)}") # displays in debug-mode as a dict
return choice(riddle_list) # the choice method randomizes the variable
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'])