RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: Concept: Deleting cars, RESTfully
0:01 So how do we go about deleting that car?
0:03 What we decided the best way to do it was to a delete request
0:06 to the actual url that represented the details of that car, api /auto/ car_id,
0:11 so we're just going to issue a delete request straight to that
0:14 and submit nothing else, there is no body,
0:16 so we created this delete auto function,
0:18 we mapped the route name to the one that contains the car id,
0:22 the singular auto api, and we have the request method set to delete.
0:26 Notice there is no renderer, there is no place where we are like returning anything,
0:29 either we return an error message or we return just hey everything worked.
0:33 So we don't need the renderer, right.
0:36 So just like before, we are going to get the car_id out of the matchdict,
0:39 and then we're going to make sure the car exists,
0:42 so we'll go to this little step here where we get the car back from the database,
0:45 we could do something more efficient but remember,
0:47 it's in memory, this is as good as anything,
0:49 and we're going to go and just check that it exists, if for some reason it doesn't,
0:52 we're going to give them a message with a 404
0:54 and then we get to the really what we are trying to solve here,
0:57 so we're going to call delete car by id,
0:59 this is going to go and delete it out of the database,
1:02 and then we'll return 204 that worked, everything was great,
1:04 but there is no content to return, so go for it and be happy.
1:07 And if for some reason something goes wrong,
1:10 again, like I said, you've got to decide maybe this should be 500, 400
1:13 we'd have to be a little more careful about the errors we catch,
1:16 but because this is just an in memory database, we can't be very specific.
1:19 So, there you have it, it's actually quite easy to delete things with our service.