RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: Deleting cars, RESTfully

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The final method that we're going to write to make our service read/write is the ability to delete a car.
0:08 Now this is going to be based on the auto api url not the autos, the singular one that takes the car id
0:15 so let's duplicate this function, this is the put one when we just wrote and right away say change the name, so you don't forget that
0:23 and we'll put a delete here, so we're only going to process this function
0:27 or map this function to a delete request, to that url, not a get or post or whatever, ok so this is good, and we're still going to need to get the car
0:36 and we want to verify the cars there, but we don't need to send a body do we? We don't care what else you're saying, if you say delete the car
0:45 and you give me an id, that car is gone, the only option is either the car is there or it's not,
0:49 or maybe you don't have permission, but we don't have authentication they're not passing like api token or something
0:56 so there there's no check really but either way, getting the body here doesn't make a lot of sense
1:01 and so then the last thing we want to do is just come over here and say delete car, and I think when we delete the car
1:07 status everything was okay but there's no content is probably the way to go, so this is going to be car id, and let's just say car deleted,
1:16 I don't really think that go es through, but I will go and put it there anyway. Alright, so let's go ahead and add this final function here
1:22 and this is going to be the car id and we'll just want to work with this— this actually, so because this is just an inmemory dictionary for now
1:32 we're going to convert this to database operations later, but for now, we'll just use the del operation and the key is going to be car id.
1:40 So we just want to delete that, and that should do it, super simple, okay, so we come in, we get the car id, we do a little validation just to say
1:47 yeah the thing you're going to delete actually exists and then we could call delete, we could change this
1:53 so it raises an exception if it's not there and then catch it here like there's some variations we could apply, but I think this will do it.
1:58 So, I'll run this, everything is good, let's go back to postman, and go through these steps a little bit here,
2:03 so we'll get all the cars they're all loaded up, let's go and create this one, now it has a new id because that's always regenerated
2:15 the way we generated them here let's go see the details about it, edits were gone which we created
2:20 so it's back like this, now let's change this, let's actually change this one
2:25 let's make a duplicate here and say we're going to do a delete to this url, there's no body, there's no headers, there's just a delete request to this,
2:33 go— alright, what do we get back, status 204, ok that sounds really good in seven milliseconds nice and quick;
2:41 let's see if we can see the details about this one now, refresh, the car with that id was no longer found, why—
2:48 because we just deleted it from the database. Perfect, so it looks like we got our delete operation going very well,
2:54 like you probably thought it would be complicated, right, not too bad, not too bad at all.


Talk Python's Mastodon Michael Kennedy's Mastodon