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