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