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


Talk Python's Mastodon Michael Kennedy's Mastodon