1 00:00:00,830 --> 00:00:07,220 Welcome back and this lecture, you will learn about the HTP interceptor, which can be used to intercept 2 00:00:07,220 --> 00:00:09,020 or change values of a request. 3 00:00:09,230 --> 00:00:15,350 So in this case, the API key since we are passing the API key and more than one method can make it 4 00:00:15,350 --> 00:00:21,530 available in the header so that it can always be set by the interceptor where it is required. 5 00:00:22,100 --> 00:00:27,740 So let's go ahead, and first of all, make sure that we added the dependency to our Gradle. 6 00:00:28,350 --> 00:00:35,300 Therefore, let's go over to the build, but Gradle and at interceptor dependency. 7 00:00:37,330 --> 00:00:45,010 Which is the OK age TGP three interceptor, so let me put that in here. 8 00:00:45,580 --> 00:00:46,430 It's this one here. 9 00:00:46,460 --> 00:00:47,680 Com Square up, OK. 10 00:00:47,710 --> 00:00:51,610 HDP three logging interceptor 472. 11 00:00:51,910 --> 00:00:55,600 So let me sink my project after having done that. 12 00:00:56,140 --> 00:01:05,349 So now in our API file, once the seeking is done, we can go ahead and set up an HDP client. 13 00:01:05,800 --> 00:01:08,350 OK, so we have the Moshi, we have retrofit. 14 00:01:08,590 --> 00:01:19,540 But now let's also just before we set up our retrofit, let's set up the H.T. he client, which is going 15 00:01:19,540 --> 00:01:25,570 to be an okay HTP client builder. 16 00:01:26,080 --> 00:01:30,690 So we need to build it and apply the changes. 17 00:01:30,730 --> 00:01:31,960 So what do we want to apply? 18 00:01:32,320 --> 00:01:39,820 So here I'm going to add an interceptor where I need to pass an interceptor and here how that's going 19 00:01:39,820 --> 00:01:50,560 to work is I get a chain with a builder, so I set up the builder where I use my chain request and with 20 00:01:50,560 --> 00:01:51,670 the new builder. 21 00:01:53,620 --> 00:02:01,510 And then I used this builder to add a header to it and not the head, but a header and the header name 22 00:02:01,510 --> 00:02:03,820 is going to be X AP I. 23 00:02:06,130 --> 00:02:06,580 Key. 24 00:02:08,830 --> 00:02:11,530 And the value will be my API key. 25 00:02:11,920 --> 00:02:17,170 So this one here, actually we can also use API key here like so. 26 00:02:19,390 --> 00:02:21,340 And now we need to return something. 27 00:02:21,520 --> 00:02:33,220 So let's return at interceptor, a chain that we have gotten and we proceed with our builder that we 28 00:02:33,220 --> 00:02:33,580 built. 29 00:02:35,130 --> 00:02:35,510 OK. 30 00:02:35,580 --> 00:02:38,070 This has to be closely linked. 31 00:02:38,520 --> 00:02:42,600 OK, so that's how we set up an FTP client, so there's a lot going on here. 32 00:02:43,410 --> 00:02:49,950 So we set up an HDB client using the OK to be client class, which we get from the dependency that we 33 00:02:49,950 --> 00:02:54,180 just added, and it performs best when you create the single page. 34 00:02:54,450 --> 00:02:54,780 OK. 35 00:02:54,840 --> 00:02:58,860 GP client and reuse it for all your HTP calls, which is what we're going to do. 36 00:02:59,340 --> 00:03:04,350 So then we are using the builder to now build the client. 37 00:03:04,660 --> 00:03:11,000 Then we call the apply method, which calls the specific function block with this value. 38 00:03:11,010 --> 00:03:15,360 So we're just saying that we're adding an interceptor and the interceptor. 39 00:03:15,780 --> 00:03:24,720 Once we use that, we get a chain and we use that chain to set up a builder where we then pass the header, 40 00:03:24,720 --> 00:03:28,530 which has two parameters a key and two value. 41 00:03:28,890 --> 00:03:34,380 So the key is going to be the API key, and the value is going to be the actual key that we have and 42 00:03:34,380 --> 00:03:39,120 we need to return it interceptor using the chain, proceeding with the builder. 43 00:03:39,260 --> 00:03:41,040 So we had set up and we build it. 44 00:03:41,400 --> 00:03:45,840 Now if you were wondering where I get this exchange API key from. 45 00:03:46,170 --> 00:03:48,020 So it's from the request parameter. 46 00:03:48,060 --> 00:03:53,430 So this one is required in the HTP header you saw our program worked without. 47 00:03:53,430 --> 00:04:00,720 But if we want to use it properly, we need to use the header and passed the API key via the header, 48 00:04:01,140 --> 00:04:02,370 which we are now doing. 49 00:04:03,270 --> 00:04:03,810 OK. 50 00:04:04,590 --> 00:04:07,520 So that's the keyword that you saw just there. 51 00:04:09,060 --> 00:04:18,420 All right, now, we can use this HTP client when we are setting up our retrofit object and we can use 52 00:04:19,079 --> 00:04:26,040 the Dot client method here and just pass our HTP client to it. 53 00:04:26,310 --> 00:04:31,770 Now, in order for this to work, we actually need to build our HDTV client like so. 54 00:04:31,980 --> 00:04:32,270 All right. 55 00:04:32,280 --> 00:04:38,160 Once we build it, then we can pass the client HTP client to our client method here. 56 00:04:40,400 --> 00:04:46,490 Now, at this point, we can go over to our new service and get rid of the API key here. 57 00:04:46,760 --> 00:04:52,970 OK, we don't need to pass the API key anymore because we're passing it via the header, so we don't 58 00:04:52,970 --> 00:04:55,550 need to pass it in our methods anymore. 59 00:04:58,630 --> 00:05:04,270 Accordingly, in our news manager, we don't need to pass the API key anymore, so we need to get rid 60 00:05:04,270 --> 00:05:11,950 of this line here where we got the articles by category and the same thing goes for this part here where 61 00:05:11,950 --> 00:05:13,300 we get all articles. 62 00:05:16,760 --> 00:05:21,470 All right, now, let's rerun our application just to check if it's still going to work as intended. 63 00:05:26,120 --> 00:05:30,680 And we will see that it still works as it did before. 64 00:05:31,130 --> 00:05:35,270 So since we are talking about interceptors, let's look at the logging interceptor. 65 00:05:35,780 --> 00:05:42,200 The logging interceptors help you see the response status of your request right on the locket as they 66 00:05:42,200 --> 00:05:42,500 happen. 67 00:05:42,860 --> 00:05:48,380 So this is a good way to quickly find out what exactly is going on with your request. 68 00:05:48,950 --> 00:05:56,690 So let's go over to our API Katy file, and here I'm going to set up a logging variable. 69 00:05:57,740 --> 00:06:07,790 So while logging, which will be HTP logging interceptor, so it's going to be a logging interceptor 70 00:06:07,790 --> 00:06:12,470 that we can now use in our H2B client. 71 00:06:13,250 --> 00:06:13,880 So here. 72 00:06:16,420 --> 00:06:26,440 Before we build it, so after we added the interceptor, we can use our logging level and we can set 73 00:06:26,440 --> 00:06:31,960 that to HTP, a logging interceptor level thought body. 74 00:06:34,100 --> 00:06:39,500 And then we can add the network interceptor called logging. 75 00:06:42,100 --> 00:06:48,940 So this will give us the details about what's going on in our Lochhead directly, so logging meaning 76 00:06:48,940 --> 00:06:51,490 logging something on the blockade. 77 00:06:51,850 --> 00:06:52,990 In this particular case? 78 00:06:53,590 --> 00:06:56,020 So now if we run our application again? 79 00:06:57,750 --> 00:07:02,520 And we searched a locket for, OK, TTP. 80 00:07:03,850 --> 00:07:05,590 We're going to find. 81 00:07:07,670 --> 00:07:18,410 Here to AP API key, the host, the connection, the accept ending and coding user agent and so forth. 82 00:07:18,650 --> 00:07:21,590 And here this the response that we're getting. 83 00:07:21,620 --> 00:07:25,580 So headlines country us and 248 milliseconds. 84 00:07:25,580 --> 00:07:31,340 We got this response and we get the response code of 200, which means all good. 85 00:07:34,100 --> 00:07:41,300 So this is a little bit extra code that we had to set up, but this will help us to figure out what 86 00:07:41,300 --> 00:07:47,210 is going on if something happens with a request so we can quickly get a better hint on what the issue 87 00:07:47,210 --> 00:07:49,640 actually is without having to guess. 88 00:07:49,940 --> 00:07:52,070 We just get the additional details. 89 00:07:52,790 --> 00:07:53,220 All right. 90 00:07:53,270 --> 00:07:57,770 So that's it for this video and the next one, we're going to look at the source of screen. 91 00:07:57,920 --> 00:07:58,850 So see you there.