I added code into api.py so client.py works now!!! super cool and fun!

1. Tik Tok is one service I use on my phone. A feature is new posts load automatically when I keep scrolling. Tik tok makes a GET request to Tik Tok users asking for the next set of posts. the requests includes headers with different parameterss then the server responds with 200 OK and sends back a JSON containing data. When I post, the app sends a POST request with the post ID and the servers responds confirming it was registered.
2. This lab was pretty fun! I think a bit differently on how apps and websites are created and used. I am more aware of how much data is being sent back and forth for a server to work successfully! In the end, it was really cool to be able to just enter a number for the riddle and guess the riddle.
This commit is contained in:
erbrown2
2026-05-09 14:54:37 -04:00
parent 820476587a
commit bd266cc4dc

22
api.py
View File

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