generated from mwc/lab_riddles
I wrote the answers to both checkpoints and fixed the codes in api.py to implement client.py.
1. I use Google almost every day, especially for searching things quickly. When I type something into the search bar and press enter, my device sends an HTTP request to Google’s servers with my search words. Google then sends back an HTTP response with the search results, which show up on my screen. 2. Yes, this lab helped me understand that my devices are constantly communicating with servers without me even noticing.
This commit is contained in:
24
api.py
24
api.py
@@ -36,17 +36,31 @@ class RiddleAPI:
|
||||
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?")
|
||||
params = {'id': riddle_id}
|
||||
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?")
|
||||
|
||||
route = "/all"
|
||||
response = requests.get(self.server_url + route)
|
||||
if response.ok:
|
||||
riddles=response.json()['riddles']
|
||||
return choice(riddles)
|
||||
else:
|
||||
raise APIError(response.json()['errors'])
|
||||
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?")
|
||||
|
||||
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'])
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user