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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The most complicated method that we're going to work with in our api is the ability to update a car.
0:09 So getting cars, pretty straightforward, creating cars somewhat involved, but updating means we have to both get the car back
0:17 and make the changes to push it in there, so we have to be pretty careful here. Now, the first thing that we're going to do
0:25 is we're going to use the request and we're going to go to the matchdict and say give me the id that's in the url,
0:30 because we're doing a put of a json body to api /auto/ car_id so we need to know what car you are talking about;
0:38 grab that there, use the repository to make a quick check does this car actually exist, if it doesn't,
0:43 return 404, like hey there is no car with that id. The next thing we have to do is parse the body,
0:49 and as you saw in that last example when I made the json malformed, that can crash, how do we know a crash,
0:56 because we got the response 400 could not parse your json, your body is json and that's in the except clause there,
1:02 so you want to make sure that that part goes into a try except unless you're willing to take 500 when they send you a bad request,
1:08 which is okay, but it would be better to communicate like the problem is on your end, not ours, so please, don't bother us.
1:15 Then, we have to actually do the work, we're going to go update the car, and we want to return something,
1:20 so in this case, there is nothing really new about the car, other than what they have already told us,
1:26 there is no reason to send additional data back to them and say and here is the updated version, because guess what, they gave us all the updates.
1:33 So, what we decided to do is return a 204 status code which means successfully updated but no content. So, that worked out really well,
1:40 in the case of the post, we decided to use 201 created which is the best response for the post operation.


Talk Python's Mastodon Michael Kennedy's Mastodon