Archived
working on post requests
This commit is contained in:
Vendored
+1
@@ -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",
|
||||
|
||||
+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
|
||||
|
||||
+7
-1
@@ -1 +1,7 @@
|
||||
from .animal_requests import get_all_animals, get_single_animal
|
||||
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
|
||||
|
||||
@@ -39,4 +39,20 @@ def get_single_animal(id):
|
||||
if animal["id"] == id:
|
||||
requested_animal = animal
|
||||
|
||||
return requested_animal
|
||||
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
|
||||
|
||||
@@ -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
|
||||
},
|
||||
|
||||
|
||||
+13
-13
@@ -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
|
||||
},
|
||||
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user