generated from mwc/lab_riddles
Completed riddles lab.
This commit is contained in:
21
api.py
21
api.py
@@ -36,17 +36,26 @@ 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?")
|
riddle_list = self.get_all_riddles()
|
||||||
|
return 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'])
|
||||||
|
|
||||||
|
#print(f"The server sent back: {response.json()}")
|
||||||
Reference in New Issue
Block a user