generated from mwc/lab_riddles
Implemented methods for api.py & updated notes.md
1. I use my phone daily. I assume every time I access a website and put in my username/password, I am sending out HTPP post requests to the server and it is sending me information back to be decoded by my phone. 2. It made me realize just how complicated everything is. All of these features that we take as commonplace had to be tediously tested and created by people before we want use them the way we do now.
This commit is contained in:
27
api.py
27
api.py
@@ -33,19 +33,34 @@ class RiddleAPI:
|
||||
else:
|
||||
raise APIError(response.json()['errors'])
|
||||
|
||||
def get_riddle(self, riddle_id):
|
||||
def get_riddle(self, riddle_id): #done
|
||||
"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'])
|
||||
|
||||
def get_random_riddle(self):
|
||||
def get_random_riddle(self): #done
|
||||
"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()
|
||||
count = len(allriddles)
|
||||
mychoice = choice(range(0,count))
|
||||
return self.get_riddle(mychoice)
|
||||
|
||||
def add_riddle(self, question, answer):
|
||||
def add_riddle(self, question, answer): #done
|
||||
"Adds a new riddle to the server"
|
||||
route = "/new"
|
||||
raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")
|
||||
allriddles = self.get_all_riddles()
|
||||
newid = len(allriddles)+1
|
||||
params = {"correct":0,"difficulty":0,"guesses":0,"id":newid,"question":question,"answer":answer}
|
||||
response = requests.post(self.server_url+route,json=params)
|
||||
if response.ok:
|
||||
print("Added successfully")
|
||||
else:
|
||||
raise APIError(response.json()['errors'])
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user