1 00:00:00,190 --> 00:00:02,990 With all this working, I wanna show you how you can set up multiple 2 00:00:02,990 --> 00:00:05,000 APIs within a single app using Retrofit. 3 00:00:06,140 --> 00:00:13,630 So you may have in your app, connecting to Twitter, Facebook, Imgur, multiple APIs. 4 00:00:13,630 --> 00:00:15,730 And to do that, it's really easy. 5 00:00:15,730 --> 00:00:18,750 So in this app as an example what we're gonna do is 6 00:00:18,750 --> 00:00:23,360 we're gonna think of the Imgur API as kinda two different APIs and 7 00:00:23,360 --> 00:00:27,570 that's the anonymous and the authenticated. 8 00:00:27,570 --> 00:00:32,568 So we've been using the authenticated API thus far where the user has to sign in and 9 00:00:32,568 --> 00:00:34,675 we upload images on their behalf. 10 00:00:34,675 --> 00:00:39,732 The next part we want to do is add the ability to anonymously upload images and 11 00:00:39,732 --> 00:00:42,893 it turns out it's a really straightforward and 12 00:00:42,893 --> 00:00:45,910 easy because retrofit makes it really easy. 13 00:00:45,910 --> 00:00:50,500 So first we need to do is add a new API and just like we did it for 14 00:00:50,500 --> 00:00:55,350 our interface for authenticating off, we're gonna do the exact same thing. 15 00:00:55,350 --> 00:01:00,461 We're gonna add a new interface for Anon. 16 00:01:00,461 --> 00:01:05,680 And we only have one method that we want which is the uploadImage and 17 00:01:05,680 --> 00:01:10,630 it turns out it's the exact same as our uploadImage from before. 18 00:01:10,630 --> 00:01:13,334 It looks the exact same so I'll just go ahead and copy that over. 19 00:01:18,618 --> 00:01:22,724 So if you're using completely different APIs like Twitter or Facebook, 20 00:01:22,724 --> 00:01:27,040 you would just have a different interface with the names of each one of those. 21 00:01:28,530 --> 00:01:33,000 So here we have the upload image to the exact same API end point, but 22 00:01:33,000 --> 00:01:34,330 we're gonna do it anonymously. 23 00:01:34,330 --> 00:01:36,650 The next part we add to the service. 24 00:01:38,230 --> 00:01:43,790 So here we can just add a new method, and it actually looks 25 00:01:43,790 --> 00:01:47,040 basically identical to this one that we've already done so let's go and copy that. 26 00:01:50,130 --> 00:01:55,299 Put it in here and instead of off the API it's gonna be AnonApi and 27 00:01:55,299 --> 00:02:00,110 it's 'gonna return back to the Anon interface instance. 28 00:02:03,856 --> 00:02:09,420 Same base URL, same clien, Gson factory is all good. 29 00:02:09,420 --> 00:02:14,030 The only difference here is, we're not gonna have the access token anymore. 30 00:02:14,030 --> 00:02:17,585 The access token was only given to us when we signed the user in and, 31 00:02:17,585 --> 00:02:20,660 we're not gonnas have the user sign in for this. 32 00:02:22,170 --> 00:02:26,410 So instead, the documentation, which I've linked to in the teacher's notes, 33 00:02:26,410 --> 00:02:29,240 says that we can still upload anonymously. 34 00:02:29,240 --> 00:02:32,090 We just have to change the header for the authorization, 35 00:02:32,090 --> 00:02:34,020 and that's we're gonnas do here. 36 00:02:34,020 --> 00:02:38,542 So, instead of the Bearer, what we do is we just say our Client-ID. 37 00:02:41,727 --> 00:02:45,830 And we kept track of that earlier, so let's go ahead and get it. 38 00:02:48,200 --> 00:02:51,290 And that's the only difference between these two APIs. 39 00:02:52,450 --> 00:02:57,410 One uses the Client-ID the other uses the access token. 40 00:02:58,420 --> 00:03:01,710 So now we can go ahead and head back to our main activity and 41 00:03:01,710 --> 00:03:03,500 add the ability to anonymously upload. 42 00:03:03,500 --> 00:03:06,733 We already have a button for that uploadAnon, it's gonna go down there. 43 00:03:09,648 --> 00:03:12,641 And here will just add a method like we did before, 44 00:03:12,641 --> 00:03:15,708 this one is called uploadAnon and it's actually, 45 00:03:15,708 --> 00:03:21,070 nearly identical to this other one because the API is pretty much the exact same. 46 00:03:21,070 --> 00:03:27,140 So let's just copy this, paste it and change the name. 47 00:03:30,390 --> 00:03:34,630 And it can indicate that it's Anon. 48 00:03:34,630 --> 00:03:36,800 And then we just need to get the other APIs. 49 00:03:36,800 --> 00:03:39,231 So get the AnonApi.. 50 00:03:39,231 --> 00:03:42,760 It turns out it's the exact same parameters. 51 00:03:42,760 --> 00:03:46,020 So that's all the same, already in the same file. 52 00:03:46,020 --> 00:03:48,540 The only difference here is what it comes back, 53 00:03:48,540 --> 00:03:51,990 when i'm gonna fetch the account images here because we're not signed in. 54 00:03:51,990 --> 00:03:54,280 So instead of that what we wanna do is let's go ahead and 55 00:03:54,280 --> 00:03:59,680 just go to this newly uploaded image and that has a URL. 56 00:03:59,680 --> 00:04:04,740 So when this comes back we get the image object and we're going to 57 00:04:04,740 --> 00:04:09,740 grab the URL which they call little link and then just direct the user to that. 58 00:04:09,740 --> 00:04:11,562 So we can do that using an Intent. 59 00:04:11,562 --> 00:04:16,585 So we'll say startActivity, new Intent, 60 00:04:16,585 --> 00:04:23,115 Intent.Action_VIEW and we just need to parse the URI. 61 00:04:23,115 --> 00:04:27,659 And we're gonna get that from our response.body, 62 00:04:27,659 --> 00:04:30,452 data which is the image, link. 63 00:04:34,069 --> 00:04:38,790 So if we successfully upload this image we should then mean directed to 64 00:04:38,790 --> 00:04:42,078 the browser where that image resides with a URL. 65 00:04:44,518 --> 00:04:49,248 So let's run this and we can see how the user would upload it anonymously an image. 66 00:05:00,341 --> 00:05:02,900 So in this case we haven't signed in yet. 67 00:05:02,900 --> 00:05:05,610 But we have the upload anonymous image option. 68 00:05:05,610 --> 00:05:06,620 So let's go ahead and choose it. 69 00:05:12,850 --> 00:05:17,379 And we can see that when I came back, we were able to fetch that URL and 70 00:05:17,379 --> 00:05:21,290 go to the browser to show that image that we just uploaded. 71 00:05:22,890 --> 00:05:24,490 Now that we've used Retrofit, 72 00:05:24,490 --> 00:05:28,900 you can see how easy it is to add new API calls to your apps. 73 00:05:28,900 --> 00:05:33,870 It handles the hard parts with it's easy syntax and friendly callback mechanism. 74 00:05:33,870 --> 00:05:36,930 You can also see the power retrofit in ok HTTP 75 00:05:36,930 --> 00:05:40,180 have to customize requests however you need. 76 00:05:40,180 --> 00:05:43,190 I can't wait to see what kinds of apps you all come up with next.