generated from mwc/lab_riddles
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:
80
api.py
80
api.py
@@ -3,50 +3,66 @@
|
|||||||
# By Chris Proctor
|
# 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
|
import requests
|
||||||
from random import choice
|
from random import choice
|
||||||
|
|
||||||
|
|
||||||
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"
|
||||||
|
|
||||||
|
|
||||||
class RiddleAPI:
|
class RiddleAPI:
|
||||||
"Provides an easy way for Python programs to interact with a Riddle Server"
|
"Provides an easy way for Python programs to interact with a Riddle Server"
|
||||||
def __init__(self, server_url):
|
def __init__(self, server_url):
|
||||||
self.server_url = 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):
|
def get_all_riddles(self):
|
||||||
"Submits a guess to the server. Returns True or False"
|
"Fetches all the riddles from the server"
|
||||||
route = "/guess"
|
route = "/all"
|
||||||
params = {'id': riddle_id, 'answer': guess}
|
response = requests.get(self.server_url + route)
|
||||||
response = requests.post(self.server_url + route, json=params)
|
if response.ok:
|
||||||
if response.ok:
|
return response.json()['riddles']
|
||||||
return response.json()
|
else:
|
||||||
else:
|
raise APIError(response.json()['errors'])
|
||||||
raise APIError(response.json()['errors'])
|
|
||||||
|
|
||||||
def get_riddle(self, riddle_id):
|
def guess_riddle(self, riddle_id, guess):
|
||||||
"Fetches a single riddle from the server"
|
"Submits a guess to the server. Returns True or False"
|
||||||
route = "/show"
|
route = "/guess"
|
||||||
raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?")
|
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_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):
|
def get_riddle(self, riddle_id):
|
||||||
"Adds a new riddle to the server"
|
"Fetches a single riddle from the server"
|
||||||
route = "/new"
|
route = "/show"
|
||||||
raise NotImplementedError("The API doesn't support `add_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"
|
||||||
|
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?")
|
||||||
27
notes.md
27
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
|
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 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
|
## 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.
|
||||||
|
|
||||||
|
### 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
|
||||||
|
|||||||
Reference in New Issue
Block a user