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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Before we add the modification features to our api let me just talk really quickly about a little bit of reorganization that I've done for you.
0:08 So, the previous service that we created together I put that into its own folder, and I have given it various safe points
0:16 and now I've created a new service called restful auto service, so you'll be able to distinguish the two first service, restful auto service.
0:23 Also, I made a snapshot of exactly what we're starting with and called it like starter or something like that in that folder
0:29 so if you want to follow along, you can grab that and work from there, it should be exactly what I've started from myself.
0:35 Okay, so let' s just run it and remind ourselves where we are; okay, so we have our auto service, basic restful auto service
0:42 and we we're working with two url structures here so one we can get all of the autos, all the cars here, like so
0:50 and we are going to do a get against that or, if we have an individual car, we could get its details by saying api /auto/ whatever its id there is.
1:00 Now, if we're going to create a new car do we need a new url, do we want to use the same url? The way this typically works is, if you know the location
1:10 of the thing that you're trying to modify, we'll use this; so like if we're going to do a put and update it, we'd probably use that url,
1:16 if we're going to do a delete, we'll definitely use that url but with post, usually the server is the thing that ultimately decides where it is,
1:23 this is often the primary key or something like that that gets generated on the server side, so we don't know where it would live,
1:29 so we can't do like a post to its not yet existing location. So what we're going to do, is we are going to do a post against that url
1:36 and we're going to submit the details of the car, like its price and whatnot, and then, the server will return back the finally created one
1:45 with its location and things like that. Ok, so let's go over here, as you can see if we're starting from this one
1:51 probably the all cars is a good thing to model off of so let's go over here to the api, and here's the all cars
1:58 and let's put this modification stuff down below here for a minute, okay so this is going to be all autos, but we're not going to use get
2:07 that was the one that displayed them, we're going to use post and we're going to keep the same renderer for here
2:13 and instead of doing this, we're going to do a couple of different things. The first thing that we need to do is get the submitted body,
2:21 like what car was delivered to us, so let's go over here and we'll just say for now return nothing, so how do we do that?
2:29 Well, here notice there was this request and I gave the underscore to say I don't care about it,
2:34 but you know what, I do care about it now, so let's put it back and let's even use the type hints, type annotation stuff
2:39 so that we could get a little help if you're not familiar with this. Alright, so we'll come over here and we'll say request.
2:45 now, there's lots of places data gets submitted to us there is the matchdict, there's the get, the post, dictionaries,
2:51 but what's coming in here is a body, and it doesn't come in as a form body
2:55 it comes in as json, and the way we get that is there's a json body property;
3:00 basically what it does is it looks to json text or it looks at the text in the body
3:04 and then it uses the json library to pars it into a Python dictionary. So, this is going to give us our data,
3:11 now, that's all well and good, but it just blissfully passes this along to the json serialization, deserialization bit
3:19 and if there's something like there is no body or there is something wrong with it,
3:24 this is going to crash, so we're going to need to be a little bit careful there. So let's go ahead and put this into a try except block here,
3:32 and in case there is an error, we need to, we could just let it crash right, or return something,
3:38 but we want to be very restful here, so we're going to return a response and the status is going to be 400, number 400 stands for bad request
3:47 something broke and it's your fault, not the server's fault that it broke. And for the body, we could put some kind of message like
3:54 could not parse your response as json, something like that, and we're going to of course return this, all right.
4:02 Now, PyCharm has given us a little bit of a warning let me just tell it to leave us alone for now.
4:08 Okay great, so we've either told them that what they have given us didn't work or we've now parsed it into a proper object.
4:16 The next thing to do, we should probably add some validation here but for the time being, let's just put the validation on hold
4:23 and let's just try to convert this and save this as a car. So we have our repository and it has an add,
4:31 it does not yet have an add car, we're going to write an add car and we are going to have the data, and just add that, okay.
4:37 Now, we're going to do something a little bit funny here, so let's rename this to make it a little more obvious— car data,
4:45 now, this thing where we're adding this value here what we're going to do is we're actually going to take
4:52 whatever is created by the repository the data access layer, and use that and then we want to return this back as well.
5:00 Now, there's another possibility here that something goes wrong maybe we can't talk to the database or something like that,
5:06 so we're going to do something like this. Now, whether this should be 400, because some validation at the data layer failed
5:14 or it should be 500, because we can't talk to the server, we probably should be a little more careful,
5:19 but for now let's just say could not save car, all right, great, so one of the reasons we might not be to save the car
5:26 is there's no add car method, let's add that. All right, so for now, remember we are just using this fake data things
5:32 we're going to stash it in here, so we'll say cls.car_data of well, we're going to need some kind of key
5:43 up here notice, we're doing this little trick as we read it from the file to generate one of these fake ids and so it's a key = row
5:50 the row is what we're after, car data is what we're calling it right now. And then, let's return that, okay
5:58 so whether this is the same object we're passing back or we're actually generating a new object which might be the case in the database
6:05 we're going to go and save this, the most important thing is this now is going to have an id we're going to need that in a little bit.
6:13 Okay, so let's go and test it. If I click here, you'll find it's not the easiest thing so what do I click, even if I put a thing
6:22 I guess I could put a little form that submits something to test it but that seems a little hooky,
6:27 so when we get into things that are not get it's a little bit easier to use something outside the browser,
6:32 I know you can get plug in for the browser, but they're not as nice so let's go over here and look at postman.
6:39 So first, we have our get request and let's just run that and make sure it's working,
6:43 oh it is not— this is very interesting, let's go back and see why this is not working.
6:48 All right, so it's not the problem here, the problem is down here; remember the way Python works is when you execute that bit of code
6:56 when you literally parse this file, it executes this which defines an object like name this, all autos and down here, we are executing this bit of code
7:06 and it's defining the thing called all autos as well and replacing the value of the previous one, but this time it only listening here,
7:13 so we need to change the name of this to something like this, create auto or something like that, so it no longer is listening to get
7:22 it is only listening to post, all right. So now we got this back, and let's just take the body of one of these
7:28 because that's going to tell us what we need and we can come over here and duplicate this,
7:33 alright, so we're not going to do a get, we're going to do a post against that url and we're going to need to supply a body, alright.
7:39 We don't want this form data we want to just do raw, do this, make sure it has no id, it doesn't really matter but it's not going to,
7:46 and this is going to be the opel concept or whatever and 29 thousand euros and I guess this is fine, we'll start out with no damage.
7:59 So what we are going to do, so we are going to post this here, it's going to look at the request method as well as the url,
8:07 together it's going to say okay this function create auto is the one we should run well, convert this to a Python dictionary by calling json body,
8:15 come back to validation, we're going to assume that we're entering it, right, that's a big assumption and then we're going to go to the repository
8:23 and we're going to store this into our little temporary in memory database give it a primary key and return it back
8:28 and because of the renderer is json it's going to convert it back to json. All right, try this one more time. So here we got a post, here is our body
8:37 we'll see the response down here, go. And look at that, we got 200 ok that's pretty awesome
8:44 we can look at the brand, the opel concept, it's pretty much the same except if you scroll down, notice it now also has an id
8:52 so we could go back and just verify this now is a thing that exists in our database, so let's duplicate this again;
8:59 I want to do /autos/get/ this id here, and look there's our opel concept
9:04 and the price is 29 thousand, and the year, and all the stuff that we just set is there.
9:09 All right, so we've now added this ability to create new things, new automobiles for sale at our dealership.
9:17 So, that's great, now obviously we're skipping some validation and we're going to come back and do that but let's work on the other ones as well,
9:24 throughout this section we'll work on the updating this thing we just created and if we want to take it off we could even delete it.


Talk Python's Mastodon Michael Kennedy's Mastodon