#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: API Star structure and its type system
0:01 All right, so we loaded in the JSON data
0:04 and we got some variables and constants defined.
0:08 Now let's look at a car's definition.
0:11 If we inherent from the types.Type framework
0:14 we get access to some powerful features of API Star
0:18 namely validation and serialization.
0:24 This is what I had in mind for a car.
0:26 An id, a manufacturer, a model, a year, and a vin.
0:30 Now we use validators for each of those
0:33 which I imported at the top.
0:37 An integer, I could not find an auto-incrementor
0:42 so I'm going to handle it in the post.
0:45 That is, I allow it to be null here
0:48 but when I make a new car
0:50 I calculate what the new id is.
0:52 I take the length of cars
0:54 which now a 1,000, and I'll add one.
0:57 But we will see that when we get
0:58 to the post or create car request.
1:01 The manufacturer, look here you see
1:04 the valid manufacturers I set up above.
1:07 And I'm passing that to an enum.
1:10 An enum does require a list, so I typecast it to a list
1:15 and what this does is when you add a car or edit a car
1:20 it looks at this manufacturer, valid manufacturer list
1:25 and if the value provided is not in there
1:28 it throws an error, and the others are pretty simple.
1:31 A model can be a 50 characters wide
1:35 if you give it more than that, it throws an error.
1:37 A year in between 1900 and 2050.
1:42 If the year is outside that range
1:44 it throws a validation error.
1:47 And the vin is optional, and I can make it optional
1:49 by giving it a default of empty.
1:52 And with these five lines of code
1:54 we've actually accomplished quite a bit.
1:56 We can read about this in the documentation
1:59 under the type system.
2:00 And you see here are similar example
2:03 but here they use a product
2:05 and it literally says you can use the type system
2:09 for both validation and serialization
2:12 of the outgoing response.
2:13 And that's exactly what we're going to do.
2:15 So we don't have to write like manufacturer
2:17 provided is in this list.
2:20 The framework is doing that out
2:21 of the box, which is awesome.
2:24 So that's one, then I'm going to the end of the file
2:27 and the other set up code you need to have
2:29 you need to instantiate an app, giving it the routes
2:32 which we're going to write next.
2:35 And if app is ran standalone, we launch the app server.
2:39 So we'll run on local host, port 5000
2:43 and while I'm developing the API
2:45 I'm putting debug equals true.
2:47 So that will catch any error.
2:49 So this all done, the only thing
2:51 that we need to do is define the routes.
2:54 So I have some code here...
2:57 And every route specify the end point
3:01 the method, and the handler.
3:04 So here the GET, POST, PUT, and DELETE we mentioned
3:08 in CRUD terms, this will do the read
3:11 this will be the create
3:13 Update and delete.
3:15 And you see that I defined endpoints
3:17 where these methods are valid
3:19 so to GET, to do a GET or POST
3:22 I do it against the root, and GET, PUT, DELETE
3:26 Here's another GET, so this is the get of all the cars
3:30 and this is the GET of an individual car.
3:32 So the individual GET, and the PUT, and the DELETE
3:35 those are done against an id.
3:37 According to the method we use
3:38 in a request, it calls the handler
3:42 which basically is the function we define above.
3:46 So list_cars, create_car, get_car
3:52 update_car, and delete_car.
3:57 In the next two videos, I will flesh out those functions
4:01 or API methods, so that we get a fully functional API.