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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 So how do we go about deleting that car? What we decided the best way to do it was to a delete request
0:07 to the actual url that represented the details of that car, api /auto/ car_id, so we're just going to issue a delete request straight to that
0:15 and submit nothing else, there is no body, so we created this delete auto function, we mapped the route name to the one that contains the car id,
0:23 the singular auto api, and we have the request method set to delete.
0:27 Notice there is no renderer, there is no place where we are like returning anything,
0:30 either we return an error message or we return just hey everything worked. So we don't need the renderer, right.
0:37 So just like before, we are going to get the car_id out of the matchdict, and then we're going to make sure the car exists,
0:43 so we'll go to this little step here where we get the car back from the database, we could do something more efficient but remember,
0:48 it's in memory, this is as good as anything, and we're going to go and just check that it exists, if for some reason it doesn't,
0:53 we're going to give them a message with a 404 and then we get to the really what we are trying to solve here, so we're going to call delete car by id,
1:00 this is going to go and delete it out of the database, and then we'll return 204 that worked, everything was great,
1:05 but there is no content to return, so go for it and be happy. And if for some reason something goes wrong,
1:11 again, like I said, you've got to decide maybe this should be 500, 400 we'd have to be a little more careful about the errors we catch,
1:17 but because this is just an in memory database, we can't be very specific.
1:20 So, there you have it, it's actually quite easy to delete things with our service.

Talk Python's Mastodon Michael Kennedy's Mastodon