generated from mwc/lab_riddles
completed api.py so that client.py works
This commit is contained in:
parent
1c2ee8c4a0
commit
b00517a8b0
Binary file not shown.
20
api.py
20
api.py
|
@ -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, 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):
|
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?")
|
riddles = self.get_all_riddles()
|
||||||
|
return choice(riddles)
|
||||||
|
#raise NotImplementedError("The API doesn't support `get_random_riddle` yet. Can you add it?")
|
||||||
|
|
||||||
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'])
|
||||||
|
#raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue