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:
94
api.py
94
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?")
|
||||
Reference in New Issue
Block a user