From cb4a93108c49f202aa4dcfe2efee38e73379eb7d Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Fri, 14 Jan 2022 10:48:24 -0600 Subject: [PATCH] working on post requests --- .vscode/launch.json | 1 + request_handler.py | 43 ++++++++++++++++++++++++++++--------- views/__init__.py | 8 ++++++- views/animal_requests.py | 18 +++++++++++++++- views/customer_requests.py | 14 ++++++------ views/employees_requests.py | 26 +++++++++++----------- 6 files changed, 78 insertions(+), 32 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index d8984a9..2b16460 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,6 +1,7 @@ { "version": "0.2.0", "configurations": [ + {"name":"Python: Current File","type":"python","request":"launch","program":"${file}","console":"integratedTerminal"}, { "name": "Request Handler", "type": "python", diff --git a/request_handler.py b/request_handler.py index 5262ef6..3762760 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,8 +1,11 @@ from http.server import BaseHTTPRequestHandler, HTTPServer from views import get_all_animals, get_single_animal from http.server import BaseHTTPRequestHandler, HTTPServer +from views.customer_requests import get_all_customers, get_single_customer from views.employees_requests import get_all_employees, get_single_employee from views.location_requests import get_all_locations, get_single_location +from views.animal_requests import create_animal +import json @@ -64,8 +67,8 @@ class HandleRequests(BaseHTTPRequestHandler): # Here's a method on the class that overrides the parent's method. # It handles any GET request. - def do_GET(self): - self._set_headers(200) + def do_GET(self): + self._set_headers(200) response = {} # Default response # Parse the URL and capture the tuple that is returned @@ -78,7 +81,7 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_animals()}" - self.wfile.write(response.encode()) + if resource == "locations": if id is not None: @@ -87,7 +90,7 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_locations()}" - self.wfile.write(response.encode()) + if resource == "employees": if id is not None: @@ -96,21 +99,40 @@ class HandleRequests(BaseHTTPRequestHandler): else: response = f"{get_all_employees()}" + + if resource == "customers": + if id is not None: + response = f"{get_single_customer(id)}" + + else: + response = f"{get_all_customers()}" + self.wfile.write(response.encode()) # Here's a method on the class that overrides the parent's method. # It handles any POST request. def do_POST(self): - """Handles POST requests to the server - """ - # Set response code to 'Created' self._set_headers(201) - content_len = int(self.headers.get('content-length', 0)) post_body = self.rfile.read(content_len) - response = f"received post request:
{post_body}" - self.wfile.write(response.encode()) + # Convert JSON string to a Python dictionary + post_body = json.loads(post_body) + + # Parse the URL + (resource, id) = self.parse_url(self.path) + + # Initialize new animal + new_animal = None + + # Add a new animal to the list. Don't worry about + # the orange squiggle, you'll define the create_animal + # function next. + if resource == "animals": + new_animal = create_animal(post_body) + + # Encode the new animal and send in response + self.wfile.write(f"{new_animal}".encode()) # Here's a method on the class that overrides the parent's method. # It handles any PUT request. @@ -118,6 +140,7 @@ class HandleRequests(BaseHTTPRequestHandler): """Handles PUT requests to the server """ self.do_POST() + # This function is not inside the class. It is the starting diff --git a/views/__init__.py b/views/__init__.py index 3f9c8fe..f195576 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1 +1,7 @@ -from .animal_requests import get_all_animals, get_single_animal \ No newline at end of file +from .animal_requests import get_all_animals, get_single_animal +from http.server import BaseHTTPRequestHandler, HTTPServer +from views import get_all_animals, get_single_animal +from http.server import BaseHTTPRequestHandler, HTTPServer +from views.employees_requests import get_all_employees, get_single_employee +from views.location_requests import get_all_locations, get_single_location +from views.animal_requests import create_animal diff --git a/views/animal_requests.py b/views/animal_requests.py index 8d04552..ef1fe97 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -39,4 +39,20 @@ def get_single_animal(id): if animal["id"] == id: requested_animal = animal - return requested_animal \ No newline at end of file + return requested_animal + +def create_animal(animal): + # Get the id value of the last animal in the list + max_id = ANIMALS[-1]["id"] + + # Add 1 to whatever that number is + new_id = max_id + 1 + + # Add an `id` property to the animal dictionary + animal["id"] = new_id + + # Add the animal dictionary to the list + ANIMALS.append(animal) + + # Return the dictionary with `id` property added + return animal diff --git a/views/customer_requests.py b/views/customer_requests.py index ff0c418..2e27b8f 100644 --- a/views/customer_requests.py +++ b/views/customer_requests.py @@ -3,42 +3,42 @@ CUSTOMERS = [ "id": 1, "name": "Ryan Tanay", "email": "ryan@tanay.com", - "employee": false + "employee": False }, { "id": 2, "name": "Emma Beaton", "email": "emma@beaton.com", - "employee": false + "employee": False }, { "id": 3, "name": "Dani Adkins", "email": "dani.adkins.com", - "employee": false + "employee": False }, { "id": 4, "name": "Adam Oswalt", "email": "adam@oswalt.com", - "employee": false + "employee": False }, { "id": 5, "name": "Fletcher Bangs", "email": "flangs@bangs.com", - "employee": false + "employee": False }, { "id": 6, "name": "Angela Lee", "email": "lee@lee.com", - "employee": false + "employee": False }, { "name": "mike mike", "email": "m@m.com", - "employee": false, + "employee": False, "id": 7 }, diff --git a/views/employees_requests.py b/views/employees_requests.py index 80c4632..cb96176 100644 --- a/views/employees_requests.py +++ b/views/employees_requests.py @@ -3,79 +3,79 @@ EMPLOYEES = [ "id": 1, "name": "Jessica Younker", "email": "jessica@younker.com", - "employee": true + "employee": True }, { "id": 2, "name": "Jordan Nelson", "email": "jordan@nelson.com", - "employee": true + "employee": True }, { "id": 3, "name": "Zoe LeBlanc", "email": "zoe@leblanc.com", - "employee": true + "employee": True }, { "name": "Meg Ducharme", "email": "meg@ducharme.com", "id": 4, - "employee": true + "employee": True }, { "name": "Hannah Hall", "email": "hannah@hall.com", "id": 5, - "employee": true + "employee": True }, { "name": "Emily Lemmon", "email": "emily@lemmon.com", "id": 6, - "employee": true + "employee": True }, { "name": "Jordan Castelloe", "email": "jordan@castelloe.com", "id": 7, - "employee": true + "employee": True }, { "name": "Leah Gwin", "email": "leah@gwin.com", "id": 8, - "employee": true + "employee": True }, { "name": "Caitlin Stein", "email": "caitlin@stein.com", "id": 9, - "employee": true + "employee": True }, { "name": "Greg Korte", "email": "greg@korte.com", "id": 10, - "employee": true + "employee": True }, { "name": "Charisse Lambert", "email": "charisse@lambert.com", "id": 11, - "employee": true + "employee": True }, { "name": "Madi Peper", "email": "madi@peper.com", "id": 12, - "employee": true + "employee": True }, { "name": "Jenna Solis", "email": "jenna@solis.com", "id": 14, - "employee": true + "employee": True }, ]