From 06b4ecc96c3429fcbd366410cd617f8c96bee2cd Mon Sep 17 00:00:00 2001 From: bytesparallel Date: Wed, 12 Jan 2022 13:32:51 -0600 Subject: [PATCH] saving changes --- request_handler.py | 53 ++++++++++++++++++++++++++-------------- views/__init__.py | 2 +- views/animal_requests.py | 17 ++++++++++++- views/employees_requests | 0 views/location_requests | 0 5 files changed, 52 insertions(+), 20 deletions(-) delete mode 100644 views/employees_requests delete mode 100644 views/location_requests diff --git a/request_handler.py b/request_handler.py index 6b168ac..76e3fdb 100644 --- a/request_handler.py +++ b/request_handler.py @@ -1,13 +1,34 @@ from http.server import BaseHTTPRequestHandler, HTTPServer -from views import get_all_animals +from views import get_all_animals, get_single_animal from http.server import BaseHTTPRequestHandler, HTTPServer + # Here's a class. It inherits from another class. # For now, think of a class as a container for functions that # 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,32 +55,28 @@ 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) + def do_GET(self): + 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 = [] - - # This weird code sends a response back to the client - self.wfile.write(f"{response}".encode()) + self.wfile.write(response.encode()) # Here's a method on the class that overrides the parent's method. # It handles any POST request. diff --git a/views/__init__.py b/views/__init__.py index fa5bd8d..3f9c8fe 100644 --- a/views/__init__.py +++ b/views/__init__.py @@ -1 +1 @@ -from .animal_requests import get_all_animals \ No newline at end of file +from .animal_requests import get_all_animals, get_single_animal \ No newline at end of file diff --git a/views/animal_requests.py b/views/animal_requests.py index 75baa55..8d04552 100644 --- a/views/animal_requests.py +++ b/views/animal_requests.py @@ -24,4 +24,19 @@ ANIMALS = [ def get_all_animals(): - return ANIMALS \ No newline at end of file + 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 \ No newline at end of file diff --git a/views/employees_requests b/views/employees_requests deleted file mode 100644 index e69de29..0000000 diff --git a/views/location_requests b/views/location_requests deleted file mode 100644 index e69de29..0000000