diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a96c22 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +source .venv/bin/activate \ No newline at end of file diff --git a/api.py b/api.py index 35759bf..7737689 100644 --- a/api.py +++ b/api.py @@ -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()}") \ No newline at end of file