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