generated from mwc/lab_riddles
Completed Checkpoints 2 and 3.
One Internet-enabled service that I use regularly is Life360. You can get a record of a family member's location history which seems like an HTTP get request. Creating a family group seems like an HTTP post. Yes, this lab has caused me to think differently about the tech interactions in my daily life. I never really considered before how different applications and devices communicate with info that is stored on a server. Of course I understood that all the information could not possibly be stored on a phone or laptop, but I was not completely aware of what was actually going on behind the scenes. I think I will be a lot more concious of the presence of HTTP requests now in my daily life.
This commit is contained in:
parent
404a674898
commit
6de3b3ab61
Binary file not shown.
18
api.py
18
api.py
|
@ -36,16 +36,28 @@ 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.get(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?")
|
||||
riddles = self.get_all_riddles()
|
||||
riddle = choice(riddles)
|
||||
return riddle
|
||||
|
||||
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'])
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue