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
We have a way to obtain the URLs we need, but the information encapsulated is incomplete. In this video, let's look at one way we can model the entity values.
-
0:05
By conforming to the end point protocol, the iTunes enum let's us
-
0:09
easily create URLs for making requests to the search API.
-
0:13
But we haven't modeled it exactly how we want.
-
0:16
When we played around with the API in postman, we used the entity and attribute
-
0:21
keys to restrict the search even further and keep our results somewhat sane.
-
0:25
If we go back to the documentation again, and take a look at the entity and
-
0:30
attributes keys in the parameters list, you'll see that it's not straightforward.
-
0:35
Like the media key entity has subsets of values that
-
0:38
depend on the media types selected, and same goes for the attribute.
-
0:42
There are a couple ways we can model this, but ultimately the end goal
-
0:47
is to make the final use site as easy and as clear as possible.
-
0:52
To our endpoint group over here, let's add a new Swift file, and
-
0:57
we'll call this iTunes entity.
-
1:03
The first approach that comes to mind is to create an enum called iTunes entity
-
1:08
to model the top level entity types.
-
1:11
Now, these are the same values as encapsulated in the iTunes media type, so
-
1:16
let's just go to the media enum right here, and we'll copy paste this over.
-
1:25
Instead of calling it ItunesMedia,
-
1:26
we'll call this ItunesEntity but this isn't sufficient, right?
-
1:32
For the music case right here,
-
1:33
there are six possible entity values we can include in the parameter as a value.
-
1:39
We can modify each of these enum members,
-
1:42
to carry those specific arguments as associated values.
-
1:46
For example, let's create another type here, we'll create a second enum and
-
1:52
we'll call this music entity and model the actual value as members of this type.
-
1:58
So we'll say case musicArtist, musicTrack and
-
2:02
these are the same values that are listed in the description section for
-
2:08
the entity key or under music at least.
-
2:11
And case album,
-
2:14
case musicVideo, mix and song.
-
2:21
Now the purpose of this type is to allow us to specify the value for
-
2:26
the entity parameter easily without resorting to strings.
-
2:31
So one way we can do that is by conforming to custom string convertible and
-
2:36
returning the string we need for the value.
-
2:40
So if I make this enum have a rawValue of a String,
-
2:45
we can then in an extension of MusicEntity
-
2:50
conform to CustomStringConvertable and we'll just return for
-
2:55
the description key the Strings or the enums rawValue.
-
3:01
But this isn't entirely useful.
-
3:03
Because in the iTunes enum, we're using the ItunesEntity, or
-
3:08
we plan to use the ItunesEntity, as our top-level type.
-
3:13
So what do I mean by that?
-
3:15
Back in the ItunesEntity, over here,
-
3:18
we can add an associated value to each member that provides information
-
3:23
about each subset modeled by these furthered types that we've described.
-
3:28
So in the music case for example,
-
3:29
we can say case music carries an associated value of type MusicEntity.
-
3:35
Without implementing the rest, so movie, podcast,
-
3:38
none of these, let's see how this would work out.
-
3:41
So here we have a top level enum ItunesEntity, and
-
3:45
it has cases to represent each media type.
-
3:48
Each media type then carries as an associated value,
-
3:52
the values for the entity under that category.
-
3:56
So music contains all the music entity values.
-
3:59
Which are these, one, two, thee, four, five, six.
-
4:03
So how are we gonna use this?
-
4:04
Let's go back to the endpoint file and
-
4:07
in the iTunes enum, let's add an associated value for the entity.
-
4:13
So here we'll say entity of type ItunesEntity.
-
4:17
And again this is going to be optional.
-
4:20
We'll get an error now and that's because the switch statement inside the query
-
4:24
items computer property needs to be fixed.
-
4:26
The enum case we're matching against is now incorrect because over here
-
4:30
we're not accounting for the entity value, we just add it as an associated value.
-
4:35
So we'll say let entity.
-
4:39
Now once we add that, we can try and
-
4:41
get the entity parameter into our query items array.
-
4:45
So over here, first, we need to unwrap it.
-
4:48
Entity being an optional.
-
4:50
So we'll say if let entity = entity.
-
4:56
Now, unfortunately inside here, it's not so easy to get at that value.
-
5:01
The entity type here is represented by an enum, it's that top level iTunes enum.
-
5:08
We need to switch on to each case and
-
5:11
then each of those cases members has an associated value.
-
5:15
And that associated value is also represented by an enum.
-
5:19
Anyway, let's give it try.
-
5:20
And for the sake of exploring again, we'll just focus on the music case.
-
5:25
So inside of here, I can say switch on the entity.
-
5:30
And we'll say case .music.
-
5:33
And it carries an associate value to represent the music entity.
-
5:36
So say, let entity, or let musicEntity to make it more clear.
-
5:43
And now inside here, I can say let musicEntity =
-
5:48
URLQueryItem Name is entity and
-
5:52
the value is music entity.description.
-
5:56
Because remember, we added that conformance.
-
5:59
Finally we can append this to the result.
-
6:05
And now we have the music entity value inside of our array.
-
6:10
Let's just add a default case for the rest since we're not going to implement it, and
-
6:13
we'll break out of the switch statement.
-
6:15
This is a lot of work already, and
-
6:17
remember, we're just covering one entity here.
-
6:21
If we really want to model all the entity values, this is going to be a very large
-
6:25
switch statement because we have to match on each case, get that associative
-
6:30
value as an entity and then duplicate these lines of code for every single one.
-
6:34
And then, after we do all that, we then have to cover the attributes case.
-
6:39
So let's ignore all of that right just for
-
6:42
now we'll pretend that this is the way we want to go.
-
6:45
Let's see how this works out.
-
6:47
So if we go to the ViewController.swift we can now
-
6:50
test how this writes at the call site.
-
6:53
So Itunes.search now is an error because we have an entity associated value.
-
6:59
And for this we'll say .music because first we need that top level type
-
7:04
ITunesEntity and
-
7:05
then inside of the associated value you could say .musicArtist.
-
7:10
If we run now, in the console we have the value that we want So
-
7:16
this seems okay at first but theres one obvious and one subtle thing going on.
-
7:22
The obvious bit that pops out to me at the call site
-
7:25
is that there is a lot of repetition.
-
7:28
We've specified that the media type is music and then the entity type is
-
7:32
also music and then the actual values deep in there and that is music artists.
-
7:37
The non-obvious thing though, is that because the arguments for media and
-
7:42
entity are separate arguments with different types, there's nothing
-
7:47
preventing me from specifying the media as music and the entity as podcast.
-
7:53
Then something we said, we explicitly want to avoid.
-
7:57
We don't want to have to think about these things, right.
-
7:59
I don't want you to remember that this
-
8:02
is going to end up in an error on the service site.
-
8:05
So this is a no-go for me.
-
8:07
This approach is not gonna work.
-
8:09
In the next video, let's take a look at this problem in a different manner
You need to sign up for Treehouse in order to download course files.
Sign up