#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: API methods: GET and POST, testing with Postman

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So we define our car class and this make's that out of the box. We have validation and serialization of the data. So the only thing we need to do now
0:10 is to write our API methods. list_cars, create_car, get_car, update_car, delete_car. In in this lesson, were going to focus on GET and POST
0:18 and the next lesson we do update and delete. Back to my terminal. Heres the skeleton code; those methods. And let's first write the list_cars
0:30 to get all the cars. Now for that one, I don't any input variables because this is going to get them all. But I do specify with the new typing
0:39 and I'm going to say that this is a list I have import that from the typing module. A list of cars. And car is of course this class that we defined.
0:51 Now, it's convenient that we have the cars stored in dictionary. We just, look ups are very fast and it's easy to retrieve cars by id.
1:01 But opposed to a list the dictionary's not sorted. So when I return the cars, I do want to have them sorted. Because that just makes sense right?
1:09 So, to start my list comprehension as I always do; from inside out. So, car for car E, and return this. But this not going to work, because cars is
1:22 a dictionary dictionaries. So I need to retrieve the actual cars return the values. But there's a problem;
1:29 If I sort values, it doesn't know what to sort them. So I'm going to request the items which are tuples of key and values.
1:37 And if we do sort it on items it will do one of them. I can actually, quickly show this. Let's make a small data set just to show how this works.
1:51 Take a slice of five cars, breakpoint and here are cars. So...keys values
2:10 items. Okay. Now doing a sorted on values is not even working. Doing a sorted on items works. Which gives them sorted, as I want.
2:29 So let do that. Now items are, as we saw tuples of id That's the first value and dictionary's the second. We want to the second, so I'm going to get
2:49 that second element and lastly, I have to wrap that in a car. And this is the serialization I mentioned. So we turn a dictionary into a car object.
3:04 Each car dictionary, I wrap it into car which can wraps into a list comprehension. As I mentioned before, I started as a list of dictionaries
3:12 and this method was actually simpler in that implementation, but I promise you that the other methods are simpler
3:20 because I can do the dictionary lookup. Alright, lets test it out in Postman. So, this is get, this is the root
3:28 and let's go back to route to felt the list car We just have written, we do a GET request against the root URL, which is just slash.
3:43 I have to run my server again because it got stuck in pdb. And I made a typo because that's items.
4:01 Alright. As you make your changes the app server just refreshes, except last time where there was a breakpoint in it. And here you go.
4:11 I got a list of dictionaries, and they're sorted in numerical order. And I only see five because, when I was debugging I was getting a slice
4:23 so lets see the full output. Bang. Awesome. Alright, now we want to have one car so we do a get car. And here's where our dictionary is.
4:40 Beautiful. Now I can just do cars, get car id. And this will get the car if it's in the dictionary and otherwise it returns, none.
4:50 So then I can just say, if not car return adjacent response of error and 400. And the error will be a dictionary. That's best practice.
5:08 And I define this constant, remember? CAR_NOT_FOUND. I made that the constant because I'm going to use this string various times. So I abstracted that
5:17 into a variable. I put that into the error dictionary and returned it as adjacent response with status code 400.
5:24 In status codes, you can just look those up on Wikipedia and the most important thing to remember is that 200 range is success
5:33 so we so get okay, create it. Make sure, we'll see in a bit. And we do the post request. 204 is when we delete, and we'll see that later.
5:40 300 is redirection and 400 are the errors. In this case, we actually don't want a 400 We wanted 404. Because if the car is not there
5:50 it's actually not found. It's just like a webpage, right? So that's actually the error we need to use to do it properly.
5:58 Now, beyond this point, we are okay. Because if there was not a car we would bail out. I mean return, just return from the method
6:04 nothing else gets executed. So beyond this we are okay. But we then need to return is a car a serialized object? We hit the 200 code. 200 is okay.
6:15 The car is what we looked up. So that's the dictionary. And we wrapped that in the car class which then gives a valid car object. let's try this.
6:30 One, I think we need a trailing slash and it already works, okay. Three let's just, let's make sure that's correct. So, seven should be a Toyota
6:46 and it is. Okay I think the trailing slash works as well. And the last one is 1000 and 1001 does not exist, and indeed it returns a 404.
7:04 Wow, a valid one returns a status code 200. Lastly, I'm going to do a post which again maps to create_car.
7:20 First, I calculate the new id it needs to get. Get the length of the cars, plus one. Then the car dictionary, the input data
7:31 comes as a dictionary of manufacturer model, year and vin; not the id per se. I'm not requiring the id to get sent we calculate that on the fly.
7:41 Going I'm going to set the id on the object to car id. Then I'm going to store the new car. And this is just a memory I'm keeping this example simple
7:56 not involving a database or anything because I just want focus on the API. But in real life, this would be inserting a record in the database.
8:04 And lastly, I'm going to return a response of the new car. So again, we wrap it into a valid car object and the typical code for created
8:20 as we saw, was 201 created. Let's try that out. Method not allowed. That's because I'm making the request against id.
8:33 In our routes, we're only allowed to make it against the root. Here, the problem is that it don't pass any parameters.
8:41 So post makers are very easy, I can go to body and I can set a couple of key values. I'm going to set manufacturer to Honda model year to 1995
9:02 and send. And wallah, see that? It made the object and it gave the new id of 1001. Now I'm not convinced yet
9:14 because it's just returning the new object. So this is not saying it persisted into the data store. How we really contest that is to now
9:22 make a get request to that new object. And it did. So this means the object is stored in the dictionary, or what in real life would be the database.
9:33 So that worked. Let's do another one. Again, I cannot post to 1001 I have to do it against the root. Now let's see some validation in action.
9:46 First of all, I'm not passing in the year, what happens? Year field is required. Notice, how I didn't write this message.
9:54 This is all from the framework based on the car definition we made. So that's awesome, they stack up. If I do a year out of the boundaries.
10:15 Same if I go to low if I go beyond 50 characters or if I do a manufacturer that's not in the list because its case sensitive, right?
10:31 Now look at that; all that validation that I got out of the box by just defining this class. Awesome. So let's do a valid one, a Jeep.
10:53 Boom, here you see that we had 1001 now its 1002, based on the calculation and it got there. We can, of course add a vin number.
11:04 I don't want to do much validation against that. Its optional, so it didn't complain when I didn't provide it, and again
11:13 actually, I made another post request so the counter went to three. Actually I've made two cars now because I'm not checking against
11:25 if the car is duplicated so we get two without vin and three with vin. Alright, that is it for get and post.
11:35 In the next video we're going to update and delete cars.

Talk Python's Mastodon Michael Kennedy's Mastodon