RESTful and HTTP APIs in Pyramid Transcripts
Chapter: A nearly RESTful service
Lecture: Concept: RESTfully creating cars
0:01 Let's review the main ideas behind creating a new automobile via post.
0:07 So, we decided that the server was going to be the one that assigns the primary key as is almost always the correct choice,
0:16 the server is going to decide how and where to create this car, and so that means we're going to do an http post against / api /autos
0:26 not including the id, and then what's going to happen is we're going to submit the json body to that url,
0:32 the server is going to take it in, generate it and save it to the database pull out any default values that might have been generated in the database
0:39 as well as the primary key and then return this new updated version back to us, so let's see how that works.
0:45 As always, we start out by adding the view config and we set the rout name to the one that matches the url listed above
0:51 and then it's very important that we set the request method to post we don't want to handle a get request or put or delete in the same function
0:59 this one only handles what it means to do a post which is to create an auto and you see, we've even named the function that.
1:05 Sometimes you'll see people looking at the request inside the function and saying well if it's get, do this, if it's post, do that
1:12 and that's almost never the right choice because often these are very dramatically different behaviors
1:17 of like returning what a car is versus creating and validating the car, things like that, so we've created our own method and set up the disambiguation
1:25 by adding the request method and setting that to post. Notice there is no renderer set here, because it's not needed
1:32 in every case we're doing any sort of return value we're using the response object which skips the renderer anyway.
1:37 So, the first thing we have to do is actually get the car data that they are posting to us, and they're doing that in json form
1:43 that's what we've asked of our api clients so we need to somehow get that out and notice we can go to the request and say request.json body
1:51 and this will basically decode the encoded bytes of the body and then send those off to the json module to parse them into a Python dictionary;
2:01 and what comes back, we're calling car is just a Python dictionary of the values they submitted.
2:06 Now this might fail, it could feel for a couple of reasons, it could be something— maybe the body is empty,
2:11 maybe there's a malformed text that's not parsable as json, whatever reason, there could be an exception thrown
2:17 so we want to catch that here, not let it crash our server, we want to say instead hey there's a problem status code 400 bad request
2:24 the problem is on your side please check what you posted because we can't parse that, we don't understand that. Once we've got the car created,
2:32 then we're going to go to the repository and say add car and if everything works, what we're going to do is
2:37 we want to return not just the car with code 200, that would be okay it would be better to say
2:41 use the most precise status code we can which is 201 created in order to return an object and change the status code
2:49 we can't just return car, that would be returning the car and then doing 200;
2:53 to get the 201, we have to return the response, set the status code to 201 and then set the json body of that response to car.
3:00 So that will return the new and updated car to the client if everything works right, if there's some kind of exception
3:06 maybe this is a server side thing, if we had a real database we could maybe look on constraints
3:13 versus can't connect to the server and disambiguate on error type, but for now, we're just going to say 500 sorry something went wrong.
3:19 And that's all we have to do to save and create a new car on server.