Completed riddles lab.

This commit is contained in:
stacystudent
2026-02-10 11:20:10 -05:00
parent 92cb8a105c
commit e802c4a2eb
2 changed files with 16 additions and 6 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
source .venv/bin/activate

21
api.py
View File

@@ -36,17 +36,26 @@ 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, params=params)
if response.ok:
return response.json()
else:
raise APIError(response.json()['errors'])
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()
return choice(riddle_list)
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'])
#print(f"The server sent back: {response.json()}")