def do_GET(self): # Set the response code to 'Ok' self._set_headers(200) # Your new console.log() that outputs to the terminal print(self.path) # It's an if..else statement # if self.path == "/animals": # # In Python, this is a list of dictionaries # # In JavaScript, you would call it an array of objects # response = [ # { "id": 1, "name": "Snickers", "species": "Dog" }, # { "id": 2, "name": "Lenny", "species": "Cat" } # ] # else: # response = [] if self.path == "/animals": response = get_all_animals() else: response = [] # This weird code sends a response back to the client self.wfile.write(f"{response}".encode())
def do_GET(self): self._set_headers(200) response = {} # Parse URL and store entire tuple in a variable parsed = self.parse_url(self.path) # Response from parse_url() is a tuple with 2 # items in it, which means the request was for # `/animals` or `/animals/2` if len(parsed) == 2: ( resource, id ) = parsed if resource == "animals": if id is not None: response = get_single_animal(id) else: response = get_all_animals() if resource == "locations": if id is not None: response = get_single_location(id) else: response = get_all_locations() if resource == "employees": if id is not None: response = get_single_employee(id) else: response = get_all_employees() if resource == "customers": if id is not None: response = get_single_customer(id) else: response = get_all_customers() # Response from parse_url() is a tuple with 3 # items in it, which means the request was for # `/resource?parameter=value` elif len(parsed) == 3: ( resource, key, value ) = parsed # Is the resource `customers` and was there a # query parameter that specified the customer # email as a filtering value? if key == "email" and resource == "customers": response = get_customers_by_email(value) if key == "location_id" and resource == "animals": response = get_animals_by_location_id(value) if key == "location_id" and resource == "employees": response = get_employees_by_location_id(value) if key == "status" and resource == "animals": response = f"{get_animals_by_status(value)}" self.wfile.write((response).encode())
def do_GET(self): # Set the response code to 'Ok' self._set_headers(200) # Your new console.log() that outputs to the terminal print(self.path) # It's an if..else statement if self.path == "/animals": response = get_all_animals() # In Python, this is a list of dictionaries # In JavaScript, you would call it an array of objects else: response = [] # This weird code sends a response back to the client self.wfile.write(f"{response}".encode())
def do_GET(self): self._set_headers(200) print(self.path) parsed = self.parse_url(self.path) if len(parsed) == 2: (resource, id) = parsed if resource == 'animals': if id is not None: response = get_single_animal(id) else: response = get_all_animals() elif len(parsed) == 3: (resource, key, value) = parsed if key == "email" and resource == 'customers': response = get_customers_by_email(value) self.wfile.write(response.encode())