1 00:00:00,530 --> 00:00:04,880 Now that we know how to use the web client class to retrieve data from a web page, 2 00:00:04,880 --> 00:00:08,190 we can use the Bing search API to retrieve data. 3 00:00:08,190 --> 00:00:11,410 We'll take our top ten players of the MLS and then search for 4 00:00:11,410 --> 00:00:13,070 news headlines about each player. 5 00:00:14,580 --> 00:00:18,010 Earlier we use the web client class with just a URL but 6 00:00:18,010 --> 00:00:21,590 this time we'll need to send our API key with our request. 7 00:00:21,590 --> 00:00:23,390 We'll do that with headers. 8 00:00:23,390 --> 00:00:25,900 You might have learned about headers with us before, 9 00:00:25,900 --> 00:00:28,980 they are extra pieces of information about a web request or 10 00:00:28,980 --> 00:00:32,220 response that includes additional information about the message. 11 00:00:33,760 --> 00:00:36,554 >> Let's take a look at the Bing News Search API documentation. 12 00:00:38,838 --> 00:00:40,480 Here, we've got a little preview tool. 13 00:00:41,640 --> 00:00:44,260 We can click on one of these examples. 14 00:00:44,260 --> 00:00:45,890 And it looks like we've got results to the right. 15 00:00:46,970 --> 00:00:48,050 Let's take a look at the JSON. 16 00:00:49,570 --> 00:00:51,690 We start out with a curly brace. 17 00:00:51,690 --> 00:00:55,530 That means that this entire JSON response is one object. 18 00:00:57,210 --> 00:01:01,490 And looks like the results are in this property named value. 19 00:01:01,490 --> 00:01:03,730 It's got an array, I can tell because of this bracket. 20 00:01:04,930 --> 00:01:07,890 Name, it looks like that's a headline. 21 00:01:07,890 --> 00:01:10,520 And description, that's the summary. 22 00:01:12,220 --> 00:01:14,710 Let's go back up and click on Documentation. 23 00:01:16,580 --> 00:01:19,930 Let's see, to get started read our Getting Started guide 24 00:01:19,930 --> 00:01:23,350 which describes how you can obtain your own subscription keys. 25 00:01:23,350 --> 00:01:26,730 I've already got my API key, so if you haven't already and 26 00:01:26,730 --> 00:01:30,530 wanna follow along make sure to pause the video now. 27 00:01:30,530 --> 00:01:34,530 Click on the Getting Started link right here and follow the instructions. 28 00:01:34,530 --> 00:01:38,870 Use of these APIs are free under a certain number of requests, so keep that in mind. 29 00:01:40,640 --> 00:01:44,472 For information that shows you how to use the API, see News Guide. 30 00:01:44,472 --> 00:01:48,670 All right, let's see we've got three URLs we can use. 31 00:01:48,670 --> 00:01:52,600 This second one here looks like the one we should use for 32 00:01:52,600 --> 00:01:54,290 searching on soccer player names. 33 00:01:55,440 --> 00:01:56,930 Let's see if we can find an example. 34 00:01:58,930 --> 00:02:01,406 Getting news articles that are relevant to a search query. 35 00:02:01,406 --> 00:02:05,002 All right. 36 00:02:05,002 --> 00:02:08,417 Get, here's what our URL should look like when we make the request. 37 00:02:08,417 --> 00:02:12,390 And here's what our API key header will look like. 38 00:02:12,390 --> 00:02:14,130 And we've got a sample of the response. 39 00:02:15,260 --> 00:02:17,470 I think this is all we need to get started. 40 00:02:17,470 --> 00:02:20,950 We'll need a class first so we can deserialize this data. 41 00:02:20,950 --> 00:02:23,135 We can copy this JSON response. 42 00:02:23,135 --> 00:02:29,088 And we'll use our paste JSON as classes trick we did earlier. 43 00:02:29,088 --> 00:02:31,250 We'll reference this page later for 44 00:02:31,250 --> 00:02:35,420 the URL in our API key once we get into using web client. 45 00:02:35,420 --> 00:02:43,500 Back in our code we'll click at class and let's name a new search. 46 00:02:46,090 --> 00:02:48,330 Okay, we've got a response in the clipboard. 47 00:02:48,330 --> 00:02:50,710 So I'll highlight this. 48 00:02:50,710 --> 00:02:51,930 Click Edit. 49 00:02:51,930 --> 00:02:52,790 Paste Special. 50 00:02:52,790 --> 00:02:54,214 Paste JSON as Classes. 51 00:02:56,301 --> 00:02:59,350 Back in the day, we used to have to type all this out by hand. 52 00:03:00,410 --> 00:03:03,300 Let's rename RootObject to NewSearch. 53 00:03:04,960 --> 00:03:07,260 Now we can go back to our program class and 54 00:03:07,260 --> 00:03:09,580 create a method to retrieve our new search results. 55 00:03:11,900 --> 00:03:17,136 Down here at the bottom we can copy and paste our Get Google home page method. 56 00:03:22,173 --> 00:03:25,957 We need to make a request for each one of our players, 57 00:03:25,957 --> 00:03:28,718 so let's call it get news for player. 58 00:03:28,718 --> 00:03:31,396 GetNewsForPlayer, and 59 00:03:31,396 --> 00:03:37,010 it'll take a string parameter called playerName. 60 00:03:38,280 --> 00:03:42,149 For now we'll focus on just getting the string result before we deserialize. 61 00:03:43,280 --> 00:03:46,428 Let's change the variable name to, searchResults. 62 00:03:49,003 --> 00:03:50,715 And change it down here, too. 63 00:03:53,103 --> 00:03:56,511 Now we need the URL, we'll go copy it from the page. 64 00:03:58,510 --> 00:04:02,181 All right this, right here and 65 00:04:04,559 --> 00:04:08,060 We'll need to replace the search parameter in the your URL, though. 66 00:04:08,060 --> 00:04:11,520 We can use a method on the string class called format. 67 00:04:11,520 --> 00:04:14,570 It returns a string and accepts additional parameters, 68 00:04:14,570 --> 00:04:17,170 that it inserts into the string it returns. 69 00:04:17,170 --> 00:04:18,710 Let's see how it works. 70 00:04:18,710 --> 00:04:23,250 Place Google with string.format. 71 00:04:25,810 --> 00:04:29,490 The first parameter is the composite string in which it will insert values. 72 00:04:29,490 --> 00:04:30,790 Which is our URL. 73 00:04:32,710 --> 00:04:34,342 Paste that there. 74 00:04:34,342 --> 00:04:41,261 Then we'll replace the text after Q equals with an indexed placeholder. 75 00:04:41,261 --> 00:04:45,682 Which is specified by curly braces and an index number. 76 00:04:45,682 --> 00:04:48,261 Curly brace zero, curly brace. 77 00:04:48,261 --> 00:04:53,039 Then we'll pass the player name as the second parameter into 78 00:04:53,039 --> 00:04:55,386 the string.format method. 79 00:04:55,386 --> 00:05:01,144 PlayerName, then we enclose the string.format method. 80 00:05:01,144 --> 00:05:06,028 The placeholder here is telling string.format to insert the value 81 00:05:06,028 --> 00:05:08,190 of the playerName variable. 82 00:05:08,190 --> 00:05:10,620 There are lots of things you can do with this method. 83 00:05:10,620 --> 00:05:13,720 It's especially useful for formatting date time values and 84 00:05:13,720 --> 00:05:17,540 decimal numbers into culture specific formatting. 85 00:05:17,540 --> 00:05:21,730 For more reading on this method, check out the documentation and the notes. 86 00:05:21,730 --> 00:05:24,370 Next we need to get our API key in the headers. 87 00:05:24,370 --> 00:05:25,750 You got your key ready? 88 00:05:25,750 --> 00:05:29,473 You won't be able to use the one I am about to use, because it will be inactive. 89 00:05:29,473 --> 00:05:35,173 First, we'll need to add our header, 90 00:05:35,173 --> 00:05:39,504 webClient.Headers.Add. 91 00:05:39,504 --> 00:05:42,630 This add method has a few overloads. 92 00:05:42,630 --> 00:05:46,670 One of them uses an enum that has some predefined headers that are often used. 93 00:05:48,210 --> 00:05:50,280 Except as a common one. 94 00:05:50,280 --> 00:05:53,010 It tells the endpoint what type of data we want. 95 00:05:53,010 --> 00:05:59,270 So if the service we're requesting can either send JSON or XML we'd specify JSON. 96 00:05:59,270 --> 00:06:02,720 In our case the default is JSON so we won't need it. 97 00:06:02,720 --> 00:06:05,460 The header we need to use though is a custom header and 98 00:06:05,460 --> 00:06:06,839 it won't be on this list. 99 00:06:09,013 --> 00:06:14,548 We need to use the last overload here which just takes two strings one for 100 00:06:14,548 --> 00:06:17,390 name and one for value. 101 00:06:17,390 --> 00:06:20,302 We'll go back to the documentation and grab the header name we need. 102 00:06:25,888 --> 00:06:32,030 All right, and we can paste that in here, then I'm gonna paste in my API key. 103 00:06:35,970 --> 00:06:39,150 Okay, that should be all we need to make this web request. 104 00:06:39,150 --> 00:06:40,018 Let's try it out in main. 105 00:06:44,212 --> 00:06:47,855 I'll replace this GetGoogleHomePage with our new method. 106 00:06:47,855 --> 00:06:55,450 GetNewsForPlayer and I'll pass it Diego Valari. 107 00:06:55,450 --> 00:06:58,080 And one more parenthesis. 108 00:06:58,080 --> 00:07:00,410 Okay, let's run with control F5. 109 00:07:03,620 --> 00:07:05,410 And we've got some data. 110 00:07:05,410 --> 00:07:10,204 Next step we'll need to deserialize these results into our new search class. 111 00:07:10,204 --> 00:07:11,581 [BLANK-AUDIO]