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