I tried and I dont think this works.

1. When you connect to a 3-d printer each device musut send a GET and POST requests
200 will verify that you are connected and these are connected to each other.

2. I find this extremely interesting yet complicated. I was able to use
my understanding of a status codes to understand a website was down
that we were trying to use in school.
This commit is contained in:
louiscooper136 2024-04-25 20:42:38 -04:00
parent 0c8f749f78
commit 352eef9af7
2 changed files with 96 additions and 14 deletions

106
api.py
View File

@ -16,36 +16,114 @@ class RiddleAPI:
def get_all_riddles(self):
"Fetches all the riddles from the server"
route = "/all"
response = requests.get(self.server_url + route)
response = requests.get(self.server_url + "/all")
if response.ok:
return response.json()['riddles']
else:
raise APIError(response.json()['errors'])
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"
route = "/guess"
params = {'id': riddle_id, 'answer': guess}
response = requests.post(self.server_url + route, json=params)
response = requests.post(self.server_url + "/guess", json=params)
if response.ok:
return response.json()
return response.json()['correct']
else:
raise APIError(response.json()['errors'])
raise APIError(response.json().get('errors', 'Unknown 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?")
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 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?")
"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"
route = "/new"
raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?")
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 =

View File

@ -21,3 +21,7 @@ Lne 23: Thats the beginning of the content in a dictionary or tuple I think.
The goal of this checkpoint is to see what status codes you can get back from
the riddle server. Paste below several `http` requests and the status codes
they return.
200 returns when youre connected to the server
400 returns when you miss something in the post