1 00:00:00,510 --> 00:00:03,880 We now know how to receive data from a web resource. 2 00:00:03,880 --> 00:00:07,760 Let's learn about how to send data to a web resource. 3 00:00:07,760 --> 00:00:12,090 We'll be using another Microsoft API called text analytics. 4 00:00:12,090 --> 00:00:14,190 I've got a demo up of it here. 5 00:00:14,190 --> 00:00:19,430 This web service analyzes a piece of text for sentiment, topics and language. 6 00:00:19,430 --> 00:00:21,148 All we do is send it some text. 7 00:00:21,148 --> 00:00:28,700 I love C#. 8 00:00:28,700 --> 00:00:32,250 And it comes back with analysis of the text we sent. 9 00:00:32,250 --> 00:00:35,031 It says our sentence is 82% positive. 10 00:00:35,031 --> 00:00:36,890 I'd agree with that. 11 00:00:36,890 --> 00:00:40,199 Let's see what it says If I type in something a little less positive. 12 00:00:41,730 --> 00:00:44,300 I hate lima beans. 13 00:00:46,890 --> 00:00:53,127 Analyze, it detected our text was much less positive 10%. 14 00:00:53,127 --> 00:00:57,760 We can use this service to analyze the sentiment in each of our news headlines. 15 00:00:58,800 --> 00:01:00,850 Let's click on getting started. 16 00:01:02,980 --> 00:01:07,580 Let's see to follow along you need to follow the steps and 17 00:01:07,580 --> 00:01:10,650 task one here in order to get an API key. 18 00:01:10,650 --> 00:01:14,210 It's a different key from the one we used earlier for a new search. 19 00:01:14,210 --> 00:01:17,249 Feel free to posit video and get your API key ready. 20 00:01:19,785 --> 00:01:25,960 Okay here's task two, says we'll need to use these headers. 21 00:01:25,960 --> 00:01:27,090 We'll get back to that in a bit. 22 00:01:29,230 --> 00:01:32,200 Let's see, we're going to need two new classes. 23 00:01:32,200 --> 00:01:34,900 One for our request data and one for response data. 24 00:01:35,980 --> 00:01:39,180 Here, it's telling us how to format our request. 25 00:01:39,180 --> 00:01:42,230 Let's copy and paste this into a new class in our application. 26 00:01:44,420 --> 00:01:50,212 Add class, we'll call it, SentimentRequest. 27 00:01:54,249 --> 00:02:01,520 Then, edit, Paste Special, Paste JSON As Classes. 28 00:02:01,520 --> 00:02:03,820 No, it didn't like that. 29 00:02:05,680 --> 00:02:08,630 This sample has some invalid JSON, 30 00:02:08,630 --> 00:02:13,050 we can use a JSON validation service to try and figure out what's wrong. 31 00:02:13,050 --> 00:02:18,110 Head to google and JSON validation 32 00:02:18,110 --> 00:02:21,880 service, we'll use this one JSONLint. 33 00:02:23,700 --> 00:02:26,940 Let's paste our JSON here and then click validate. 34 00:02:29,810 --> 00:02:31,690 It's got line five highlighted, but 35 00:02:31,690 --> 00:02:35,260 I think the problem is these ellipses right here. 36 00:02:35,260 --> 00:02:37,910 Let's delete that and try again. 37 00:02:37,910 --> 00:02:41,010 Validate, all right, valid JSON. 38 00:02:42,110 --> 00:02:45,935 Let's copy this and go back over and try to paste it into our class again. 39 00:02:45,935 --> 00:02:52,130 Edit, Paste Special, Paste JSON as Classes. 40 00:02:52,130 --> 00:02:57,137 All right, let's rename Rootobject to SentimentRequest and 41 00:02:57,137 --> 00:03:05,987 we'll change this array to a list, List documents and 42 00:03:05,987 --> 00:03:11,268 I'd like to change the properties to capitals. 43 00:03:15,208 --> 00:03:17,468 And we'll need our JsonProperty again. 44 00:03:20,725 --> 00:03:27,031 JsonProperty all right, 45 00:03:27,031 --> 00:03:33,920 PropertyName=document. 46 00:03:33,920 --> 00:03:38,636 Copy that, that's an id, 47 00:03:38,636 --> 00:03:42,950 and then our text field. 48 00:03:46,250 --> 00:03:50,030 Okay, now we need a class for our response data. 49 00:03:50,030 --> 00:03:52,294 We can go back and get the response JSON. 50 00:03:55,788 --> 00:03:58,020 Here it is, sentiment response. 51 00:03:59,900 --> 00:04:03,090 Copy and paste that, it looks like we've got our ellipses again. 52 00:04:03,090 --> 00:04:09,175 Let's use JSONLint, take out this ellipsis and validate. 53 00:04:09,175 --> 00:04:16,310 Uh-oh, think we've got an extra comma here, good thing we checked. 54 00:04:16,310 --> 00:04:19,180 Validate and valid Json. 55 00:04:20,550 --> 00:04:25,146 So we'll do the same thing back in our code, 56 00:04:25,146 --> 00:04:28,500 add another class, add class, 57 00:04:28,500 --> 00:04:33,979 then this is going to be called SentimentResponse. 58 00:04:38,958 --> 00:04:42,835 And one more time Paste Special, paste JSON as classes. 59 00:04:42,835 --> 00:04:43,680 All right, great. 60 00:04:45,290 --> 00:04:50,452 Rename the root object to SentimentResponse 61 00:04:50,452 --> 00:04:53,585 change our array to a list. 62 00:04:57,677 --> 00:05:03,010 And, we've got a little red squiggly line right here. 63 00:05:03,010 --> 00:05:06,300 I think it's because we've got two document classes defined, and 64 00:05:06,300 --> 00:05:08,060 they're conflicting. 65 00:05:08,060 --> 00:05:10,300 Well there are a lot of things we could do to fix this, 66 00:05:10,300 --> 00:05:13,919 but the simplest I can think of is to rename one of these classes. 67 00:05:15,120 --> 00:05:19,280 Well the response is giving us an ID and a score. 68 00:05:19,280 --> 00:05:21,690 That's not really a document is it? 69 00:05:21,690 --> 00:05:25,590 Let's rename this to sentiment and 70 00:05:25,590 --> 00:05:30,740 then we'll need to change the list up here and the name to sentiments. 71 00:05:30,740 --> 00:05:33,660 Then we'll need our JSON property. 72 00:05:33,660 --> 00:05:38,395 JSON property, you'll be an expert at the quick actions by now. 73 00:05:38,395 --> 00:05:44,833 And property name equals documents, 74 00:05:44,833 --> 00:05:50,263 and we'll capitalize our ID and 75 00:05:50,263 --> 00:05:53,290 score down here. 76 00:05:53,290 --> 00:05:56,510 ID capital I, and 77 00:05:56,510 --> 00:06:02,535 one more time for score, Score. 78 00:06:02,535 --> 00:06:08,450 Okay, that looks good. 79 00:06:08,450 --> 00:06:11,890 Now we can get our method set up to make the request to the web service. 80 00:06:13,140 --> 00:06:17,770 Since the request can have more than one piece to analyze, we can give the method 81 00:06:17,770 --> 00:06:21,330 a list of news results and then have it return the sentiment response. 82 00:06:22,650 --> 00:06:29,770 All the way down here, public static SentimentResponse, 83 00:06:31,040 --> 00:06:34,910 and we'll call it GetSentimentResponse. 84 00:06:36,010 --> 00:06:39,946 It'll take a list of NewsResults, 85 00:06:39,946 --> 00:06:44,018 and we'll call those newsResults. 86 00:06:46,985 --> 00:06:54,008 And we'll go ahead and var SentimentResponse= 87 00:06:54,008 --> 00:06:59,660 new SentimentResponse and return it, 88 00:06:59,660 --> 00:07:04,290 so that visual studio is happy. 89 00:07:04,290 --> 00:07:10,329 Okay, now we'll also need a SentimentRequest, 90 00:07:10,329 --> 00:07:15,950 SentimentRequest = new SentimentRequest. 91 00:07:19,190 --> 00:07:23,160 Okay but we need to add data to this SentimentRequest object. 92 00:07:24,460 --> 00:07:27,710 We can use a foreach loop to iterate through our news results and 93 00:07:27,710 --> 00:07:29,776 add data to our SentimentRequest object. 94 00:07:29,776 --> 00:07:34,961 foreach(var result in 95 00:07:34,961 --> 00:07:39,920 newsResults), and then inside the foreach loop, 96 00:07:39,920 --> 00:07:44,205 we'll add a new document to the list that contains the text from each news result. 97 00:07:44,205 --> 00:07:51,530 sentimentRequest.Documents.Add,we can 98 00:07:51,530 --> 00:07:55,220 use object initialization here to create an instance of a document 99 00:07:55,220 --> 00:07:57,450 as we add it to the list. 100 00:07:57,450 --> 00:08:02,130 New document, in a curly brace. 101 00:08:02,130 --> 00:08:04,365 I can use a space here to get it. 102 00:08:04,365 --> 00:08:10,425 Okay, we've got ID equals, we'll l use the headline as the ID, 103 00:08:10,425 --> 00:08:17,690 result.Headline, and then Text equals, 104 00:08:17,690 --> 00:08:22,450 it should be the summary, result.Summary. 105 00:08:22,450 --> 00:08:24,730 There's one thing we need to do, 106 00:08:24,730 --> 00:08:29,770 we need to instantiate this Documents to a new list of documents. 107 00:08:29,770 --> 00:08:36,570 So sentimentRequest.Documents = new list of document. 108 00:08:38,080 --> 00:08:39,470 If we didn't do this and 109 00:08:39,470 --> 00:08:43,420 tried to add a document to the list, we'd end up with a runtime error. 110 00:08:44,550 --> 00:08:46,680 Now we can create our web client. 111 00:08:46,680 --> 00:08:49,506 We can copy and paste from our news requests to start 112 00:09:01,187 --> 00:09:04,830 But this download data isn't the method we're going to use. 113 00:09:05,960 --> 00:09:07,600 We need to send data. 114 00:09:07,600 --> 00:09:15,310 There's a different method on web client called upload data .UploadData, 115 00:09:15,310 --> 00:09:18,930 we can see that it wants a byte array in addition to the url. 116 00:09:18,930 --> 00:09:20,990 I think we can handle that. 117 00:09:20,990 --> 00:09:25,730 We've already got our API key header but we'll need to insert our new API key. 118 00:09:27,380 --> 00:09:33,760 Make sure to use your own, and there was some additional headers it wanted. 119 00:09:34,830 --> 00:09:36,350 Let's go back to the documentation. 120 00:09:36,350 --> 00:09:41,130 I think it was up here, there they are. 121 00:09:42,380 --> 00:09:45,750 We need two more content type and accept. 122 00:09:45,750 --> 00:09:49,050 We saw accept earlier on those standard headers. 123 00:09:49,050 --> 00:09:53,923 We'll copy this value since it's the same for both, head back over the code. 124 00:09:56,698 --> 00:10:04,314 Okay, webClient.Headers.Add. 125 00:10:04,314 --> 00:10:09,420 And we'll use this enum here, Accept and 126 00:10:09,420 --> 00:10:14,781 we'll pass in the value which is application/JSON. 127 00:10:14,781 --> 00:10:18,219 And web client.headers.Add, 128 00:10:18,219 --> 00:10:23,251 one more content type there it is at the bottom and 129 00:10:23,251 --> 00:10:26,823 we'll pass application/JSON. 130 00:10:26,823 --> 00:10:33,990 We'll go ahead and change this search results to response. 131 00:10:33,990 --> 00:10:36,050 We're almost ready to make this request.