Archived
working on post requests
This commit is contained in:
+33
-10
@@ -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:<br>{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
|
||||
|
||||
Reference in New Issue
Block a user