completed ch. 3 and 4

This commit is contained in:
2022-01-19 09:12:06 -06:00
parent cb4a93108c
commit 449b94d3fd
7 changed files with 191 additions and 23 deletions
+56 -12
View File
@@ -1,10 +1,8 @@
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
from views.customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer
from views.employees_requests import create_employee, get_all_employees, get_single_employee, delete_employee
from views.location_requests import create_location, get_all_locations, get_single_location, delete_location
from views.animal_requests import create_animal, get_all_animals, get_single_animal, delete_animal, update_animal
import json
@@ -123,23 +121,69 @@ class HandleRequests(BaseHTTPRequestHandler):
(resource, id) = self.parse_url(self.path)
# Initialize new animal
new_animal = None
new_obj = 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)
new_obj = create_animal(post_body)
if resource == "location":
new_obj = create_location(post_body)
if resource == "employees":
new_obj = create_employee(post_body)
if resource == "customers":
new_obj = create_customer(post_body)
# Encode the new animal and send in response
self.wfile.write(f"{new_animal}".encode())
self.wfile.write(f"{new_obj}".encode())
# Here's a method on the class that overrides the parent's method.
# It handles any PUT request.
def do_PUT(self):
"""Handles PUT requests to the server
"""
self.do_POST()
self._set_headers(204)
content_len = int(self.headers.get('content-length', 0))
post_body = self.rfile.read(content_len)
post_body = json.loads(post_body)
# Parse the URL
(resource, id) = self.parse_url(self.path)
# Delete a single animal from the list
if resource == "animals":
update_animal(id, post_body)
# Encode the new animal and send in response
self.wfile.write("".encode())
def do_DELETE(self):
# Set a 204 response code
self._set_headers(204)
# Parse the URL
(resource, id) = self.parse_url(self.path)
# Delete a single animal from the list
if resource == "animals":
delete_animal(id)
if resource == "customers":
delete_customer(id)
if resource == "employees":
delete_employee(id)
if resource == "locations":
delete_location(id)
# Encode the new animal and send in response
self.wfile.write("".encode())