From f502a5c94ae4d64a9dde5043999fc329decbeae3 Mon Sep 17 00:00:00 2001 From: owengavi2 Date: Thu, 19 Feb 2026 13:22:04 -0500 Subject: [PATCH] Updated notes.md, implemented methods in api.py One internet enabeled service I use regularly is Google as a search engine. I would think that Google uses a very complex version of the HTTP request and response systems that we worked on in this lab. The request is my search query from my machine, and the response is the search results coming from Google servers.This lab has given me a deeper understanding of how the internet and HTTP work. Before this lab, these services that I use just worked, and I never thought about how. Now, when I load a webpage or use an internet service, I have more background as to what is going on. --- .envrc | 1 + api.py | 94 +++++++++++++++++++++++++++++++++----------------------- notes.md | 27 ++++++++++++++++ 3 files changed, 83 insertions(+), 39 deletions(-) create mode 100644 .envrc 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..ff6531b 100644 --- a/api.py +++ b/api.py @@ -1,52 +1,68 @@ # RiddleAPI # --------- # By Chris Proctor -# The Riddle API takes care of connecting to the server. +# The Riddle API takes care of connecting to the server. + import requests from random import choice + class APIError(Exception): - "A custom error we'll use when something goes wrong with the API" + "A custom error we'll use when something goes wrong with the API" + class RiddleAPI: - "Provides an easy way for Python programs to interact with a Riddle Server" - def __init__(self, server_url): - self.server_url = server_url - - def get_all_riddles(self): - "Fetches all the riddles from the server" - route = "/all" - response = requests.get(self.server_url + route) - if response.ok: - 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" - params = {'id': riddle_id, 'answer': guess} - response = requests.post(self.server_url + route, json=params) - if response.ok: - return response.json() - else: - raise APIError(response.json()['errors']) - - 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?") - - 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?") - - 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?") + "Provides an easy way for Python programs to interact with a Riddle Server" + def __init__(self, server_url): + self.server_url = server_url - + def get_all_riddles(self): + "Fetches all the riddles from the server" + route = "/all" + response = requests.get(self.server_url + route) + if response.ok: + 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" + params = {'id': riddle_id, 'answer': guess} + response = requests.post(self.server_url + route, json=params) + if response.ok: + return response.json() + else: + raise APIError(response.json()['errors']) + + def get_riddle(self, riddle_id): + "Fetches a single riddle from the server" + route = "/show" + params = {'id': riddle_id} + response = requests.get(self.server_url + route, json=params) + if response.ok: + return response.json() + else: + raise APIError(response.json()['errors']) + #raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?") + + + + def get_random_riddle(self): + "Fetches all riddles from the server and then randomly returns one" + allriddles = self.get_all_riddles() + return choice(allriddles) + + def add_riddle(self, question, answer): + "Adds a new riddle to the server" + 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']) + #raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?") \ No newline at end of file diff --git a/notes.md b/notes.md index 3a338be..ec317d1 100644 --- a/notes.md +++ b/notes.md @@ -7,7 +7,34 @@ 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 1: command to get data from the url given +Line 2: sent request to server +Line 3: I will accept any data type +Line 4: Accepts gzip files which are compresseed files +Line 5: Continued connection to server + + ## 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. + +### Request 1 +http -v post https://riddles.makingwithcode.org/new riddle="What kind of man has bats on the brain?" answer="Batman" + + +Code: 400 Bad Request + + +### Request 2 +http -v get https://riddles.makingwithcode.org/something + + +Code: 404 Not Found + + +### Request 3 +http -v post https://riddles.makingwithcode.org/all + + +Code: 405 Method Not Allowed