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