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
Since the iTunes Search API documentation is not very detailed, we need to examine responses themselves to understand how to create our models. In this video, let's use a free tool that allows us to send requests without writing any code
-
0:00
We're going to use a service called Postman to poke around the iTunes
-
0:04
search API and see what the JSON looks like when we make a request.
-
0:08
In doing this, we'll get a better understanding of how to structure our data
-
0:12
models to work with the API, and how we can define some fake data to make
-
0:17
the initial parts of the project easier to build.
-
0:20
Let's open up a new tab and head over to getpostman.com.
-
0:25
Now you should go ahead and download the Mac client and
-
0:28
explore this website to see what this app is about.
-
0:31
Basically, Postman is a feature rich client that allows us to
-
0:36
pass a request in and inspect the response pretty easily.
-
0:40
It also happens to be free which is great for use in this course, now,
-
0:44
once you've downloaded the app it should look like this.
-
0:48
Services like Postman, again are made for exploring and building APIs,
-
0:52
without having to deal with all those tricky aspects like proper URL and coding.
-
0:56
So up at the very top over here, we have a search bar, essentially,
-
1:01
where we can enter a request URL.
-
1:04
And here we're going to add a URL and then hit Send to send off a request.
-
1:09
In the section below marked Response, we get a response, and we can inspect it.
-
1:14
Okay, so let's go back to our browsers and go back to the docs page,
-
1:20
and let's scroll up to that section marked Searching.
-
1:26
And we're gonna grab this base URL and path, so itunes.apple.com/search,
-
1:32
we'll hit copy, and then we'll go back to Postman.
-
1:36
And in this bar here, in the fields, let's enter that URL,
-
1:42
okay, I just hit Send by mistake but ignore that, we're not done just yet.
-
1:46
Now this isn't enough, as you can see my response doesn't contain anything.
-
1:50
We need to provide a set of parameters,
-
1:52
again some required, some optional to get the right data.
-
1:55
So, at the end of this box here, there's a button named Params.
-
2:00
If you click on here, in this vaguely visible section below,
-
2:04
we can list parameters as key value pairs.
-
2:07
Postman then takes care of the encoding and fires off the request for us.
-
2:11
We're going to explore a couple different requests, but for
-
2:14
now, let's see what information we get back when we search for a term.
-
2:19
So remember the first parameter key is term, and this is required,
-
2:23
and for the value I'm going to specify taylor.
-
2:27
If we send the request now, remember that we'll be searching for this term across
-
2:31
all the stores and getting back a result set of every possible match.
-
2:36
So let's go ahead and restrict this to music, and
-
2:38
the way that we do that is to specify a media type.
-
2:42
They key here is media, the value is music.
-
2:46
When you add these key value pairs to this list,
-
2:48
you can see in the URL text field how these values are added to that base URL.
-
2:54
We'll cover all of that in detail though, so if you're confused, don't worry.
-
2:58
Now searching in the music store should be sufficient, but
-
3:02
just to make things easier, I want the server to match our search term
-
3:06
only to artist names, and not songs or albums or anything else.
-
3:10
For that, we need to specify an entity or an attribute.
-
3:14
For the entity, the key is entity and
-
3:17
the value is musicArtist, written out just like that.
-
3:22
Lastly, the attribute key, so that is attribute and the value is artistTerm.
-
3:29
Now just as an FYI, in case you're confused about how at least I know whether
-
3:33
these are the right terms or how did I figure out what I need to type in here to
-
3:38
get this URL constructed properly.
-
3:40
Well, I had to play around with these for a bit before I actually understood how,
-
3:45
exactly, these attributes affected my query.
-
3:48
Like I said, the documentation isn't great, so some experimentation will be
-
3:52
required on your part if you want to modify this, but the great thing is with
-
3:56
an app like Postman, it should be relatively easy to do.
-
3:59
You don't have to write code just yet, okay, so
-
4:02
now we have a URL that models the kind of information we're searching for.
-
4:06
Just for the sake of this example, since we only want to see what
-
4:09
kind of data we get back, let's add another key value pair, and limit
-
4:14
the results to just 5 data points, so that we don't have a giant wall of JSON.
-
4:20
Now with all this set up, hit Send, and
-
4:22
you should get back a response in the section below.
-
4:27
Now by default, this section has a format of auto,
-
4:30
it tries to detect what data format this is in automatically.
-
4:34
But if we drop down and select JSON and
-
4:36
make sure it's on pretty, you'll see it's formatted better.
-
4:39
So this is what a response looks like,
-
4:41
we get an array called results at the top level.
-
4:45
And each item in this array is a nested dictionary,
-
4:49
the dictionary includes information about what type of result this is.
-
4:53
And as you see here, this is an artist, for
-
4:56
our purposes this is always going to be an artist, because we restricted the search.
-
5:00
But you can imagine how this is useful,
-
5:02
this wrapper type, if you wanted to search across all the stores and
-
5:06
show categorize results on one page, just like the actual iTunes app.
-
5:11
So what do we care about from the values here?
-
5:13
Well we want the name, the artist name, since we need to display it,
-
5:18
we also want the artist's ID if we want to query that artist further, and
-
5:22
maybe we want some information about the genre.
-
5:25
You'll notice there's no information here about any of the music this
-
5:28
artist has created, to get that, we need to send another request
-
5:32
with a different set of information using the lookup end point.
-
5:36
All in due time, now that we understand better how this initial JSON is going to
-
5:40
be structured, let's head over to Xcode, create a new project, and our first model.
-
5:46
This time, let's go with a single page application template,
-
5:49
a single view application, and build from there.
-
5:52
We could go with the master detail because as you saw, our app had just a list of
-
5:56
series of table views, but we don't want any of the boilerplate code.
-
5:59
We wanna just start from scratch, so single view it is, and
-
6:03
I'm going to call this iTuneClient, make sure the language is Swift.
-
6:08
I'm gonna select a Team just so it doesn't have that initial error
-
6:11
in the signing identities spot, and then figure out where you wanna save it.
-
6:20
Let's put this in a folder called iTunesClient, there we go.
-
6:26
Let's create a couple groups in here to start out so
-
6:28
that our project remains somewhat sane and organized.
-
6:31
So New Group, Models, or Models, there we go,
-
6:37
New Group, Views, and finally Controllers.
-
6:44
In the Model group let's create our first new file, so
-
6:47
New File empty Swift File and we'll call this Artist.
-
6:53
We usually define our models as struts, but
-
6:56
we need reference types this time around.
-
6:59
We're going to be passing objects around from one view controller to the next and
-
7:03
mutating certain properties based on user input.
-
7:06
With a copy behavior of value types this can get messy.
-
7:10
We'll also need to maintain some state across our models and
-
7:14
value types aren't good at that.
-
7:15
Initially it's going to seem like there really isn't a reason for
-
7:19
classes, as we write our networking code you'll see why.
-
7:22
So class Artist, and
-
7:26
to start with let's add two simple properties to store the ID and name.
-
7:30
So let id of type Int and let name of type String,
-
7:35
that's not all we wanna store though.
-
7:37
How about the genre?
-
7:38
That would certainly be useful to display, if you go back and inspect the response,
-
7:43
you can see that there are two data points returned here, a genre name and an ID.
-
7:49
Since I've already explored the API, I know that there are some cases where only
-
7:53
a name is returned, and some, like this one, where we could get an ID as well.
-
7:58
We could simply store the name of the genre as a string in our model, but
-
8:02
we should try news types wherever we can to avoid mistakes and
-
8:06
to get the compiler to help us.
-
8:07
If we go back to the documentation that is the iTunes documentation.
-
8:12
Go up to the URL over here and
-
8:14
get rid of all of this until documentation and hit enter.
-
8:19
You should go to kinda the main reference page,
-
8:20
and if you scroll down there is a link to a Genre IDs Appendix.
-
8:26
As you can see here, there are a lot of genres, this list is pretty long.
-
8:32
But thankfully, not all of these pertain to music, so if you scroll around a bit,
-
8:36
you can find the music section right here.
-
8:42
But even under music there are quite a few genres and then even more sub genres.
-
8:47
So let's model just the top level genres under music for our app, so blues,
-
8:52
comedy and so on.
-
8:53
Your first thought might be to create a tiny struct to model these points, but
-
8:58
as you can see the list of genres is a finite one.
-
9:01
So we can use an enum to model this,
-
9:03
we're going to give this enum a raw value of int to store this genre ID.
-
9:08
And then add an init method that lets us
-
9:10
initialize the type using a stringly type name.
-
9:13
This way we can easily create a genre value from either the ID or name.
-
9:18
Now, this is a lot of typing if we were going to model all this,
-
9:21
so in the teacher's notes to this video there is a link to a Genre.swift.
-
9:26
Create a new file under Models named genre,
-
9:32
Genre, and then just drop in the code.
-
9:36
At this point you should be extremely familiar with creating models and
-
9:39
working with Swift types, so
-
9:41
unless we're doing something extraordinary we'll be adding a good amount of code in,
-
9:45
especially in cases like this, where most of the work is just typing.
-
9:49
Now back in the Artist model let's add a property for the genre.
-
9:57
We need to add one more property, an artist has albums to their name and
-
10:01
we'll model that as an array on the Artist type.
-
10:05
Initially, when we create an instance of an Artist to display in the search
-
10:09
results, we won't have any album data.
-
10:11
Well that's okay, because we can just initialize the property to an empty array.
-
10:16
When we fetch relevant album data, we'll mutate that value.
-
10:19
So this needs to be a variable property, but
-
10:22
first we need an album type, so in the Models group let's add a new file.
-
10:27
We'll name it Album, and in here we'll create an empty class,
-
10:31
named Album, just for now, we'll get to this in a second.
-
10:35
But now back in Artist we can say var albums an array of Album.
-
10:43
Let's finish this type up by adding in a simple initializer,
-
10:48
so we'll end it with id, type Int, name type String,
-
10:53
primaryGenre of type Genre albums, which is an array of Album, and then easy stuff.
-
11:01
We'll just assign each one back to the stored property is self.name
-
11:07
= name self.primaryGenre = primaryGenre, and albums = albums.
You need to sign up for Treehouse in order to download course files.
Sign up