Ejemplo n.º 1
0
    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 = f"{get_single_animal(id)}"
                else:
                    response = f"{get_all_animals()}"
            elif resource == "customers":
                if id is not None:
                    response = f"{get_single_customer(id)}"
                else:
                    response = f"{get_all_customers()}"
            elif resource == "locations":
                if id is not None:
                    response = f"{get_single_location(id)}"
                else:
                    response = f"{get_all_locations()}"
            # If URL resource = employees

            elif resource == "employees":
                if id is not None:
                    response = f"{get_single_employee(id)}"

                else:
                    response = f"{get_all_employees()}"

        # 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)

            elif key == "location_id" and resource == "animals":
                response = get_animal_by_location(value)

            elif key == "location_id" and resource == "employees":
                response = get_employee_by_location(value)

            elif key == "status" and resource == "animals":
                response = get_animals_by_status(value)

        self.wfile.write(response.encode())
Ejemplo n.º 2
0
    def do_GET(self):
        # Set the response code to 'Ok'
        self._set_headers(200)
        response = {}  # Default response

        #Parse the URL and store enture tuple in a variable
        parsed = self.parse_url(self.path)

        # Response from parse_url() is a tuple with 2 items
        # 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 = f"{get_single_animal(id)}"
                else:
                    response = f"{get_all_animals()}"

            if resource == "locations":
                if id is not None:
                    response = f"{get_single_location(id)}"

                else:
                    response = f"{get_all_locations()}"

            if resource == "customers":
                if id is not None:
                    response = f"{get_single_customer(id)}"
                else:
                    response = f"{get_all_customers()}"

            if resource == "employees":
                if id is not None:
                    response = f"{get_single_employee(id)}"
                else:
                    response = f"{get_all_employees()}"

            # Response from parse_url() is a tuple with 3 items
            # 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 param that specified the customer email as a filtering val?
            if key == "email" and resource == "customers":
                response = get_customers_by_email(value)

            elif key == "location_id" and resource == "animals":
                response = get_animals_by_location(value)

            elif key == "location_id" and resource == "employees":
                response = get_employees_by_location(value)

            elif key == "status" and resource == "animals":
                response = get_animals_by_status(value)

        # This weird code sends a response back to the client
        self.wfile.write(response.encode())
Ejemplo n.º 3
0
    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)
        parsed = self.parse_url(self.path)

        # It's an if..else statement
        if len(parsed) == 2:
            (resource, id) = parsed

            if resource == "animals":
                if id is not None:
                    response = f"{get_single_animal(id)}"
                else:
                    response = f"{get_all_animals()}"

            elif resource == "locations":
                if id is not None:
                    response = f"{get_single_location(id)}"
                else:
                    response = f"{get_all_locations()}"

            elif resource == "employees":
                if id is not None:
                    response = f"{get_single_employee(id)}"
                else:
                    response = f"{get_all_employees()}"

            elif resource == "customers":
                if id is not None:
                    response = f"{get_single_customer(id)}"
                else:
                    response = f"{get_all_customers()}"
            else:
                response = []

        # This weird code sends a response back to the client
        if len(parsed) == 3:
            (resource, key, value) = parsed

            if key == "email" and resource == "customers":
                response = get_customers_by_email(value)
            elif key == "location" and resource == "employees":
                response = get_employees_by_location(value)
            elif key == "location" and resource == "animals":
                response = get_animals_by_location(value)
            elif key == "status" and resource == "animals":
                response = get_animals_by_status(value)

        self.wfile.write(f"{response}".encode())
Ejemplo n.º 4
0
    def do_GET(self):
        # Set the response code to 'Ok'
        self._set_headers(200)
        response = {}  #default response

        parsed = self.parse_url(self.path)

        if len(parsed) == 2:
            (resource, id) = parsed

            if resource == "animals":
                if id is not None:
                    response = f"{get_single_animal(id)}"
                else:
                    response = f"{get_all_animals()}"

            elif resource == "customers":
                if id is not None:
                    response = f"{get_single_customer(id)}"
                else:
                    response = f"{get_all_customers()}"

            elif resource == "employees":
                if id is not None:
                    response = f"{get_single_employee(id)}"
                else:
                    response = f"{get_all_employees()}"

            elif resource == "locations":
                if id is not None:
                    response = f"{get_single_location(id)}"
                else:
                    response = f"{get_all_locations()}"

        elif len(parsed) == 3:
            (resource, key, value) = parsed

            if key == "email" and resource == "customers":
                response = get_customers_by_email(value)

            elif key == "location_id" and resource == "animals":
                response = get_animals_by_location(value)

            elif key == "status" and resource == "animals":
                response = get_animals_by_status(value)

            elif key == "location_id" and resource == "employees":
                response = get_employees_by_location(value)

        # This weird code sends a response back to the client
        self.wfile.write(f"{response}".encode())