Transcript for:
Application Program Interfaces: API, REST API, and Example Use in Sports Data

In this video we will discuss Application Program Interfaces; API for short. Specifically, we will discuss: What is an API, API Libraries, REST API, including Request and Response. An API lets two pieces of software talk to each other. For example you have your program, you have some data, you have other software components. You use the API to communicate with the API via inputs and outputs. Just like a function, you don’t have to know how the API works, but just its inputs and outputs. Pandas is actually a set of software components, much of which are not even written in Python. You have some data. You have a set of software components. We use the pandas API to process the data by communicating with the other Software Components. Let’s clean up the diagram. When you create a dictionary, and then create a pandas object with the Dataframe constructor, in API lingo, this is an “instance.” The data in the dictionary is passed along to the pandas API. You then use the dataframe to communicate with the API. When you call the method head, the dataframe communicates with the API, displaying the first few rows of the dataframe. When you call the method mean, the API will calculate the mean and return the values. REST APIs are another popular type of API; they allow you to communicate through the internet allowing you to take advantage of resources like storage, access more data, artificial intelligent algorithms, and much more. The RE stands for Representational, the S stands for State, the T stand for Transfer. In rest API’s your program is called the client. The API communicates with a web service you call through the internet. There is a set of rules regarding Communication, Input or Request, and Output or Response. Here are some common terms. You or your code can be thought of as a client. The web service is referred to as a resource. The client finds the service via an endpoint. We will review this more in the next section. The client sends requests to the resource and the response to the client. HTTP methods are a way of transmitting data over the internet. We tell the Rest API’s what to do by sending a request. The request is usually communicated via an HTTP message. The HTTP message usually contains a JSON file. This contains instructions for what operation we would like the service to perform. This operation is transmitted to the webservice via the internet. The service performs the operation. In the similar manner, the webservice returns a response via an HTTP message, where the information is usually returned via a JSON file. This information is transmitted back to the client. Sports data is always changing. This is an excellent application of an API as it can be constantly updated. We will use the nba API by Swar Patel. The API is always being updated from endpoints at NBA.com. It's simple to use so you can focus on the task of collecting data. In the nba API, to make a request for a specific team, it's quite simple. We don't require a JSON file. All we require is an id. This information is stored locally in the API. We import the module teams. The method "get teams" returns a lists of dictionaries, which have the same keys, but the values depend on the team. The dictionary key id has a unique identifier for each team as a value. To make things easier, we can convert the dictionary to a table. First, we create the function "one dict," to create a dictionary. We use the common keys for each team as the keys, the value is a list; each element of the list corresponds to the values for each team. We then convert the dictionary to a dataframe; each row contains the information for a different team. We’ll use the team's nickname to find the unique id. We can find the row that contains the Warriors as follows. The id is the first column. We can use the following line of code to access the first column of the data frame. We now have an integer that can be used to request the Warriors’ information. The function "League Game Finder" will make an API call. The parameter "team id nullable" is the unique ID for the Warriors. Under the hood, the NBA API is making an HTTP request. This is transmitted to NBA.com. The information requested is provided and is transmitted via an HTTP response. This is assigned to the object “gamefinder.” The gamefinder object has a method get data frame, that returns a dataframe. If we view the dataframe, we can see it contains information about all the games the Warriors played. The PLUS MINUS column contains information on the score. If the value is negative the Warriors lost by that many points; if the value is positive the Warriors won by that amount of points. The column Matchup had the team the Warriors were playing; GSW stands for Golden State and TOR means Toronto Raptors vs signifies it was a home game and the @ symbol means an away game. We can create two dataframes, one for the games where the Warriors faced the Raptors, at home, and the second for away games. We can plot out the PLUS MINUS column for both dataframes. We see the Warriors played better at home.