generated from mwc/lab_riddles
I submitted the changes I previously made during
Office Hours last week. 1. I use Steam regularly when I play games. I would think that there are many webs of POST and GET requests within that enable online connections, downloading and saving game data etc. I think the Steam client probably communicates with the game servers with the POST and GET requests to accompolish this. 2. Yeah I would say so! I didn't know about either type of the requests before so I think I learned about how things work. Very vaguely I knew that anything we do on the Internetis basically an endless transmission of data but I never considered the technicality of transmissions that take place.
This commit is contained in:
22
api.py
22
api.py
@@ -4,7 +4,7 @@
|
|||||||
# The Riddle API takes care of connecting to the server.
|
# The Riddle API takes care of connecting to the server.
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from random import choice
|
import random
|
||||||
|
|
||||||
class APIError(Exception):
|
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"
|
||||||
@@ -36,16 +36,30 @@ class RiddleAPI:
|
|||||||
def get_riddle(self, riddle_id):
|
def get_riddle(self, riddle_id):
|
||||||
"Fetches a single riddle from the server"
|
"Fetches a single riddle from the server"
|
||||||
route = "/show"
|
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):
|
def get_random_riddle(self):
|
||||||
"Fetches all riddles from the server and then randomly returns one"
|
"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?")
|
route = "/random"
|
||||||
|
riddle_list = self.get_all_riddles()
|
||||||
|
return random.choice(riddle_list)
|
||||||
|
|
||||||
|
|
||||||
def add_riddle(self, question, answer):
|
def add_riddle(self, question, answer):
|
||||||
"Adds a new riddle to the server"
|
"Adds a new riddle to the server"
|
||||||
route = "/new"
|
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'])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
28
notes.md
28
notes.md
@@ -7,7 +7,35 @@ 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
|
You are welcome to research the meanings of these headers, but it's also
|
||||||
fine to speculate for now.
|
fine to speculate for now.
|
||||||
|
|
||||||
|
- Line 6 (Host): I think this line shows where the pulled data is hosted, in this case the mwc website.
|
||||||
|
|
||||||
|
- Line 7: (User-Agent): I was curious why this line said "HTTPie" instead of "HTTP" so I wanted to research it. Apparently this line shows a version of HTTP that is used for APIs, improving the user-friendliness.
|
||||||
|
|
||||||
|
- Line 4: (Connection): I think this line ensures that the connection between the host and the user stays "alive" until the user (maybe also host?) shuts it down.
|
||||||
|
|
||||||
|
- Line 14 (Content-Type): This line shows the type of program the user operates.
|
||||||
|
|
||||||
|
- Line 18 (Server): I think this line shows the version and name of the program the user operates.
|
||||||
|
|
||||||
## Checkpoint 2
|
## Checkpoint 2
|
||||||
The goal of this checkpoint is to see what status codes you can get back from
|
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
|
the riddle server. Paste below several `http` requests and the status codes
|
||||||
they return.
|
they return.
|
||||||
|
|
||||||
|
1.
|
||||||
|
http -v post https://riddles.makingwithcode.org/guess id=20 answer="to get to the other side"
|
||||||
|
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
|
||||||
|
|
||||||
|
2.
|
||||||
|
http -v get https://riddles.makingwithcode.org/answer id=4
|
||||||
|
HTTP/1.1 404 Not Found
|
||||||
|
|
||||||
|
3.
|
||||||
|
http -v post https://riddles.makingwithcode.org/new question="Why did the fly never land on the computer" answer="He was afraid of the world wide web"
|
||||||
|
HTTP/1.1 200 OK
|
||||||
|
|
||||||
|
4.
|
||||||
|
http -v post https://riddles.makingwithcode.org/guess id=11 answer="it's me"
|
||||||
|
HTTP/1.1 200 OK
|
||||||
@@ -6,7 +6,9 @@ authors = [{ name = "Chris Proctor", email = "chris@chrisproctor.net" }]
|
|||||||
requires-python = ">=3.10,<4.0"
|
requires-python = ">=3.10,<4.0"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
license = { text = "MIT" }
|
license = { text = "MIT" }
|
||||||
dependencies = ["requests (>=2.32.3,<3.0.0)"]
|
dependencies = [
|
||||||
|
"requests>=2.32.3,<3.0.0",
|
||||||
|
]
|
||||||
|
|
||||||
[build-system]
|
[build-system]
|
||||||
requires = ["hatchling"]
|
requires = ["hatchling"]
|
||||||
|
|||||||
Reference in New Issue
Block a user