# RiddleAPI # --------- # By Chris Proctor # The Riddle API takes care of connecting to the server. import requests from random import choice class APIError(Exception): "A custom error we'll use when something goes wrong with the API" 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 def get_all_riddles(self): "Fetches all the riddles from the server" response = requests.get(self.server_url + "/all") if response.ok: return response.json()['riddles'] else: raise APIError(response.json().get('errors', 'Unknown error')) def guess_riddle(self, riddle_id, guess): "Submits a guess to the server. Returns True or False" params = {'id': riddle_id, 'answer': guess} response = requests.post(self.server_url + "/guess", json=params) if response.ok: return response.json()['correct'] else: raise APIError(response.json().get('errors', 'Unknown error')) def get_riddle(self, riddle_id): "Fetches a single riddle from the server" response = requests.get(self.server_url + "/show/{}".format(riddle_id)) if response.ok: return response.json() else: raise APIError(response.json().get('errors', 'Unknown error')) def get_random_riddle(self): "Fetches a random riddle from the server" riddles = self.get_all_riddles() if riddles: return choice(riddles) else: raise APIError("No riddles available") def add_riddle(self, question, answer): "Adds a new riddle to the server" data = {'question': question, 'answer': answer} response = requests.post(self.server_url + "/new", json=data) if response.ok: return response.json() else: raise APIError(response.json().get('errors', 'Unknown error')) class RiddleView: "Allows a player to interact with the Riddle Server from the Terminal" def __init__(self, url): self.api = RiddleAPI(url) def run(self): print("Welcome to the Riddler") print("Press control + c to quit") print("-" * 80) self.show_menu() def show_menu(self): choices = [ "Show riddles", "Random riddle", "Add a riddle" ] choice = self.get_choice("What do you want to do?", choices) if choice == 0: self.list_riddles() elif choice == 1: riddle = self.api.get_random_riddle() self.ask_riddle(riddle['id']) elif choice == 2: self.add_riddle() def list_riddles(self): riddles = self.api.get_all_riddles() if len(riddles) == 0: print("Sorry, there are no riddles on the server!") self.show_menu() else: choices = [riddle['question'] for riddle in riddles] choice = self.get_choice("Which riddle do you want to guess?", choices) riddle_id = riddles[choice]['id'] self.ask_riddle(riddle_id) def ask_riddle(self, riddle_id): riddle = self.api.get_riddle(riddle_id) print(riddle['question']) guess = input("> ") correct = self.api.guess_riddle(riddle_id, guess) if correct: print("Yes!") else: print("Nope, that's not the answer.") self.show_menu() def add_riddle(self): question = input("question: ") answer = input("answer: ") choices = ["Add it!", "Edit", "Never mind"] choice = self.get_choice(question + " " + answer, choices) if choice == 0: self.api.add_riddle(question, answer) self.show_menu() elif choice == 1: self.add_riddle() elif choice == 2: self.show_menu() def get_choice(self, prompt, choices): print(prompt) for i, choice in enumerate(choices): print("{}. {}".format(i, choice)) while True: selection =