summaryrefslogtreecommitdiffstats
path: root/lib/Client.php
blob: da71d5d650af56377f6095dd2875196685341a93 (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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php

require 'resources/api_keys.php';
require 'resources/asm_groups.php';
require 'resources/asm_suppressions.php';
require 'resources/global_stats.php';

class Client
{
    const VERSION = '4.0.2';

    protected
        $namespace = 'SendGrid',
        $client,
        $options;

    public
        $apiKey,
        $url,
        $endpoint,
        $api_keys,
        $asm_groups,
        $asm_suppressions,
        $version = self::VERSION;

    public function __construct($apiKey, $options = array())
    {
        // Check if api key is present
        if (is_string($apiKey)) {
            $this->apiKey = $apiKey;
            $this->options = $options;
        } else {
            throw new InvalidArgumentException('Need an api key!');
        }

        $this->options['turn_off_ssl_verification'] = (isset($this->options['turn_off_ssl_verification']) && $this->options['turn_off_ssl_verification'] == true);
        if (!isset($this->options['raise_exceptions'])) {
            $this->options['raise_exceptions'] = true;
        }
        $protocol = isset($this->options['protocol']) ? $this->options['protocol'] : 'https';
        $host = isset($this->options['host']) ? $this->options['host'] : 'api.sendgrid.com';
        $port = isset($this->options['port']) ? $this->options['port'] : '';

        $this->url = isset($this->options['url']) ? $this->options['url'] : $protocol . '://' . $host . ($port ? ':' . $port : '');
        if (isset($this->options['endpoint'])) {
          $this->endpoint = $this->options['endpoint'];
        }
        $this->client = $this->prepareHttpClient();
        $this->api_keys = new APIKeys($this);
        $this->asm_groups = new ASMGroups($this);
        $this->asm_suppressions = new ASMSuppressions($this);
        $this->global_stats = new GlobalStats($this);
    }

    /**
     * Prepares the HTTP client
     *
     * @return \Guzzle\Http\Client
     */
    private function prepareHttpClient()
    {
        $guzzleOption = array(
            'request.options' => array(
                'verify' => !$this->options['turn_off_ssl_verification'],
                'exceptions' => (isset($this->options['enable_guzzle_exceptions']) && $this->options['enable_guzzle_exceptions'] == true)
            )
        );

        $guzzleOption['request.options']['headers'] = array('Authorization' => 'Bearer ' . $this->apiKey, 'Content-Type' => 'application/json', 'Accept'=> '*/*');

        // Using http proxy
        if (isset($this->options['proxy'])) {
            $guzzleOption['request.options']['proxy'] = $this->options['proxy'];
        }

        $client = new \Guzzle\Http\Client($this->url, $guzzleOption);
        $client->setUserAgent('sendgrid/' . $this->version . ';php');

        return $client;
    }
    
    public function setClient($client)
    {
      $this->client = $client;
    }

    /**
     * The following *Request functions make the HTTP API requests to SendGrid
     *
     * @param $api is an endpoint object defined in the resources 
     * @param $data is array of parameters
     *
     * @return Guzzle Response object: http://guzzle3.readthedocs.org/http-client/response.html
     */
    
    public function postRequest($api, $data)
    {
        $url = $this->url . $api->getEndpoint();
        $response = $this->client->post($url, null, json_encode($data))->send();
        return $response;
    }
  
    public function patchRequest($api, $data)
    {
        $url = $this->url . $api->getEndpoint();
        $response = $this->client->patch($url, null, json_encode($data))->send();
        return $response;
    }
    
    public function getRequest($api){
        $url = $this->url . $api->getEndpoint();
        $response = $this->client->get($url)->send();
        return $response;
    }

    public function deleteRequest($api){
        $url = $this->url . $api->getEndpoint();
        $response = $this->client->delete($url)->send();
        return $response;
    }

    public static function register_autoloader()
    {
        spl_autoload_register(array('Client', 'autoloader'));
    }

    public static function autoloader($class)
    {
        // Check that the class starts with 'Client'
        if ($class == 'Client' || stripos($class, 'Client\\') === 0) {
            $file = str_replace('\\', '/', $class);

            if (file_exists(dirname(__FILE__) . '/' . $file . '.php')) {
                require_once(dirname(__FILE__) . '/' . $file . '.php');
            }
        }
    }
}