Esempio n. 1
0
    def do_POST(self):
        self._set_headers(201)
        #reads up to the end of the characters
        content_len = int(self.headers.get('content-length', 0))
        #turns that string into a python dictionary
        post_body = self.rfile.read(content_len)

        # 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_creation = 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_creation = create_animal(post_body)
        # elif resource == "customers":
        #     new_creation = create_customer(post_body)
        # elif resource == "employees":
        #     new_creation = create_employee(post_body)
        # elif resource == "locations":
        #     new_creation = create_location(post_body)

        # Encode the new animal and send in response
        self.wfile.write(json.dumps(new_creation).encode())
Esempio n. 2
0
    def do_POST(self):
        self._set_headers(201)
        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)

        # 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_resource = 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_resource = create_animal(post_body)

        elif resource == "locations":
            new_resource = create_location(post_body)

        elif resource == "employees":
            new_resource = create_employee(post_body)

        # Encode the new animal and send in response
        self.wfile.write(f"{new_resource}".encode())
Esempio n. 3
0
    def do_POST(self):
        self._set_headers(201)
        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)

        # Convert JSON string to a Python dictionary
        post_body = json.loads(post_body)

        # Parse the URL
        (resource, id) = self.parse_url(self.path)

        new_animal = None
        new_location = None
        new_employee = None
        new_customer = None

        if resource == "animals":
            new_animal = create_animal(post_body)
            self.wfile.write(f"{new_animal}".encode())
        if resource == "locations":
            new_location = create_location(post_body)
            self.wfile.write(f"{new_location}".encode())
        if resource == "employees":
            new_employee = create_employee(post_body)
            self.wfile.write(f"{new_employee}".encode())
        if resource == "customers":
            new_customer = create_customer(post_body)
            self.wfile.write(f"{new_customer}".encode())
Esempio n. 4
0
    def do_POST(self):
        self._set_headers(201)

        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)
        post_body = json.loads(post_body)

        (resource, id) = self.parse_url(self.path)

        new_object = None

        if resource == 'animals':
            new_object = create_animal(post_body)

        self.wfile.write(json.dumps(new_object).encode())
    def do_POST(self):
        self._set_headers(201)
        # You should be able to explain the purpose of a 201 status code.
        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)

        # 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
        new_customer = None
        new_employee = None
        new_location = 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())


        if resource == "customers":
            new_customer = create_customer(post_body)
            # Encode the new customer and send in response
            self.wfile.write(f"{new_customer}".encode())


        if resource == "employees":
            new_employee = create_employee(post_body)
            # Encode the new employee and send in response
            self.wfile.write(f"{new_employee}".encode())


        if resource == "locations":
            new_location = create_location(post_body)
            # Encode the new location and send in response
            self.wfile.write(f"{new_location}".encode())
Esempio n. 6
0
    def do_POST(self):
        self._set_headers(201)
        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)

        # Convert JSON string to a Python dictionary
        post_body = json.loads(post_body)

        # Parse the URL. url returns a tuple with 2 things in it. We are unpacking it on the right side of equal sign.
        (resource, id) = self.parse_url(self.path)

        # Initialize new animal
        new_object = None

        # We are checking the resource on line 121. Add a new animal to the list. Don't worry about
        # the orange squiggle, you'll define the create_animal
        # function next.
        #"post_body" is the body of the post/object in the Animals array of objects
        if resource == "animals":
            new_object = create_animal(post_body)

        # Encode the new animal and send in response

        #locations post request
        # new_object = None
        if resource == "locations":
            new_object = create_location(post_body)

        #employees post request
        # new_object = None
        if resource == "employees":
            new_object = create_employee(post_body)

        #customers post request
        # new_object = None
        if resource == "customers":
            new_object = create_customer(post_body)

        self.wfile.write(f"{new_object}".encode())
Esempio n. 7
0
    def do_POST(self):
        self._set_headers(201)
        content_len = int(self.headers.get('content-length', 0))
        post_body = self.rfile.read(content_len)

        # 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 resource
        new_resource = None

        if resource == "animals":
            new_resource = create_animal(post_body)

        if resource == "locations":
            new_resource = create_location(post_body)

        # Encode the new resource and send in response
        self.wfile.write(f"{new_resource}".encode())
Esempio n. 8
0
    def do_POST(self):
        # 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)

        post_body = json.loads(post_body)

        resource, id = self.parse_url(self.path)

        new_item = None

        if resource == "animals":
            new_item = create_animal(post_body)
        elif resource == "locations":
            new_item = create_location(post_body)
        elif resource == "employees":
            new_item = create_employee(post_body)
        elif resource == "customers":
            new_item = create_customer(post_body)

        self.wfile.write(f"{new_item}".encode())