summaryrefslogtreecommitdiffstats
path: root/lib/sendgrid.js
blob: 13d783d127cf27bbd2c4c1e2d75162d57cfb8fa9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
"use strict";
var package_json = require('./../package.json');
var emptyRequest = JSON.parse(JSON.stringify(require('sendgrid-rest').emptyRequest));

// SendGrid allows for quick and easy access to the v3 Web API
function SendGrid (apiKey, host, globalHeaders) {
  var Client = require('sendgrid-rest').Client
  var globalRequest = JSON.parse(JSON.stringify(require('sendgrid-rest').emptyRequest));
  globalRequest.host = host || "api.sendgrid.com";
  globalRequest.headers['Authorization'] = 'Bearer '.concat(apiKey)
  globalRequest.headers['User-Agent'] = 'sendgrid/' + package_json.version + ';nodejs'
  globalRequest.headers['Accept'] = 'application/json'
  if (globalHeaders) {
    for (var obj in globalHeaders) {
      for (var key in globalHeaders[obj] ) {
        globalRequest.headers[key] = globalHeaders[obj][key]
      }
    }
  }
  var client = new Client(globalRequest)

  this.emptyRequest = function () {
    return JSON.parse(JSON.stringify(require('sendgrid-rest').emptyRequest));
  }

  // Interact with the API with this function
  this.API = function(request, callback) {
    client.API(request, function (response) {
      callback(response)
    })
  };

  this.globalRequest = globalRequest
  return this;
};

module.exports =
{
  SendGrid: SendGrid,
  emptyRequest: emptyRequest
}