In the last video we nearly finished up our mailer.
There's one last thing we have to do here inside the constructor.
We're going to define one last property that represents the second grade object that we're going to
use to actually kind of communicate this mailer off to the grid API.
Now the signature of this thing is another point where I'm kind of like us and grid come on but hey
let's get to it.
We're going to say this dot send grid.
For short we're just going to say S-G like so API equals send grid.
And we're going to pass in our center key from the Keys object will say keys dot and grid key like so.
So you are just another location where you send grid in a little bit weird here so you can start the
entire Meilleur object and all the little helpers inside of here.
We made use of the helper property off of the send grid really the mail property I should say off the
grid library but then to actually take this mailer and send it off to send grid.
We use the send grid object or the grid function the grid library directly here.
So we take the grid function the center grid library we pass in our API key that returns an object that
we can use to communicate with the send grid API.
And so that's why we assign this property to S-G API or send grid API.
So we've got this API object here.
The very last thing we have to do is to put together a function that we all use to actually communicate
the entire Meilleur off to the grid API.
So we're going to define this thing at the very bottom of the class.
So go down towards the bottom and then right underneath our recipient's inside of here.
We're going to add add in the function called Send.
And as you might imagine when we call send the intent is that we take this mailer and send it off to
send ground to actually be e-mailed out to all of these different people.
Now anytime we talk about making an API request which is what we're doing right now or a network request
of any time remember we're always thinking that this is going to be some asynchronous code.
So rather than writing this all out with promises and whatnot we're just going to kind of beeline straight
to the real solution which is to use our async await syntax.
So I'm going to define the Send method.
And I'm going to immediately market as containing some asynchronous code by placing the async keyword
just like that.
Then inside of here we're going to create our Send grid API request and then send it off to send it.
So we're going to say Konst requests and yet this is another place where said grid is going to be just
a little bit weird.
So we're going to say S-G API.
Remember this is the API object that we just created.
Do make sure that you get the capitalization of API consistent with however you placed it up here in
the constructor.
So I used capital a lowercase API.
You will probably want to make sure that you've got the least consistent between the constructor and
down here as well.
Then we'll say datt empty request.
We're going to pass in some configuration options inside of an object.
So we're going to define a method of post.
We're going to define the path of slash v3 slash male slash send and then the body of the request which
will be this dot to Jason like so and so we saw this last step right here inside of our diagram just
a little bit ago.
We had said oh yeah it said all these different properties on the mailer called to Jaison to take all
those different properties and convert them to Jason data and then take that entire thing and send it
off to send it.
So that is essentially what we're doing right now.
One thing to keep in mind do make sure that you have capital Jason inside of here.
So this is another function that is defined by the male base class.
This is not something that you and I are going to define now after we create the request will then send
off the request by saying this dot S-G a P.I. dot a p I I pass in the request like so.
So again make sure that you've got the correct capitalization here consistent with all of your other
uses.
And then on the API object we're going to call call this API function which does have to be capitalized
because this is a function that is provided by the send grid API.
So this is what actually sends the thing off to grid.
And then the very last step we're going to get a response back is we'll say Konst response and then
we are going to return the response from the send function.
OK.
So that's just about it.
Man this has been nasty.
Josh this is not a pretty little thing.
I mean look at this grid API right.
Like make an empty request and then you have to call S-G dot API and pass the request why couldn't have
just been like this dot S-G API dot you know send and then hey here's the body Here's the stuff they
want to send off.
You know why did they have to make this nasty looking library you know nasty like an API here.
Why.
What does it have to al these have all these weird property names.
Again I apologize on behalf of second grade.
I know this cent. code has been pretty nasty and so if you want to do some follow up later on and look
at some other e-mail options out there that might have a better javascript library.
Certainly do so.
Again we really you send great here because they are one of the few providers that are completely free
to get started with.
Which definitely makes it perfect for our purposes.
OK.
So we're pretty much done at this point.
We're now ready to test out the mailer.
So in the next section we're going to figure out how we can effectively test all this logic out that
we've put together so far without actually having to wire up anything on our client side.
So it's going to be a little challenge in of itself.
So let's take a quick break.
We'll come back in the next section and we'll figure out exactly how we're going to test this stuff
out to make sure that we wired up everything correctly.
So I'll see you in just a minute.