generated from mwc/lab_riddles
question 1. I regularly use Spotify to stream
music on my phone. One feature I like is the personalized playlist recommendations on the home screen. Behind the scenes, my device likely sends an HTTP GET request to Spotify’s servers asking for recommended tracks based on my listening history. The server processes this request, runs algorithms to select songs I might like, and sends back an HTTP response containing the playlist data in JSON format. My app then parses that response and displays the recommended songs for me to play instantly. question 2. Yes, this lab made me realize that every app or website I use is constantly sending and receiving HTTP requests in the background. Even simple actions, like loading a playlist or checking the weather, involve multiple requests and responses I never notice. It makes me appreciate how much coordination happens behind the scenes to make technology feel instant and seamless.
This commit is contained in:
40
api.py
40
api.py
@@ -8,9 +8,11 @@ from random import choice
|
||||
|
||||
class APIError(Exception):
|
||||
"A custom error we'll use when something goes wrong with the API"
|
||||
pass
|
||||
|
||||
class RiddleAPI:
|
||||
"Provides an easy way for Python programs to interact with a Riddle Server"
|
||||
|
||||
def __init__(self, server_url):
|
||||
self.server_url = server_url
|
||||
|
||||
@@ -19,9 +21,9 @@ class RiddleAPI:
|
||||
route = "/all"
|
||||
response = requests.get(self.server_url + route)
|
||||
if response.ok:
|
||||
return response.json()['riddles']
|
||||
return response.json().get('riddles', [])
|
||||
else:
|
||||
raise APIError(response.json()['errors'])
|
||||
raise APIError(response.json().get('errors', 'Unknown API error'))
|
||||
|
||||
def guess_riddle(self, riddle_id, guess):
|
||||
"Submits a guess to the server. Returns True or False"
|
||||
@@ -31,22 +33,40 @@ class RiddleAPI:
|
||||
if response.ok:
|
||||
return response.json()
|
||||
else:
|
||||
raise APIError(response.json()['errors'])
|
||||
raise APIError(response.json().get('errors', 'Unknown API error'))
|
||||
|
||||
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, params=params)
|
||||
|
||||
if not response.ok:
|
||||
raise APIError(response.json().get('errors', 'Unknown API error'))
|
||||
|
||||
data = response.json()
|
||||
# Handle either 'riddles' list or 'riddle' key
|
||||
if 'riddles' in data and len(data['riddles']) > 0:
|
||||
return data['riddles'][0]
|
||||
elif 'riddle' in data:
|
||||
return data['riddle']
|
||||
else:
|
||||
raise APIError(f"No riddle found with id {riddle_id}")
|
||||
|
||||
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()
|
||||
if riddles:
|
||||
return choice(riddles)
|
||||
else:
|
||||
raise APIError("No riddles available to choose from")
|
||||
|
||||
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() # usually returns the new riddle info
|
||||
else:
|
||||
raise APIError(response.json().get('errors', 'Unknown API error'))
|
||||
Reference in New Issue
Block a user