From bd266cc4dc6452c438c25fb8d8a9069b481ff648 Mon Sep 17 00:00:00 2001 From: erbrown2 Date: Sat, 9 May 2026 14:54:37 -0400 Subject: [PATCH] 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. --- api.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/api.py b/api.py index 35759bf..7d1d36b 100644 --- a/api.py +++ b/api.py @@ -36,16 +36,32 @@ 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?") + 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): "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): "Adds a new riddle to the server" 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']) +