Added code to api so that the client code would work

Spotify app, when clicking on a song to play music. The app is telling the server what to play
Yes, its made me realizy that when things are loading its becouse it s trying to pull up
the data from a server.
This commit is contained in:
tsmith372
2026-02-16 12:24:25 -05:00
parent bcefa4518b
commit f56535ded9
5 changed files with 310 additions and 67 deletions

21
api.py
View File

@@ -36,16 +36,31 @@ 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:
data = response.json()
return data.get('riddle', data)
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?")
riddles = self.get_all_riddles()
if len(riddles) == 0:
raise APIError(["Sorry, there are no riddles on the server!"])
return random.choice(riddles)
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'])