#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: What are APIs, why you want them and API Star
0:00 So what are APIs? I mean we hear about APIs all the time, right? All services seem to have one and they're right.
0:10 It's a very common and important thing to have as a company because it provides more ways to access your data and hence, increase your business.
0:22 Look at Facebook, for example. When they introduced the like button which allowed interfacing with their data on other web sites
0:29 it went viral, incredibly beneficial for them. So think about an API as a GETeway to a company's data. Especially these days with mobile services, IoT
0:43 they all need to access a central backend and API is a way to control that access. Let's look a bit closer under the cover.
0:53 So an API really is a client talking to a server and a server talking back to the client basic network model. So let's say mobile app makes a request
1:04 to the backend of any company, right? It sends a get post update or delete request and it typically does that to an endpoint
1:16 serve IP port/ in this case, cars. For example, you do a GET against that endpoint and the API's responsibility is then to return the data
1:27 in a sensible format which is usually XML or JSON but mostly JSON these days. And the protocol is HTTP. To that same endpoint /cars
1:39 the client can also make a POST request which is a bit more interesting because then the API will probably authenticate you if
1:47 it might actually authenticate you anyway but if you're going to POST or PUT you're actually going to modify data. You're going to add a new car
1:54 or modify data of an existing car so the API server, they'll authenticate you and also importantly validate the data
2:04 if it's a valid car that you want to access. Car 1 is available but we had requested car number 5000
2:12 the API should tell you that that car does not exist and typically it does it with an error and a 404 code. The 404 you know of course
2:20 that's also, it's very similar to accessing a webpage When you access a webpage that does not exist you get a 404.
2:28 So after authentication and validation the API then typically talks with the database or with the backend to persist your data in the database.
2:38 And then makes a response of the data and a status code. If you make a GET request, it gives you a list
2:44 serialized in JSON and returns that back to the client. Or if you make a POST request it will typically return the new object that was generated.
2:55 Or if you do a PUT request it will return the updated object. If you delete an object it will typically return an empty object.
3:06 And each operation has a standard status code so if you do a POST that typically goes with the 201 created
3:14 or if you do a GET, if that went okay, you get a 200. If you do a DELETE you get a 204 which is no content.
3:22 And although that sounds now all kind of abstract don't worry apart from the authentication and persisting in database
3:29 we're going to implement all this in the API Star framework and then it will make even more sense. So here you see it a bit more detail
3:37 the client makes a request to the server to an endpoint with or without data, the endpoint authenticates validates if there is a modification request
3:47 persist in the database, and then creates a response with data and status code and sends it back to the client over HTTP.
3:55 And that's basically what an API is and what it does. Now API Star is awesome, it's for Python 3 which you all should be using now.
4:04 And it has some features on top of the standard API stuff. Which is schema generation, it's expressive so it uses type annotations to kind of
4:14 make a contract of what should go into each method and what goes out which is a great new feature of Python actually.
4:21 Adds performance and it supports async which is a big deal these days. Apart from that, it's from Tom Christie the creator of Django REST framework.
4:31 What I found very interesting is the typing feature that you define your car class and it has validation and serialization out of the box
4:39 which saves you a lot of code. But we will see that in the next video. First we need to get some cars data
4:46 so let's explore getting fake car data with Mockaroo.