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.
This commit is contained in:
owengavi2
2026-02-19 13:22:04 -05:00
parent 4855e01309
commit f502a5c94a
3 changed files with 83 additions and 39 deletions

1
.envrc Normal file
View File

@@ -0,0 +1 @@
source .venv/bin/activate

30
api.py
View File

@@ -3,17 +3,21 @@
# By Chris Proctor
# 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"
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"
@@ -33,20 +37,32 @@ class RiddleAPI:
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?")
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"
raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
allriddles = self.get_all_riddles()
return choice(allriddles)
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'])
#raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")

View File

@@ -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