1 00:00:00,380 --> 00:00:02,890 In the last video we nearly finished up our mailer. 2 00:00:02,910 --> 00:00:06,600 There's one last thing we have to do here inside the constructor. 3 00:00:06,600 --> 00:00:11,520 We're going to define one last property that represents the second grade object that we're going to 4 00:00:11,520 --> 00:00:16,080 use to actually kind of communicate this mailer off to the grid API. 5 00:00:16,440 --> 00:00:22,980 Now the signature of this thing is another point where I'm kind of like us and grid come on but hey 6 00:00:22,980 --> 00:00:23,800 let's get to it. 7 00:00:23,820 --> 00:00:27,550 We're going to say this dot send grid. 8 00:00:27,750 --> 00:00:35,250 For short we're just going to say S-G like so API equals send grid. 9 00:00:35,370 --> 00:00:45,030 And we're going to pass in our center key from the Keys object will say keys dot and grid key like so. 10 00:00:45,870 --> 00:00:51,510 So you are just another location where you send grid in a little bit weird here so you can start the 11 00:00:51,510 --> 00:00:54,970 entire Meilleur object and all the little helpers inside of here. 12 00:00:54,990 --> 00:01:00,720 We made use of the helper property off of the send grid really the mail property I should say off the 13 00:01:00,930 --> 00:01:05,720 grid library but then to actually take this mailer and send it off to send grid. 14 00:01:05,760 --> 00:01:11,120 We use the send grid object or the grid function the grid library directly here. 15 00:01:11,160 --> 00:01:17,550 So we take the grid function the center grid library we pass in our API key that returns an object that 16 00:01:17,550 --> 00:01:20,620 we can use to communicate with the send grid API. 17 00:01:20,700 --> 00:01:25,990 And so that's why we assign this property to S-G API or send grid API. 18 00:01:26,400 --> 00:01:29,010 So we've got this API object here. 19 00:01:29,130 --> 00:01:34,230 The very last thing we have to do is to put together a function that we all use to actually communicate 20 00:01:34,230 --> 00:01:36,990 the entire Meilleur off to the grid API. 21 00:01:37,170 --> 00:01:40,860 So we're going to define this thing at the very bottom of the class. 22 00:01:41,220 --> 00:01:45,590 So go down towards the bottom and then right underneath our recipient's inside of here. 23 00:01:45,990 --> 00:01:48,990 We're going to add add in the function called Send. 24 00:01:48,990 --> 00:01:54,600 And as you might imagine when we call send the intent is that we take this mailer and send it off to 25 00:01:54,600 --> 00:01:59,490 send ground to actually be e-mailed out to all of these different people. 26 00:01:59,630 --> 00:02:04,440 Now anytime we talk about making an API request which is what we're doing right now or a network request 27 00:02:04,440 --> 00:02:09,010 of any time remember we're always thinking that this is going to be some asynchronous code. 28 00:02:09,080 --> 00:02:14,340 So rather than writing this all out with promises and whatnot we're just going to kind of beeline straight 29 00:02:14,340 --> 00:02:18,320 to the real solution which is to use our async await syntax. 30 00:02:18,390 --> 00:02:21,280 So I'm going to define the Send method. 31 00:02:21,450 --> 00:02:27,540 And I'm going to immediately market as containing some asynchronous code by placing the async keyword 32 00:02:27,750 --> 00:02:29,730 just like that. 33 00:02:29,880 --> 00:02:36,210 Then inside of here we're going to create our Send grid API request and then send it off to send it. 34 00:02:36,270 --> 00:02:41,790 So we're going to say Konst requests and yet this is another place where said grid is going to be just 35 00:02:41,790 --> 00:02:42,430 a little bit weird. 36 00:02:42,420 --> 00:02:44,460 So we're going to say S-G API. 37 00:02:44,730 --> 00:02:48,240 Remember this is the API object that we just created. 38 00:02:48,240 --> 00:02:54,090 Do make sure that you get the capitalization of API consistent with however you placed it up here in 39 00:02:54,090 --> 00:02:55,260 the constructor. 40 00:02:55,260 --> 00:02:58,080 So I used capital a lowercase API. 41 00:02:58,170 --> 00:03:02,820 You will probably want to make sure that you've got the least consistent between the constructor and 42 00:03:02,820 --> 00:03:04,200 down here as well. 43 00:03:04,770 --> 00:03:10,320 Then we'll say datt empty request. 44 00:03:10,610 --> 00:03:15,200 We're going to pass in some configuration options inside of an object. 45 00:03:15,390 --> 00:03:20,060 So we're going to define a method of post. 46 00:03:20,070 --> 00:03:28,350 We're going to define the path of slash v3 slash male slash send and then the body of the request which 47 00:03:28,350 --> 00:03:36,900 will be this dot to Jason like so and so we saw this last step right here inside of our diagram just 48 00:03:36,900 --> 00:03:37,770 a little bit ago. 49 00:03:37,980 --> 00:03:43,710 We had said oh yeah it said all these different properties on the mailer called to Jaison to take all 50 00:03:43,710 --> 00:03:49,380 those different properties and convert them to Jason data and then take that entire thing and send it 51 00:03:49,380 --> 00:03:50,160 off to send it. 52 00:03:50,220 --> 00:03:53,420 So that is essentially what we're doing right now. 53 00:03:53,490 --> 00:03:57,370 One thing to keep in mind do make sure that you have capital Jason inside of here. 54 00:03:57,390 --> 00:04:00,840 So this is another function that is defined by the male base class. 55 00:04:00,840 --> 00:04:08,130 This is not something that you and I are going to define now after we create the request will then send 56 00:04:08,130 --> 00:04:19,330 off the request by saying this dot S-G a P.I. dot a p I I pass in the request like so. 57 00:04:19,380 --> 00:04:23,370 So again make sure that you've got the correct capitalization here consistent with all of your other 58 00:04:23,370 --> 00:04:24,360 uses. 59 00:04:24,360 --> 00:04:30,300 And then on the API object we're going to call call this API function which does have to be capitalized 60 00:04:30,600 --> 00:04:34,330 because this is a function that is provided by the send grid API. 61 00:04:34,920 --> 00:04:38,570 So this is what actually sends the thing off to grid. 62 00:04:39,180 --> 00:04:44,610 And then the very last step we're going to get a response back is we'll say Konst response and then 63 00:04:44,610 --> 00:04:49,300 we are going to return the response from the send function. 64 00:04:51,040 --> 00:04:51,470 OK. 65 00:04:51,550 --> 00:04:55,860 So that's just about it. 66 00:04:55,860 --> 00:04:57,860 Man this has been nasty. 67 00:04:57,860 --> 00:05:02,220 Josh this is not a pretty little thing. 68 00:05:02,290 --> 00:05:04,240 I mean look at this grid API right. 69 00:05:04,240 --> 00:05:10,390 Like make an empty request and then you have to call S-G dot API and pass the request why couldn't have 70 00:05:10,390 --> 00:05:17,350 just been like this dot S-G API dot you know send and then hey here's the body Here's the stuff they 71 00:05:17,350 --> 00:05:17,910 want to send off. 72 00:05:17,920 --> 00:05:23,830 You know why did they have to make this nasty looking library you know nasty like an API here. 73 00:05:23,830 --> 00:05:24,170 Why. 74 00:05:24,190 --> 00:05:27,360 What does it have to al these have all these weird property names. 75 00:05:27,430 --> 00:05:30,010 Again I apologize on behalf of second grade. 76 00:05:30,010 --> 00:05:35,350 I know this cent. code has been pretty nasty and so if you want to do some follow up later on and look 77 00:05:35,350 --> 00:05:38,770 at some other e-mail options out there that might have a better javascript library. 78 00:05:38,890 --> 00:05:39,980 Certainly do so. 79 00:05:40,120 --> 00:05:44,710 Again we really you send great here because they are one of the few providers that are completely free 80 00:05:44,710 --> 00:05:45,870 to get started with. 81 00:05:45,940 --> 00:05:49,040 Which definitely makes it perfect for our purposes. 82 00:05:49,550 --> 00:05:50,030 OK. 83 00:05:50,290 --> 00:05:51,940 So we're pretty much done at this point. 84 00:05:52,000 --> 00:05:54,190 We're now ready to test out the mailer. 85 00:05:54,280 --> 00:05:59,200 So in the next section we're going to figure out how we can effectively test all this logic out that 86 00:05:59,200 --> 00:06:04,210 we've put together so far without actually having to wire up anything on our client side. 87 00:06:04,210 --> 00:06:06,640 So it's going to be a little challenge in of itself. 88 00:06:06,640 --> 00:06:07,650 So let's take a quick break. 89 00:06:07,660 --> 00:06:11,560 We'll come back in the next section and we'll figure out exactly how we're going to test this stuff 90 00:06:11,560 --> 00:06:14,050 out to make sure that we wired up everything correctly. 91 00:06:14,260 --> 00:06:15,970 So I'll see you in just a minute.