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

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now that we can create cars, we better be able to update them, what if we make a mistake, what if something about the car changes, right,
0:08 we no longer want to sell it, we crashed it, we want to change the price; so let's make another method here, we just copied the post one
0:15 because this is actually closest to what we're doing but we're not going to do a post, we're going to do a put and remember
0:20 make sure you change the name or you'll mysteriously get 404. So, we want to do some similar stuff
0:27 for example we want to get the car data out but in this case, we need to figure out what car is it they're actually trying to update.
0:33 Let's just pull back our home screen here, pull this us up again, there it is, we're going to do a put against that url
0:42 so you're going to need to say I want to update this car with this data so we're going to want to actually go and get the car id out here
0:50 very much like we do in the get details and recall that the way we did that was something like this.
0:56 We do the request and here we looked in the matchdict, that's where the route data goes, I'll do a get for car id, that is basically a plain dictionary
1:07 and we can get stuff out of dictionaries in a safe way like that, and then we could try to get the car,
1:13 so we'll go to the car and say repository.car by id, car id, and if this is none it's just going to return none
1:20 so we can do a quick check here and say you know what if there's no car that you're trying to update in our database
1:25 we're going to tell you— what should we tell you— 404, no car with this id, in fact we already have that code written right here
1:32 so let's just rob that and put it here, ok so if we try to get the car and there is no car
1:39 you can't update the car because it's not found so 404, okay. The next thing we need to do is get the data
1:45 that they're going to update it with, which is just like this, right, we're going to do the same thing here
1:50 again validation probably a thing that we want to do are the fields there, are they of the right type, right
1:55 is it something that could be an integer for the price or do they pass us a list where there should have been a number
2:02 crazy things like that, or does the field even exist, alright but we'll come back to the validation. So we'll assume this works, we'll come in,
2:09 and here we can do an update car which is very similar to the add car
2:13 except for that it just copies over the values, it doesn't actually generate new ids, and we technically could return this, but we're not going to,
2:22 let's suppose that we're not going to do anything like that, instead we want to be very careful with the status codes here,
2:28 let's go back to httpstatuses.com. So if we come down here we have created,
2:34 oh we were not that careful before were we, we probably should do this as well so what we want to do is say that's great, you updated it
2:41 but there's no content, so we're going to return 204 and this is the right one update ok, so instead of returning the updated car here
2:51 we're going to return a response or the status is 204 and I can go ahead and set the body, but I think
2:58 sometimes the browsers don't download it when they see a 204 they are just like great 204 in the header we're good
3:04 but let's say a car updated successfully, there we go. And while we're at it, let's go and fix, I completely overlooked this before,
3:12 let's go and fix this as well, so we'll go up here and we'll say this case 201 and what do we put for the body like here we have the serializer, right,
3:23 for json let's go ahead and put the body here, we're going to say not body but json body is the car data
3:30 okay, we'll test both of these, because we're going to need to run update anyway. Okay, so this is looking good, we've got our status codes in place
3:39 201 created we'll give him back the car down here and we are saying we've accepted your response
3:47 there's no more content, but everything was okay and we'll give him that but like I said, you probably won't actually see that
3:53 if there's some kind of error we'll change that word to update although I think it still applies, let's save, let's go back to postman here,
4:04 so we come over, do a response, and we get all these various ones here we're going to create our opel concept again, and there it is,
4:10 that's the key that was generated, and notice importantly 201 created right there,
4:17 the request has been fulfilled, and resulted in a new resource being created guess what— details are in the body.
4:25 Cool, so now that this has been updated, notice there's no damage let's do a get over here, for this new thing that we just created,
4:34 okay, so no damage, the year and price are this, 29 thousand. Let's say we're going to update the price because it has to go down
4:43 since we like scraped the car or something; so here we've got our post, let's do a put to do an update
4:52 and we're going to update against the exact url here, all right and I don't recall if it looks in here,
5:03 I think it's a good idea to pass it here as well just to be clear and we should probably check that match or make sure that if it's not in here,
5:12 that we basically only copy over the values that are passed something like that but just to keep things simple, let's make sure the id is in the body.
5:18 So what we're going to do is we are going to make some changes let's say small scratch driver's door, otherwise fine, something like this
5:26 and even though it is the concept car, we're going to take it down to 28.8 right, instead of 29 thousand.
5:35 So what we want to do is go review the flow here, is we're going to do an update I am going to call this function,
5:41 I am going to match the route, oh it almost didn't work we need auto api, that would be a very bad thing, and here we go, auto api
5:49 we want to make sure it's going against the one that has the car id not the other one, we want do a put and then we're going to do the response in json
5:58 although nowhere are we returning that so let's say there's really nothing to go there,
6:04 there's no place in which we are using a renderer, if you return a response,
6:08 instead of some kind of object renderers are not involved, you're short circuiting that. So we are going to call get card id, use that to find the car
6:17 if there's actually no car to be updated then off that goes and then it does assume the car id is in there so that's kind of important,
6:26 all right let's rerun this really quick because I had the wrong route there which means we're going to want to run this one again.
6:36 And we just need the new url, once we switch to a real database these ids won't be sort of random every time we restart,
6:46 the ones we create they will stick. Finally, put this here, like I said, we should probably write the code
6:52 to make sure that we just copy that id across, but for now, let's just run it like this, let's go over here and do a get
6:59 just to make sure there's no values, look no damage, price is 29 thousand, we are going to make an update, put to the location, go,
7:08 204, no content, everything works, the server successfully processed the request. Great, now if we go back and say all right, cool,
7:18 so show me the details about this car, now you can see the price is going down 28800 euros
7:24 and there's a small scratch on the driver's door otherwise it's fine isn't that cool? Let's suppose we loaned it to our teenage daughter
7:32 and now something else has happened to it and there's a dent in the bumper right we can send that update again, go back over here get the details again
7:41 and now we've updated it once more price has changed again, there is a small scratch and so on. So you can see how this works, we've gone over here
7:50 and we're doing a put to the individual resource on the server and the body contains all the details,
7:56 like we could set it up so that we only pass the things that are changed and we look in there and like do an update that would be nice to do,
8:03 like if we don't pass the brand that means the brand just stays the way it is, that would be pretty easy to write, but that's not how I wrote it;
8:10 so you want to be real clear what it means, and of course we use the 204 response code, let's just check if I do like an extra five on the end
8:17 now it's 404 and you see at the bottom the car with the id such and such was not found but if we look for one that is, here we go, perfect,
8:25 and as well as if we make some malformed stuff here, could not parse your json 400 bad request,
8:32 very nice, okay let's put our json back, it looks like everything is good.


Talk Python's Mastodon Michael Kennedy's Mastodon