Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
The source of data for this app is the iTunes Search API. Let's start by exploring what we can do with the API and how we can request data
-
0:00
Open up your favorite browser and google itunes search api.
-
0:05
This is the API this first linked here
-
0:08
that we're going to be using to build our app.
-
0:10
The iTunes Search API is great,
-
0:13
in that we can do a lot with it, without having to authenticate ourselves.
-
0:17
Most robust music APIs like Spotify for example,
-
0:20
require you to authenticate each user and that's a bit advanced for us right now,
-
0:25
so we're going to be using public data provided by iTunes.
-
0:29
The downside though, is that this documentation for
-
0:32
the iTunes Search API is somewhat lacking.
-
0:35
Let's start at the very top-ish, at this section marked Searching.
-
0:41
First things first,
-
0:42
the documentation specifies that the data is returned as a JSON Object.
-
0:48
Well, good thing we know how to work with that already.
-
0:51
But how do we get this JSON data itself?
-
0:54
If you read on, it says right here that we need
-
0:57
to use a fully-qualified URL that fits the following format.
-
1:01
Now, it's not listed here specifically, but in this URL,
-
1:06
itunes.apple.com, this bit right here, is the base URL that we need to use.
-
1:12
Search is a path on the base URL that provides access to the Search API.
-
1:18
We call this an endpoint.
-
1:20
So iTunes.apple.com/search is the search endpoint.
-
1:26
In addition to search, we're going to be accessing another endpoint, lookup.
-
1:30
But we'll get to that in a second.
-
1:32
The last time, we created a URL to get JSON data.
-
1:36
All that we had a single URL with a base and a path to get that data.
-
1:41
Very few end points work that way, though, and in most APIs, the way that you
-
1:46
access certain information is by providing a set of parameters as part of the URL.
-
1:52
The server then reads these parameters and
-
1:55
creates the data set that we're looking for to return as JSON.
-
1:59
In the URL format specified, you'll see that after this search keyword,
-
2:04
there's a question mark, after which it says parameter key value.
-
2:09
Now this doesn't mean we write out that word.
-
2:12
What it means that is to construct a fully-qualified URL,
-
2:16
we need to include relevant parameters.
-
2:19
Parameters in a URL are specified as key value pairs, and
-
2:23
allow us to specify explicitly the type of data that we want.
-
2:28
The way you write out these parameters in the URL is very important, and
-
2:32
there's a bit of work that goes into it, but let's assume we know that for now.
-
2:36
Scroll down right after the search section and you should see a table.
-
2:41
This table defines the different parameter keys and
-
2:45
values that we can specify to search for different types of content.
-
2:50
So the first parameter key listed here is called term.
-
2:53
When we searched through the API, we need to provide some search terms,
-
2:57
much like you do when using a search engine.
-
3:00
Let's say we wanted to look up music for the patron saint of swift, Taylor Swift.
-
3:05
The search term here Would be Taylor Swift or TaylorSwift,
-
3:09
depending on what you type.
-
3:11
So to tell the server that's what we want, we include a parameter in our URL with
-
3:15
the key specified as term since this is defined in the documentation.
-
3:20
And the search term,
-
3:21
the actual search term that we're looking for as a value to this key value pair.
-
3:26
But it's not that straightforward.
-
3:28
If I were a user with malicious intentions, for example,
-
3:32
I could include some Swift code or whatever language the server is in,
-
3:37
let's say JavaScript, as the value for the key term in this key value pair.
-
3:42
My hope here is that the server would then run my code and I could wreak havoc.
-
3:47
This is called code injection.
-
3:50
To prevent things like this from happening we need to encode any value
-
3:54
passed in as the value to this parameter key value pair.
-
3:58
So if you look at the description here under values for the term, key,
-
4:02
you'll see that it specifies that the value should be URL encoded.
-
4:07
Now again we're gonna ignore this for just a little bit and
-
4:10
we'll cover this one we're done with our initial set up.
-
4:14
Other parameters include the country code for
-
4:17
the store you want to search if you want to limit the results for
-
4:20
specific countries, the media type you want to search and so on.
-
4:24
Now we're going to be restricting the media type to music for
-
4:28
the scope of this app.
-
4:29
But we're going to attempt to design it in a way that if you wanted to modify
-
4:33
the app to search for movies, it'll be pretty easy.
-
4:36
The next parameter in the list is called entity.
-
4:40
Once we strict the media type, we want to search for let's say music,
-
4:44
there still a broad range of types of results that we could get back.
-
4:49
So let's say that we type Taylor Swift and specify using a key value parameter
-
4:54
that the media type here that we want is music.
-
4:58
The results could still include the artists, tracks
-
5:02
any tracks with the words with Taylor or Swift are in the name, or similarly any
-
5:06
albums that contains those words in the title, music videos, songs and so on.
-
5:11
So first off, to model all that this course would be like 19 hours long.
-
5:16
And second, we'd have to create models that could accommodate for
-
5:20
all these different kinds of media.
-
5:23
Using the entity parameter though, we can restrict our entities to the very specific
-
5:28
kinds and you'll see here for music you can restrict it to this list.
-
5:31
For example, musicArtist, music rack, musicVideo or albums only.
-
5:37
The entity you specify is relative to the media type selected.
-
5:42
So, if we're to specify that the media type was podcast,
-
5:46
we could not then specify that the entity is musicArtist.
-
5:51
If you did do that, the server would return an error.
-
5:54
To prevent these kinds of simple errors from occurring,
-
5:58
we're going to try and define our URL creation
-
6:01
code in a way that doesn't allow us to create these mis-matches.
-
6:05
After entity, the next important parameter is attribute.
-
6:10
Just as entity restricts the resulting data set,
-
6:13
the attribute specifies the attribute we want to search for in the store,
-
6:17
therefore restricting our search even further.
-
6:20
This is easy to understand.
-
6:22
For example, let's say when we search through music, we
-
6:25
only want to search through album titles using our keywords or through artists.
-
6:30
As the example in here states, with the keyword Maroon 5,
-
6:34
we can restrict our search to only match artists with that name, rather
-
6:39
than all artists who have ever recorded a song with the word maroon in the title.
-
6:44
Also, just like Entity,
-
6:46
the attributes we specify are relative to the media type selected.
-
6:50
So you can not specify actor term as the attribute when searching through music for
-
6:55
example since that only applies to movies.
-
6:58
These third column in this table use the most important part of constructing
-
7:02
the URL, and that is whether the parameter is required or not.
-
7:07
So up at the top of the table, you should see that the search term and
-
7:11
country are required, but everything else is optional.
-
7:14
Although to be honest, when I'm constructing my URLs,
-
7:17
I'm going to omit the country code, as you will see, and it works perfectly fine.
-
7:21
So really, we don't have to bother with media, entity, and attribute.
-
7:25
But we will, because for starters, excluding those means that the resulting
-
7:29
dataset will include products from the iTunes Store, the App Store,
-
7:32
the iBook Store, and the Mac App Store and we don't want to parse all of that.
-
7:37
But more importantly, by including these additional attributes, we can take a look
-
7:42
at how we can construct a complex URL in code without leaving any room for errors.
-
7:47
Okay, so
-
7:48
that is a bit of information about how we interact with the search part of the API.
-
7:53
We're going to interact with another endpoint.
-
7:55
But before we get to that, how do we know what kind of data we get back by
-
7:59
interacting with the search part, using all these parameters, key and value pairs?
-
8:05
Now, this is where the documentation is lacking.
-
8:08
So if you scroll all the way down, right after this look up examples,
-
8:13
there's a section called understanding search results.
-
8:16
And here there's an example of a JSON response that we could get back and
-
8:21
right below that is a description of the values, the keys and
-
8:25
values that you get in this response, over here in the table.
-
8:29
Unfortunately though, this is only for the sample provided, so
-
8:33
this table does not contain descriptions about every possible key value pair.
-
8:38
We need some more information and for
-
8:41
that we need to interact with the API directly and inspect responses.
-
8:45
But that's not easy, right?
-
8:47
I mean accessing the network, constructing a URL properly with encoded parameters and
-
8:52
inspecting the JSON result, that's like 80% of the work in this course.
-
8:57
Thankfully there're great tools that we can use to make this initial step easier.
-
9:01
Let's check it out in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up