#100DaysOfWeb in Python Transcripts
Chapter: Days 9-12: API Star
Lecture: Getting fake cars data with Mockeroo

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Before we can build an API
0:02 we need some data.
0:04 Recently I discovered this cool
0:06 mock data generator called Mockaroo.
0:09 It lets you select all kinds of data
0:11 and export it to JSON, SQL, and a lot of formats.
0:15 So lets get some car data.
0:25 Here it gives you some basic data
0:27 which you can just click in the type column
0:30 and filter on cars.
0:32 We want the manufacturer, model, year
0:40 and the VIN number.
0:44 Don't need this one.
0:46 Lets give them sensible names.
0:55 Year, and VIN
0:58 I think thousand rows is okay
1:00 And we want JSON data.
1:03 Could go with CSV as well but JSON is easy to parse.
1:07 And we can preview the data.
1:12 This looks nice.
1:16 And download the data.
1:21 So before writing the API, let's do some preparation.
1:24 I'm going to make a virtual environment
1:26 and install the requirements.
1:27 We're going to get a copy of Postman
1:30 which is a convenient tool to test out our API.
1:33 And we're going to load in the cars data
1:36 we just saved to our hard drive.
1:39 So basically to create a virtual environment
1:41 we can use the builtin env module
1:43 we activate it, and then we install the requirements.
1:48 So let's do that now.
1:50 And I created a folder, apistar
1:55 and I pulled in the requirements.
1:58 And I have the cars JSON which I will handle next
2:02 and then add the app.py skeleton
2:05 which we will start to use to load in the cars data.
2:10 So let me create a virtual environment.
2:14 Let me activate it.
2:20 And as you can see we're running Python 3.7.
2:23 The latest and greatest at this time.
2:26 So as it's a brand new virtual environment
2:30 we don't have any dependencies installed.
2:31 So let's do that next.
2:44 Lastly, let's install a tool called Postman
2:47 which is a great help when you write your API's.
2:50 So, let's go to Postman, and you can just
2:55 download the app, for Mac, Windows, or Linux.
3:00 I happen to have it installed already.
3:04 So when you launch it
3:09 you can make an account or you can
3:10 skip signing in and go straight to the app.
3:13 And once in the tool, you'll have
3:15 your URL bar here, and the type of method here.
3:18 And that's it for now.
3:20 When we have our first API methods defined
3:23 I will start using this tool to test out the end points.
3:27 Alright, let's load in the JSON data.
3:30 We just retrieved from Mockaroo.
3:32 I have a helper function, _load_cars_data
3:35 and I wrote some code to load it in.
3:40 I saved to cars.json.
3:43 I open it with a with statement
3:45 which is always best practice
3:46 to automatically close up the resource.
3:49 And I loaded it with the json.loads.
3:51 I have to import json.
3:53 Interestingly, at first pass
3:55 I was returning a list of cars
3:59 so every car would be a dictionary in a list
4:01 but when I was writing the API
4:04 a lot of times you need to do like lookups on car id.
4:08 So I found it easier to use the dictionary
4:12 with the key id and the value, the car dictionary.
4:17 So I can do easy and fast look ups on the ids.
4:20 So I can just do dictionary.get on the car id
4:24 and it gives me the car dictionary
4:26 or None if it's not there.
4:28 And not only is it then faster to write methods
4:32 like get_car, update_car, and delete_car
4:35 which all retrieve cars by id
4:37 the look ups are very fast because
4:39 it's like an index in a database.
4:41 So that's why I changed the data structure I'm using
4:45 and that's the dictionary.
4:46 So then I need to load them in.
4:48 I just call the helper function
4:49 and then I have VALID_MANUFACTURERS
4:51 which is a set of all the manufacturers.
4:54 Why start a list?
4:56 Well, there's a list inside the set
4:58 but that gives a lot of duplicates of manufacturers
5:00 So then rep it into set to just get all the duplicates out.
5:04 So this yields a list of 66 manufacturers
5:07 and I use that, I want that variable
5:13 to demonstrate API Star's validation.
5:16 And I have a constant CAR_NOT_FOUND
5:18 which I'm using in various functions.
5:20 Why is car lower case, and VALID_MANUFACTURERS
5:22 and CAR_NOT_FOUND uppercase.
5:24 Well these two are constants.
5:27 They are not meant to change, but cars is.
5:30 Cars is the data structure that the API methods can change.
5:34 So we can delete cars, add cars, and update cars.
5:39 So that's why it's not a constant, so it's lowercase.
5:42 Let me quickly show what that looks like.
5:46 Before 3.7 we had to write pdb, pdb.set_trace.
5:53 Not anymore with Python 3.7 we can just use break point.
6:01 Alright, so, you have cars, that's a big one
6:06 but you see it's dictionary, the keys
6:08 are the ids and the values of the dictionary are cars.
6:15 So we can do and get on 1000
6:20 and 1001 if it's none, perfect.
6:25 And we have a VALID_MANUFACTURERS
6:32 and that's the set I spoke about
6:33 and it has 66 items and it makes it easy to do
6:38 look ups.