From eb40603441e70d822276c2cbad304a3a243e5f5f Mon Sep 17 00:00:00 2001 From: Rebecca Hankey Date: Sat, 19 Apr 2025 14:38:54 -0400 Subject: [PATCH] I added the code for the three missing methods so that the riddles would work. 1. I regularly use the weather app on my phone. This http request would rely on a few different factors. It would need to call upon location first, then pull data based on that location and display it. This is complicated by the requests for additional informaiton such as humidity, sun rise and set, percipitation, etc. However, I think those would all pull fom similar or the same database(s). I Thikn that there is something interesting though in the push notifications that come through when thereis an emergency weather alert. It would have to establish the route, the params would call the location and status information that the app would need to access accurate data to display, then the response would be the answers to the queries. 2. This lab really helped me conceptualize the process of such technological connections. I always knew that the things we "call upon" in our computational daily lives are complex and have so many moving parts. One struggle I had in this lab was that when I was coding the response lines I used the word 'error' instead of 'errors'. The one little 's' threw the entire system into an error (ironically). --- api.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/api.py b/api.py index 35759bf..ca78367 100644 --- a/api.py +++ b/api.py @@ -36,17 +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: + return response.json() + else: + raise APIError(response.json()['errors']) + #done 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() + return choice(riddles) + #done + 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?") - + route = "/new" + 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']) + #done