1 00:00:00,541 --> 00:00:05,086 Not so long ago making our weather app or frankly, many websites, 2 00:00:05,086 --> 00:00:09,879 apps, or services, would have been incredibly challenging because 3 00:00:09,879 --> 00:00:13,468 access to unified weather data wasn't available. 4 00:00:13,468 --> 00:00:17,224 We would of had to go through the cumbersome task of pulling data together 5 00:00:17,224 --> 00:00:18,587 from different sources. 6 00:00:18,587 --> 00:00:23,027 Imagine trying to get temperature, humidity, precipitation, and 7 00:00:23,027 --> 00:00:25,594 more from locations all over the world. 8 00:00:25,594 --> 00:00:28,639 [SOUND] What a task. 9 00:00:28,639 --> 00:00:32,923 Fortunately for us, someone else has already tackled this task and 10 00:00:32,923 --> 00:00:36,242 made the information available through a service. 11 00:00:36,242 --> 00:00:39,757 Better yet, it's free for small apps, and 12 00:00:39,757 --> 00:00:44,127 if our app goes viral, the fee still isn't too large. 13 00:00:44,127 --> 00:00:51,614 We take another look at darksky.net, scroll back down to our Dark Sky API link. 14 00:00:51,614 --> 00:00:57,025 Scroll down just a little bit here, there's a learn more button. 15 00:00:57,025 --> 00:01:01,862 Here we're taken to a page that tells us about the API and how we can use it. 16 00:01:01,862 --> 00:01:04,903 What exactly is an API though? 17 00:01:04,903 --> 00:01:08,094 API stands for application programming interface. 18 00:01:08,094 --> 00:01:13,127 It's the interface we use to write code against all sorts of systems. 19 00:01:13,127 --> 00:01:16,607 We've actually been working with APIs all along. 20 00:01:16,607 --> 00:01:19,651 For example, in previous Android courses, 21 00:01:19,651 --> 00:01:24,626 we used Android APIs to interact with activities, views, and buttons. 22 00:01:24,626 --> 00:01:27,455 When we look at documentation for a class, 23 00:01:27,455 --> 00:01:30,846 we're looking at the Android API reference docs. 24 00:01:30,846 --> 00:01:35,887 An API is literally the interface used by our code to interact with the system. 25 00:01:35,887 --> 00:01:39,442 These interfaces can be a separate third party system, 26 00:01:39,442 --> 00:01:42,705 like Dark Sky, or smaller subsystems in Android. 27 00:01:42,705 --> 00:01:46,218 Such as the parts involved to determine our physical location. 28 00:01:46,218 --> 00:01:51,069 Although it may seem very different to us, using a third party API is very similar to 29 00:01:51,069 --> 00:01:54,485 using the Android APIs we're already comfortable with. 30 00:01:54,485 --> 00:01:59,196 Our interactions with a third party API will just be slightly different. 31 00:01:59,196 --> 00:02:04,529 We'll also need to discover what's available for us from the API. 32 00:02:04,529 --> 00:02:08,999 Before we jump into that though, let's take a few moments to talk about 33 00:02:08,999 --> 00:02:13,332 the concept of an interface in application programming interface. 34 00:02:13,332 --> 00:02:16,673 In different fields people use the term black box, 35 00:02:16,673 --> 00:02:21,503 to refer to a thing that does some work that we can't see from the outside. 36 00:02:21,503 --> 00:02:24,899 A black box, in this context, is simply something that we, 37 00:02:24,899 --> 00:02:28,110 as developers, don't need to concern ourselves with. 38 00:02:28,110 --> 00:02:31,118 Let's think outside the world of programming and 39 00:02:31,118 --> 00:02:32,771 use a bakery as an example. 40 00:02:32,771 --> 00:02:36,818 The bakery's sous chef is looking at a recipe for a particular item. 41 00:02:36,818 --> 00:02:40,325 She goes out to the market, purchases the ingredients, and 42 00:02:40,325 --> 00:02:42,331 delivers them back to the bakery. 43 00:02:42,331 --> 00:02:44,305 The bakery is our black box. 44 00:02:44,305 --> 00:02:48,370 As far as we're concerned, ingredients go into the bakery and 45 00:02:48,370 --> 00:02:50,681 come out as a delicious baked item. 46 00:02:50,681 --> 00:02:56,694 For example, our bakery gets an order for a specific cake, a gateau fraisier. 47 00:02:56,694 --> 00:03:01,674 If our sous chef brings eggs, flour, sugar, baking powder, cream, 48 00:03:01,674 --> 00:03:06,488 milk, vanilla, almond paste, and strawberries from the market, 49 00:03:06,488 --> 00:03:10,484 we can expect to get the classic french strawberry cake. 50 00:03:10,484 --> 00:03:14,863 We aren't necessarily concerned with how this particular bakery goes about 51 00:03:14,863 --> 00:03:16,626 producing the final product. 52 00:03:16,626 --> 00:03:21,059 We don't need to know their exact procedure for making the sponge cake, 53 00:03:21,059 --> 00:03:24,982 pastry cream, etc, as long as the output the gateau fraisier, 54 00:03:24,982 --> 00:03:28,563 is an agreed upon outcome by both the bakery and customer. 55 00:03:28,563 --> 00:03:32,856 Similarly, if this particular bakery goes out of business, we can replace 56 00:03:32,856 --> 00:03:37,027 this bakery with another one, and should expect to get the same product. 57 00:03:37,027 --> 00:03:40,847 This is similar in nature to how things are modeled with an API. 58 00:03:40,847 --> 00:03:44,090 We provide a service some inputs, our ingredients, 59 00:03:44,090 --> 00:03:46,475 to the API, the bakery in our example. 60 00:03:46,475 --> 00:03:50,528 Which does some work and provides us with some output, our cake. 61 00:03:50,528 --> 00:03:53,750 How does this help us as developers, though? 62 00:03:53,750 --> 00:03:56,153 Let's imagine that in our application, 63 00:03:56,153 --> 00:04:01,037 we have a method that handles getting us a strawberry cake called getGateauFraise. 64 00:04:01,037 --> 00:04:04,395 Whenever we want to get some cake, we call the method, 65 00:04:04,395 --> 00:04:06,594 which handles how we go about this. 66 00:04:06,594 --> 00:04:10,331 If we had been using Gabriel's Bakery for our cake, and then, for 67 00:04:10,331 --> 00:04:13,669 whatever reason, we want to change it to Louie's bakery, 68 00:04:13,669 --> 00:04:16,574 we can do that inside our getGateauFraise method. 69 00:04:16,574 --> 00:04:20,802 We can still call and use it the same way without changing our inputs, and 70 00:04:20,802 --> 00:04:22,481 still get the same outputs. 71 00:04:22,481 --> 00:04:24,780 Pretty convenient and powerful, right? 72 00:04:24,780 --> 00:04:29,081 This makes code and project maintenance, well, a piece of cake. 73 00:04:29,081 --> 00:04:31,895 Let's think of a truly different idea. 74 00:04:31,895 --> 00:04:36,409 We could change the getGateauFraise method to interface with a 24th-century food 75 00:04:36,409 --> 00:04:39,429 replicator, to get an instantaneous strawberry cake. 76 00:04:39,429 --> 00:04:41,957 Okay, that's a bit out of this world, but 77 00:04:41,957 --> 00:04:44,638 it shows the power of the interface concept. 78 00:04:44,638 --> 00:04:48,398 We don't need to change anything with our inputs and outputs, but 79 00:04:48,398 --> 00:04:51,769 the work being done inside the black box is very different. 80 00:04:51,769 --> 00:04:52,964 The best part? 81 00:04:52,964 --> 00:04:55,795 The rest of our code doesn't care. 82 00:04:55,795 --> 00:04:57,915 Okay, let's get back to reality a bit. 83 00:04:57,915 --> 00:05:02,163 Forget about our food replicator and, sadly, cake altogether, and 84 00:05:02,163 --> 00:05:04,540 get back to grabbing data from the web. 85 00:05:04,540 --> 00:05:09,819 In programming, this black box concept shows up in a lot of different ways. 86 00:05:09,819 --> 00:05:12,393 A well-designed system is programmed so 87 00:05:12,393 --> 00:05:16,297 that the interfaces between components are clearly defined. 88 00:05:16,297 --> 00:05:20,233 Each component then can look, and act, like a black box for 89 00:05:20,233 --> 00:05:22,294 the other parts of the system. 90 00:05:22,294 --> 00:05:27,488 let's change this diagram to reflect what we'll be building in stormy with Dark Sky. 91 00:05:27,488 --> 00:05:31,460 We'll utilize the Dark Sky API and pass in an API key, 92 00:05:31,460 --> 00:05:34,115 along with a location as our input. 93 00:05:34,115 --> 00:05:37,417 Our black box then will be a getForecast method, and 94 00:05:37,417 --> 00:05:40,870 that method will get the forecast from the internet and 95 00:05:40,870 --> 00:05:44,339 output the forecast data to use and display in our app. 96 00:05:44,339 --> 00:05:47,864 Actually, there are two important interfaces at work here. 97 00:05:47,864 --> 00:05:51,045 The first is the interface of the getForecast method, 98 00:05:51,045 --> 00:05:53,684 which includes the properties we'll need. 99 00:05:53,684 --> 00:05:58,645 The second interface, is actually between getForecast and the Dark Sky API, 100 00:05:58,645 --> 00:06:01,576 this is what's known as a web interface. 101 00:06:01,576 --> 00:06:06,503 One last thing to mention here, hopefully it provides some additional clarity. 102 00:06:06,503 --> 00:06:10,896 We also talk about the Android SDK, or software development kit. 103 00:06:10,896 --> 00:06:14,101 What's the difference between an SDK and an API? 104 00:06:14,101 --> 00:06:16,894 Well, an API is part of an SDK. 105 00:06:16,894 --> 00:06:20,047 Recall that a API is just the interface, 106 00:06:20,047 --> 00:06:24,887 the interface to the rest of the SDK where all the work is done. 107 00:06:24,887 --> 00:06:27,701 The API is the way in which you use something, and 108 00:06:27,701 --> 00:06:30,176 the SDK is where the work is accomplished. 109 00:06:30,176 --> 00:06:34,749 Sometimes we use an API alone without any access to the underlying 110 00:06:34,749 --> 00:06:36,423 components of the SDK. 111 00:06:36,423 --> 00:06:40,822 Which is what we're doing when we request data from the Dark Sky API. 112 00:06:40,822 --> 00:06:44,530 We simply make a request to their interface without needing anything else.