Archived
Merge pull request #1 from JMR-dev/jr-ch3&4
Merge ch3 and 4 work to main
This commit was merged in pull request #1.
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",
|
||||
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"python.pythonPath": "/Users/jason.ross/.pyenv/versions/3.9.1/bin/python"
|
||||
}
|
||||
+129
-25
@@ -1,6 +1,10 @@
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from views import get_all_animals
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
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
|
||||
|
||||
|
||||
|
||||
# Here's a class. It inherits from another class.
|
||||
@@ -8,6 +12,26 @@ from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
# work together for a common purpose. In this case, that
|
||||
# common purpose is to respond to HTTP requests from a client.
|
||||
class HandleRequests(BaseHTTPRequestHandler):
|
||||
def parse_url(self, path):
|
||||
# Just like splitting a string in JavaScript. If the
|
||||
# path is "/animals/1", the resulting list will
|
||||
# have "" at index 0, "animals" at index 1, and "1"
|
||||
# at index 2.
|
||||
path_params = path.split("/")
|
||||
resource = path_params[1]
|
||||
id = None
|
||||
|
||||
# Try to get the item at index 2
|
||||
try:
|
||||
# Convert the string "1" to the integer 1
|
||||
# This is the new parseInt()
|
||||
id = int(path_params[2])
|
||||
except IndexError:
|
||||
pass # No route parameter exists: /animals
|
||||
except ValueError:
|
||||
pass # Request had trailing slash: /animals/
|
||||
|
||||
return (resource, id) # This is a tuple
|
||||
# This is a Docstring it should be at the beginning of all classes and functions
|
||||
# It gives a description of the class or function
|
||||
"""Controls the functionality of any GET, PUT, POST, DELETE requests to the server
|
||||
@@ -34,53 +58,133 @@ class HandleRequests(BaseHTTPRequestHandler):
|
||||
self.send_response(200)
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Access-Control-Allow-Methods',
|
||||
'GET, POST, PUT, DELETE')
|
||||
'GET, POST, PUT, DELETE')
|
||||
self.send_header('Access-Control-Allow-Headers',
|
||||
'X-Requested-With, Content-Type, Accept')
|
||||
'X-Requested-With, Content-Type, Accept')
|
||||
self.end_headers()
|
||||
|
||||
# Here's a method on the class that overrides the parent's method.
|
||||
# It handles any GET request.
|
||||
def do_GET(self):
|
||||
"""Handles GET requests to the server
|
||||
"""
|
||||
# Set the response code to 'Ok'
|
||||
self._set_headers(200)
|
||||
response = {} # Default response
|
||||
|
||||
# Your new console.log() that outputs to the terminal
|
||||
print(self.path)
|
||||
# Parse the URL and capture the tuple that is returned
|
||||
(resource, id) = self.parse_url(self.path)
|
||||
|
||||
if self.path == "/animals":
|
||||
response = get_all_animals()
|
||||
# It's an if..else statement
|
||||
if resource == "animals":
|
||||
if id is not None:
|
||||
response = f"{get_single_animal(id)}"
|
||||
|
||||
else:
|
||||
response = f"{get_all_animals()}"
|
||||
|
||||
else:
|
||||
response = []
|
||||
|
||||
|
||||
if resource == "locations":
|
||||
if id is not None:
|
||||
response = f"{get_single_location(id)}"
|
||||
|
||||
# This weird code sends a response back to the client
|
||||
self.wfile.write(f"{response}".encode())
|
||||
else:
|
||||
response = f"{get_all_locations()}"
|
||||
|
||||
|
||||
|
||||
if resource == "employees":
|
||||
if id is not None:
|
||||
response = f"{get_single_employee(id)}"
|
||||
|
||||
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_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_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_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())
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# This function is not inside the class. It is the starting
|
||||
|
||||
+6
-1
@@ -1 +1,6 @@
|
||||
from .animal_requests import get_all_animals
|
||||
|
||||
from .employees_requests import get_all_employees, get_single_employee, create_employee, delete_employee
|
||||
from .location_requests import get_all_locations, get_single_location, create_location, delete_location
|
||||
from .animal_requests import create_animal, delete_animal, get_all_animals, get_single_animal, update_animal
|
||||
from .customer_requests import get_all_customers, get_single_customer, create_customer, delete_customer
|
||||
|
||||
|
||||
@@ -4,24 +4,83 @@ ANIMALS = [
|
||||
"name": "Snickers",
|
||||
"species": "Dog",
|
||||
"locationId": 1,
|
||||
"customerId": 4
|
||||
"customerId": 4,
|
||||
"status": "Admitted"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Gypsy",
|
||||
"species": "Dog",
|
||||
"locationId": 1,
|
||||
"customerId": 2
|
||||
"customerId": 2,
|
||||
"status": "Admitted"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Blue",
|
||||
"species": "Cat",
|
||||
"locationId": 2,
|
||||
"customerId": 1
|
||||
"customerId": 1,
|
||||
"status": "Admitted"
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
def get_all_animals():
|
||||
return ANIMALS
|
||||
return ANIMALS
|
||||
|
||||
# Function with a single parameter
|
||||
def get_single_animal(id):
|
||||
# Variable to hold the found animal, if it exists
|
||||
requested_animal = None
|
||||
|
||||
# Iterate the ANIMALS list above. Very similar to the
|
||||
# for..of loops you used in JavaScript.
|
||||
for animal in ANIMALS:
|
||||
# Dictionaries in Python use [] notation to find a key
|
||||
# instead of the dot notation that JavaScript used.
|
||||
if animal["id"] == id:
|
||||
requested_animal = 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
|
||||
|
||||
def delete_animal(id):
|
||||
# Initial -1 value for animal index, in case one isn't found
|
||||
animal_index = -1
|
||||
|
||||
# Iterate the ANIMALS list, but use enumerate() so that you
|
||||
# can access the index value of each item
|
||||
for index, animal in enumerate(ANIMALS):
|
||||
if animal["id"] == id:
|
||||
# Found the animal. Store the current index.
|
||||
animal_index = index
|
||||
|
||||
# If the animal was found, use pop(int) to remove it from list
|
||||
if animal_index >= 0:
|
||||
ANIMALS.pop(animal_index)
|
||||
|
||||
|
||||
def update_animal(id, new_animal):
|
||||
# Iterate the ANIMALS list, but use enumerate() so that
|
||||
# you can access the index value of each item.
|
||||
for index, animal in enumerate(ANIMALS):
|
||||
if animal["id"] == id:
|
||||
# Found the animal. Update the value.
|
||||
ANIMALS[index] = new_animal
|
||||
break
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
CUSTOMERS = [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Ryan Tanay",
|
||||
"email": "ryan@tanay.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Emma Beaton",
|
||||
"email": "emma@beaton.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Dani Adkins",
|
||||
"email": "dani.adkins.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"name": "Adam Oswalt",
|
||||
"email": "adam@oswalt.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"name": "Fletcher Bangs",
|
||||
"email": "flangs@bangs.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"id": 6,
|
||||
"name": "Angela Lee",
|
||||
"email": "lee@lee.com",
|
||||
"employee": False
|
||||
},
|
||||
{
|
||||
"name": "mike mike",
|
||||
"email": "m@m.com",
|
||||
"employee": False,
|
||||
"id": 7
|
||||
},
|
||||
|
||||
]
|
||||
|
||||
def get_all_customers():
|
||||
return CUSTOMERS
|
||||
|
||||
def get_single_customer():
|
||||
requested_customer = None
|
||||
|
||||
|
||||
for customer in CUSTOMERS:
|
||||
if customer["id"] == id:
|
||||
requested_customer = customer
|
||||
|
||||
return requested_customer
|
||||
|
||||
def create_customer(customer):
|
||||
# Get the id value of the last location in the list
|
||||
max_id = CUSTOMERS[-1]["id"]
|
||||
|
||||
# Add 1 to whatever that number is
|
||||
new_id = max_id + 1
|
||||
|
||||
# Add an `id` property to the location dictionary
|
||||
customer["id"] = new_id
|
||||
|
||||
# Add the location dictionary to the list
|
||||
CUSTOMERS.append(customer)
|
||||
|
||||
# Return the dictionary with `id` property added
|
||||
return customer
|
||||
|
||||
def delete_customer(id):
|
||||
# Initial -1 value for customer index, in case one isn't found
|
||||
customer_index = -1
|
||||
|
||||
# Iterate the CUSTOMERS list, but use enumerate() so that you
|
||||
# can access the index value of each item
|
||||
for index, customer in enumerate(CUSTOMERS):
|
||||
if customer["id"] == id:
|
||||
# Found the customer. Store the current index.
|
||||
customer_index = index
|
||||
|
||||
# If the customer was found, use pop(int) to remove it from list
|
||||
if customer_index >= 0:
|
||||
CUSTOMERS.pop(customer_index)
|
||||
|
||||
@@ -0,0 +1,124 @@
|
||||
EMPLOYEES = [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Jessica Younker",
|
||||
"email": "jessica@younker.com",
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Jordan Nelson",
|
||||
"email": "jordan@nelson.com",
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"name": "Zoe LeBlanc",
|
||||
"email": "zoe@leblanc.com",
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Meg Ducharme",
|
||||
"email": "meg@ducharme.com",
|
||||
"id": 4,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Hannah Hall",
|
||||
"email": "hannah@hall.com",
|
||||
"id": 5,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Emily Lemmon",
|
||||
"email": "emily@lemmon.com",
|
||||
"id": 6,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Jordan Castelloe",
|
||||
"email": "jordan@castelloe.com",
|
||||
"id": 7,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Leah Gwin",
|
||||
"email": "leah@gwin.com",
|
||||
"id": 8,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Caitlin Stein",
|
||||
"email": "caitlin@stein.com",
|
||||
"id": 9,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Greg Korte",
|
||||
"email": "greg@korte.com",
|
||||
"id": 10,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Charisse Lambert",
|
||||
"email": "charisse@lambert.com",
|
||||
"id": 11,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Madi Peper",
|
||||
"email": "madi@peper.com",
|
||||
"id": 12,
|
||||
"employee": True
|
||||
},
|
||||
{
|
||||
"name": "Jenna Solis",
|
||||
"email": "jenna@solis.com",
|
||||
"id": 14,
|
||||
"employee": True
|
||||
},
|
||||
|
||||
]
|
||||
|
||||
def get_all_employees():
|
||||
return EMPLOYEES
|
||||
|
||||
def get_single_employee():
|
||||
requested_employee = None
|
||||
|
||||
|
||||
for employee in EMPLOYEES:
|
||||
if EMPLOYEES["id"] == id:
|
||||
requested_EMPLOYEE = employee
|
||||
|
||||
return requested_employee
|
||||
|
||||
def create_employee(employee):
|
||||
# Get the id value of the last location in the list
|
||||
max_id = EMPLOYEES[-1]["id"]
|
||||
|
||||
# Add 1 to whatever that number is
|
||||
new_id = max_id + 1
|
||||
|
||||
# Add an `id` property to the location dictionary
|
||||
employee["id"] = new_id
|
||||
|
||||
# Add the location dictionary to the list
|
||||
EMPLOYEES.append(employee)
|
||||
|
||||
# Return the dictionary with `id` property added
|
||||
return employee
|
||||
def delete_employee(id):
|
||||
# Initial -1 value for employee index, in case one isn't found
|
||||
employee_index = -1
|
||||
|
||||
# Iterate the EMPLOYEES list, but use enumerate() so that you
|
||||
# can access the index value of each item
|
||||
for index, employee in enumerate(EMPLOYEES):
|
||||
if employee["id"] == id:
|
||||
# Found the employee. Store the current index.
|
||||
employee_index = index
|
||||
|
||||
# If the employee was found, use pop(int) to remove it from list
|
||||
if employee_index >= 0:
|
||||
EMPLOYEES.pop(employee_index)
|
||||
@@ -0,0 +1,56 @@
|
||||
LOCATIONS = [
|
||||
{
|
||||
"id": 1,
|
||||
"name": "Nashville North"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"name": "Nashville South"
|
||||
}
|
||||
,
|
||||
]
|
||||
|
||||
def get_all_locations():
|
||||
return LOCATIONS
|
||||
|
||||
def get_single_location():
|
||||
requested_location = None
|
||||
|
||||
|
||||
for location in LOCATIONS:
|
||||
if location["id"] == id:
|
||||
requested_location = location
|
||||
|
||||
return requested_location
|
||||
|
||||
def create_location(location):
|
||||
# Get the id value of the last location in the list
|
||||
max_id = LOCATIONS[-1]["id"]
|
||||
|
||||
# Add 1 to whatever that number is
|
||||
new_id = max_id + 1
|
||||
|
||||
# Add an `id` property to the location dictionary
|
||||
location["id"] = new_id
|
||||
|
||||
# Add the location dictionary to the list
|
||||
LOCATIONS.append(location)
|
||||
|
||||
# Return the dictionary with `id` property added
|
||||
return location
|
||||
|
||||
def delete_location(id):
|
||||
# Initial -1 value for location index, in case one isn't found
|
||||
location_index = -1
|
||||
|
||||
# Iterate the LOCATIONS list, but use enumerate() so that you
|
||||
# can access the index value of each item
|
||||
for index, location in enumerate(LOCATIONS):
|
||||
if location["id"] == id:
|
||||
# Found the location. Store the current index.
|
||||
location_index = index
|
||||
|
||||
# If the location was found, use pop(int) to remove it from list
|
||||
if location_index >= 0:
|
||||
LOCATIONS.pop(location_index)
|
||||
|
||||
Reference in New Issue
Block a user