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
With a fetch method encapsulated in the protocol all our Yelp client needs to do is provide a small wrapper around interacting with the Yelp API. Everything else is handled by the protocol or small related types.
Resources
-
0:00
The second fetch method we defined is nearly identical to the first, except for
-
0:04
the fact that instead of getting a single model instance back
-
0:07
we're getting an array of models.
-
0:10
Let's copy the method signature over from the protocol body, so
-
0:13
all the way up at the top here.
-
0:14
We're gonna grab this second one, hit CMD C to copy, and
-
0:19
we're going to add it to the extension like before,
-
0:21
along with a set of braces to provide an implementation, a default implementation.
-
0:27
For the body, let's go ahead and copy the implementation of the previous method
-
0:31
over, so everything from let task to task.resume.
-
0:35
CMD C, and then CMD V to paste,
-
0:39
we just have to make some minor modifications to get it to work.
-
0:43
So at the bottom here after getting the JSON out,
-
0:46
we'll use the parse function again to convert from our JSON to our end result.
-
0:52
The end result here isn't an optional t but an array of t, so for
-
0:56
starters we don't need an if let statement here.
-
0:59
With this approach, the failure is going to be indicated by an empty array.
-
1:02
So we can check to see if an array is not empty and then execute the success case.
-
1:07
So over here, we'll say, let value = parse(json).
-
1:14
And you'll see that the type of value now is an array of t.
-
1:17
Well, it'll be an error here because we have two values.
-
1:20
So instead of an if let here, we'll say if value or
-
1:26
if value is not empty, then hey this is a success case so we'll go ahead and
-
1:33
call it, otherwise it's the failure case because we couldn't parse the JSON.
-
1:37
So now if you option click you should see that this is an array of t.
-
1:40
Pretty simple.
-
1:42
The API client protocol can now power a lot of the functionality of our clients.
-
1:47
We still need a concrete type to act as the client though,
-
1:50
an interface with the Yelp API directly.
-
1:52
So to the networking group, right click, New File,
-
1:57
Swift File, And we'll call this YelpClient.
-
2:04
The Yelp client class is going to be the main point of
-
2:07
interaction with the Yelp API.
-
2:10
So it will define a class in here, again called Yelp client, and
-
2:14
this is going to conform to the API client protocol.
-
2:18
Conforming to the protocol is really easy since we've provided default
-
2:21
implementations for all the methods requirements.
-
2:24
All we need to add to this class is a session, so
-
2:27
we'll say let session of type URL session.
-
2:31
To instantiate this class we'll provide a concession configuration object and
-
2:34
the o auth token.
-
2:36
We need the o auth token to successfully execute any requests we make.
-
2:40
So in it with a configuration of type URL session configuration,
-
2:45
and an oauthToken, and this is a string.
-
2:51
Now in here we'll say self.session = URLSession and
-
2:54
we'll initialize it with this configuration and simply pass it through.
-
2:58
Use the argument in the init method, and self.token = oauthToken.
-
3:04
Now, we'll need a stored property for the token, so let's add that.
-
3:08
And we'll make it private, so private let token: String.
-
3:12
To make it easier to initialize this class, since we're just going to be using
-
3:15
the default config every time, let's go ahead and add a convenience init.
-
3:19
So convenience init (oauthToken: String).
-
3:25
And in here we'll just call self.init, and we'll say .default for
-
3:29
the configuration and pass the token through.
-
3:32
Okay, let's dive right in.
-
3:33
This was simple stuff.
-
3:35
We're going to define a method to search for Yelp businesses given a search term.
-
3:40
Now if you go to project navigator and navigate to Endpoint.swift,
-
3:44
which is in the Networking group, you'll see here that the search endpoint,
-
3:49
so, defined in the Yelp enum.
-
3:51
So look for the Yelp enum which is easier from the jump bar, should be right here.
-
3:56
And then there's a search case, and
-
4:00
you'll see here that this accepts a few associated values.
-
4:03
So the method we define needs to accept values for these parameters, as well, so
-
4:07
that we can use them to create a valid endpoint.
-
4:10
So back to Yelp client, we'll call this method, search.
-
4:18
For the arguments, we'll start with the search term and coordinate,
-
4:21
because we need to specify a location.
-
4:23
So we'll say search(with term), we'll give it a local name of search or term rather,
-
4:28
and this is of type String at a particular coordinate of type Coordinate.
-
4:35
These are more or less the required terms that we want to specify and we'll go
-
4:39
ahead and define a few more parameters to make the search as flexible as we need to.
-
4:44
So the next parameter,categories.
-
4:48
The next parameter we'll define here allows the caller
-
4:52
of the method to specify a list of categories to restrict the search to.
-
4:56
And this is going to be an array of YelpCategory.
-
5:00
YelpCategory is a type I defined earlier.
-
5:04
To use it you'll need to provide a categories title and an alias.
-
5:08
Now I don't expect us to use this much, so we'll go ahead and
-
5:11
give it a default value of a empty array.
-
5:13
If you want to use this perimeter, check out the teachers notes for
-
5:16
a link to the category list section of the Yelp docs.
-
5:20
When searching for
-
5:20
a business around you, Yelp also allows you to specify a radius to search in.
-
5:25
So let's add that to our parameters list as well.
-
5:27
We'll ask for a radius of type Int.
-
5:30
But we'll make this optional, and we'll give it a default value of nil.
-
5:35
This way if you want to specify a radius to narrow your search down or
-
5:38
broaden it, go for it.
-
5:40
Otherwise we'll use the server's default value.
-
5:43
Speaking of defaults, the Yelp server only returns 20 results for a search query.
-
5:49
I want more, so let's define a limit parameter of type int and
-
5:54
we'll give it a default value of 50.
-
5:57
We'll also specify how we want to sort the results from the search.
-
6:02
By default the value for this is best match, but we're going to go with rating.
-
6:06
So we'll say sort by, sort type, and
-
6:11
the type here is Yelp.Yelpsorttype.
-
6:14
So if we command click this is a nested enum, so under the Yelp enum,
-
6:18
we have another enum in here.
-
6:22
So YelpSortType is a type I've defined and
-
6:24
here we're going to give it a default value of .rating.
-
6:27
You can Command click again into that type and
-
6:29
check out the different possible values.
-
6:32
Finally after accepting all these different arguments,
-
6:34
we need to define a completion handler so
-
6:36
that we can execute some logic when this task is complete.
-
6:40
The signature of this completion handler, well first,
-
6:42
it's gonna be an escaping enclosure.
-
6:44
And the signature is a closure with a type of result.
-
6:51
So the agreements need to go inside parenthesis, so we'll say result.
-
6:55
Now we know result is a generic method so we need to specify types for t and u.
-
7:00
Since search is not a generic method,
-
7:02
we can finally specify concrete types for result.
-
7:06
When we search using the Yelp API, the results we get back in a success case
-
7:10
is not a single business but an array of businesses.
-
7:13
So t here, is, we're going to substitute it now, is an array of Yelp,
-
7:20
Business, and then u is APIError.
-
7:26
Let's take a break here.
-
7:27
In the next video, let's provide an implementation for this method, and
-
7:31
go ahead and use it.
You need to sign up for Treehouse in order to download course files.
Sign up