#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: API methods: PUT and DELETE

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, next our update on the Leaf car those should be easy now. I'll check if the car is present so I get the car id in a new car object
0:12 or the fields we're going to modify. Car holds the manufacturer, year, model, and VIN optionally.
0:20 So, if the car is not there, I return an error with a 404. And if the car is there like we did with create, I set the id
0:30 and stored an object to the car's dictionary or in real life to the database. And then I serialize the new object
0:39 and return to the status code two hundred. That's all there is to it. So, let's test this out. Let me get car. And I need to restart my server.
0:55 Get car fifty five, which is a Jeep. And now we make PUT request and in the body we put something else on ABC, year
1:11 So, we send a put request to that same car id and boom, here's the new object, the updated object. Although, this is just returning the object
1:21 as I did with POST to really see. If it's stored in the dictionary we need to make a new GET request. And we see the same car
1:29 so here it's not a Jeep anymore so the change persisted, alright? And then for the Leaf it's very similar. So, I'm going to do the same check first.
1:41 To see if the car id that was pasting is a valid car so look it up. I look up the cars. If not, I return a 404
1:50 and the lead then simply deletes it from the dictionary. So, I can do a Dell car id. And then return a empty dictionary
2:07 with the status code 204. If you remember from the status codes that's the common return code, no content. So, that's it, that's that out.
2:27 Car fifty-five still present. Delete. Boom, 204 and an empty dictionary. Let's see if it's still there. Now it's gone. Awesome.
2:48 Gone, 404, alright, perfect. So, now we have a full CRUD application. We have list cars, create a car, POST
2:59 get an individual car, get update, and delete. And this is a fully-fledged API star app.

Talk Python's Mastodon Michael Kennedy's Mastodon