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 old mode 100644 new mode 100755 index 35759bf..ca4e0d6 --- a/api.py +++ b/api.py @@ -22,7 +22,7 @@ class RiddleAPI: return response.json()['riddles'] else: raise APIError(response.json()['errors']) - + def guess_riddle(self, riddle_id, guess): "Submits a guess to the server. Returns True or False" route = "/guess" @@ -36,16 +36,30 @@ 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, json=params) + if response.ok: + return response.json() + else: + raise APIError('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: + return choice(riddles) + else: + return APIError('no riddles available') 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']) diff --git a/client.py b/client.py old mode 100644 new mode 100755 index 7facc6d..5dfd4c7 --- a/client.py +++ b/client.py @@ -3,6 +3,7 @@ # By Chris Proctor # RiddleView provides a nice user interface for guessing riddles. + from api import RiddleAPI class RiddleView: diff --git a/notes.md b/notes.md index 3a338be..9b00f4c 100644 --- a/notes.md +++ b/notes.md @@ -7,7 +7,27 @@ about the meaning of the line, and some situation in which it might be useful. You are welcome to research the meanings of these headers, but it's also fine to speculate for now. + +Line 12 is related to the connection type, it can be useful to know the connection went through and didn't fail +Line 13 has something to do with size of the file, this can be useful for how much space it takes up +Line 14 is the coding language which is useful for the user to code into the files +Line 25 shows how many correct answers the users have guessed, this can be useful for assessing users on something or just collecting data +Line 28 is the ID of the riddle, this is helpful to edit the riddle or identify it when coding + + ## Checkpoint 2 The goal of this checkpoint is to see what status codes you can get back from the riddle server. Paste below several `http` requests and the status codes they return. + +http -v post https://riddles.makingwithcode.org/new question="What have I got in my pocket?" answer="My precious" +HTTP/1.1 200 OK + +http -v get https://riddles.makingwithcode.org/new question="What have I got in my pocket?" answer="My precious" +HTTP/1.1 405 Method Not Allowed + +http -v post https://riddles.makingwithcode.org/new question="What have I got in my pocket?" +HTTP/1.1 400 Bad Request + +http -v post https://riddles.makingwithcode.org question="What have I got in my pocket?" answer="My precious" +HTTP/1.1 404 Not Found diff --git a/pyproject.toml b/pyproject.toml index 23a3ba0..d94734a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -6,7 +6,9 @@ authors = [{ name = "Chris Proctor", email = "chris@chrisproctor.net" }] requires-python = ">=3.10,<4.0" readme = "README.md" license = { text = "MIT" } -dependencies = ["requests (>=2.32.3,<3.0.0)"] +dependencies = [ + "requests>=2.32.3,<3.0.0", +] [build-system] requires = ["hatchling"]