Welcome back and this lecture, you will learn about the HTP interceptor, which can be used to intercept
or change values of a request.
So in this case, the API key since we are passing the API key and more than one method can make it
available in the header so that it can always be set by the interceptor where it is required.
So let's go ahead, and first of all, make sure that we added the dependency to our Gradle.
Therefore, let's go over to the build, but Gradle and at interceptor dependency.
Which is the OK age TGP three interceptor, so let me put that in here.
It's this one here.
Com Square up, OK.
HDP three logging interceptor 472.
So let me sink my project after having done that.
So now in our API file, once the seeking is done, we can go ahead and set up an HDP client.
OK, so we have the Moshi, we have retrofit.
But now let's also just before we set up our retrofit, let's set up the H.T. he client, which is going
to be an okay HTP client builder.
So we need to build it and apply the changes.
So what do we want to apply?
So here I'm going to add an interceptor where I need to pass an interceptor and here how that's going
to work is I get a chain with a builder, so I set up the builder where I use my chain request and with
the new builder.
And then I used this builder to add a header to it and not the head, but a header and the header name
is going to be X AP I.
Key.
And the value will be my API key.
So this one here, actually we can also use API key here like so.
And now we need to return something.
So let's return at interceptor, a chain that we have gotten and we proceed with our builder that we
built.
OK.
This has to be closely linked.
OK, so that's how we set up an FTP client, so there's a lot going on here.
So we set up an HDB client using the OK to be client class, which we get from the dependency that we
just added, and it performs best when you create the single page.
OK.
GP client and reuse it for all your HTP calls, which is what we're going to do.
So then we are using the builder to now build the client.
Then we call the apply method, which calls the specific function block with this value.
So we're just saying that we're adding an interceptor and the interceptor.
Once we use that, we get a chain and we use that chain to set up a builder where we then pass the header,
which has two parameters a key and two value.
So the key is going to be the API key, and the value is going to be the actual key that we have and
we need to return it interceptor using the chain, proceeding with the builder.
So we had set up and we build it.
Now if you were wondering where I get this exchange API key from.
So it's from the request parameter.
So this one is required in the HTP header you saw our program worked without.
But if we want to use it properly, we need to use the header and passed the API key via the header,
which we are now doing.
OK.
So that's the keyword that you saw just there.
All right, now, we can use this HTP client when we are setting up our retrofit object and we can use
the Dot client method here and just pass our HTP client to it.
Now, in order for this to work, we actually need to build our HDTV client like so.
All right.
Once we build it, then we can pass the client HTP client to our client method here.
Now, at this point, we can go over to our new service and get rid of the API key here.
OK, we don't need to pass the API key anymore because we're passing it via the header, so we don't
need to pass it in our methods anymore.
Accordingly, in our news manager, we don't need to pass the API key anymore, so we need to get rid
of this line here where we got the articles by category and the same thing goes for this part here where
we get all articles.
All right, now, let's rerun our application just to check if it's still going to work as intended.
And we will see that it still works as it did before.
So since we are talking about interceptors, let's look at the logging interceptor.
The logging interceptors help you see the response status of your request right on the locket as they
happen.
So this is a good way to quickly find out what exactly is going on with your request.
So let's go over to our API Katy file, and here I'm going to set up a logging variable.
So while logging, which will be HTP logging interceptor, so it's going to be a logging interceptor
that we can now use in our H2B client.
So here.
Before we build it, so after we added the interceptor, we can use our logging level and we can set
that to HTP, a logging interceptor level thought body.
And then we can add the network interceptor called logging.
So this will give us the details about what's going on in our Lochhead directly, so logging meaning
logging something on the blockade.
In this particular case?
So now if we run our application again?
And we searched a locket for, OK, TTP.
We're going to find.
Here to AP API key, the host, the connection, the accept ending and coding user agent and so forth.
And here this the response that we're getting.
So headlines country us and 248 milliseconds.
We got this response and we get the response code of 200, which means all good.
So this is a little bit extra code that we had to set up, but this will help us to figure out what
is going on if something happens with a request so we can quickly get a better hint on what the issue
actually is without having to guess.
We just get the additional details.
All right.
So that's it for this video and the next one, we're going to look at the source of screen.
So see you there.