diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..4a96c22 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +source .venv/bin/activate \ No newline at end of file diff --git a/api.py b/api.py index 35759bf..ff6531b 100644 --- a/api.py +++ b/api.py @@ -1,52 +1,68 @@ # RiddleAPI # --------- # By Chris Proctor -# The Riddle API takes care of connecting to the server. +# 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" + "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" - route = "/all" - response = requests.get(self.server_url + route) - if response.ok: - return response.json()['riddles'] - else: - raise APIError(response.json()['errors']) - - 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) - if response.ok: - return response.json() - else: - raise APIError(response.json()['errors']) - - 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?") - - 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?") - - 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?") + "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" + route = "/all" + response = requests.get(self.server_url + route) + if response.ok: + return response.json()['riddles'] + else: + raise APIError(response.json()['errors']) + + 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) + if response.ok: + return response.json() + else: + raise APIError(response.json()['errors']) + + def get_riddle(self, riddle_id): + "Fetches a single riddle from the server" + route = "/show" + 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']) + #raise NotImplementedError("The API doesn't support `get_riddle` yet. Can you add it?") + + + + def get_random_riddle(self): + "Fetches all riddles from the server and then randomly returns one" + allriddles = self.get_all_riddles() + return choice(allriddles) + + def add_riddle(self, question, answer): + "Adds a new riddle to the server" + route = "/new" + 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']) + #raise NotImplementedError("The API doesn't support `add_riddle` yet. Can you add it?") \ No newline at end of file diff --git a/notes.md b/notes.md index 3a338be..ec317d1 100644 --- a/notes.md +++ b/notes.md @@ -7,7 +7,34 @@ about the meaning of the line, and some situation in which it might be useful. You are welcome to research the meanings of these headers, but it's also fine to speculate for now. +Line 1: command to get data from the url given +Line 2: sent request to server +Line 3: I will accept any data type +Line 4: Accepts gzip files which are compresseed files +Line 5: Continued connection to server + + ## Checkpoint 2 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. + +### Request 1 +http -v post https://riddles.makingwithcode.org/new riddle="What kind of man has bats on the brain?" answer="Batman" + + +Code: 400 Bad Request + + +### Request 2 +http -v get https://riddles.makingwithcode.org/something + + +Code: 404 Not Found + + +### Request 3 +http -v post https://riddles.makingwithcode.org/all + + +Code: 405 Method Not Allowed