summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/APIResource.php
blob: 6a1be7f219e6b5d678d61cb8f7c8f8c4da28c045 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
<?php
namespace SparkPost;
use Ivory\HttpAdapter\Configuration;
use Ivory\HttpAdapter\HttpAdapterInterface;
use Ivory\HttpAdapter\HttpAdapterException;

/**
 * @desc SDK interface for managing SparkPost API endpoints
 */
class APIResource {

	/**
	 * @desc name of the API endpoint, mainly used for URL construction.
   * This is public to provide an interface
   *
	 * @var string
	 */
	public $endpoint;

	/**
	 * @desc Mapping for values passed into the send method to the values needed for the respective API
	 * @var array
	 */
	protected static $parameterMappings = [];

	/**
	 * @desc Sets up default structure and default values for the model that is acceptable by the API
	 * @var array
	 */
	protected static $structure = [];

  /**
   * @dec connection config for making requests.
   */
  private $config;

  /**
   * @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
   */
  protected $httpAdapter;

  /**
   * @desc Default config values. Passed in values will override these.
   */
  private static $apiDefaults = [
		'host'=>'api.sparkpost.com',
		'protocol'=>'https',
		'port'=>443,
		'strictSSL'=>true,
		'key'=>'',
		'version'=>'v1'
	];

	/**
	 * @desc Initializes config and httpAdapter for use later.
   * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
   * @param $config connection config for making requests.
	 */
	public function __construct($httpAdapter, $config) {
    //config needs to be setup before adapter because of default adapter settings
    $this->setConfig($config);
    $this->setHttpAdapter($httpAdapter);
  }

  /**
	 * @desc Merges passed in headers with default headers for http requests
	 * @return Array - headers to be set on http requests
	 */
	private function getHttpHeaders(Array $headers = null) {
		$defaultOptions = [
			'Authorization' => $this->config['key'],
			'Content-Type' => 'application/json',
		];

		// Merge passed in headers with defaults
		if (!is_null($headers)) {
			foreach ($headers as $header => $value) {
				$defaultOptions[$header] = $value;
			}
		}
		return $defaultOptions;
	}


  /**
	 * @desc Helper function for getting the configuration for http requests
	 * @return \Ivory\HttpAdapter\Configuration
	 */
  private function getHttpConfig($config) {
		// get composer.json to extract version number
		$composerFile = file_get_contents(dirname(__FILE__) . "/../../composer.json");
		$composer = json_decode($composerFile, true);

		// create Configuration for http adapter
		$httpConfig = new Configuration();
		$baseUrl = $config['protocol'] . '://' . $config['host'] . ($config['port'] ? ':' . $config['port'] : '') . '/api/' . $config['version'];
		$httpConfig->setBaseUri($baseUrl);
		$httpConfig->setUserAgent('php-sparkpost/' . $composer['version']);
		return $httpConfig;
	}

  /**
   * @desc Validates and sets up the httpAdapter
   * @param $httpAdapter Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
   * @throws \Exception
   */
  public function setHttpAdapter($httpAdapter) {
  	if (!$httpAdapter instanceOf HttpAdapterInterface) {
			throw new \Exception('$httpAdapter paramter must be a valid Ivory\HttpAdapter');
		}

    $this->httpAdapter = $httpAdapter;
    $this->httpAdapter->setConfiguration($this->getHttpConfig($this->config));
  }

	/**
	 * Allows the user to pass in values to override the defaults and set their API key
	 * @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
	 * @throws \Exception
	 */
	public function setConfig(Array $settingsConfig) {
		// Validate API key because its required
    if (!isset($settingsConfig['key']) || empty(trim($settingsConfig['key']))){
			throw new \Exception('You must provide an API key');
    }

		$this->config = self::$apiDefaults;

    // set config, overriding defaults
		foreach ($settingsConfig as $configOption => $configValue) {
			if(key_exists($configOption, $this->config)) {
				$this->config[$configOption] = $configValue;
			}
		}
	}

	/**
	 * @desc Private Method helper to reference parameter mappings and set the right value for the right parameter
   *
   * @param array $model (pass by reference) the set of values to map
   * @param string $mapKey a dot syntax path determining which value to set
   * @param mixed $value value for the given path
	 */
	protected function setMappedValue (&$model, $mapKey, $value) {
		//get mapping
		if( empty(static::$parameterMappings) ) {
			// if parameterMappings is empty we can assume that no wrapper is defined
			// for the current endpoint and we will use the mapKey to define the mappings directly
			$mapPath = $mapKey;
		}elseif(array_key_exists($mapKey, static::$parameterMappings)) {
			// use only defined parameter mappings to construct $model
			$mapPath = static::$parameterMappings[$mapKey];
		} else {
			return;
		}

		$path = explode('.', $mapPath);
		$temp = &$model;
		foreach( $path as $key ) {
			if( !isset($temp[$key]) ){
				$temp[$key] = null;
			}
			$temp = &$temp[$key];
		}
		$temp = $value;

	}

  /**
   * @desc maps values from the passed in model to those needed for the request
   * @param $requestConfig the passed in model
   * @param $model the set of defaults
   * @return array A model ready for the body of a request
   */
	protected function buildRequestModel(Array $requestConfig, Array $model=[] ) {
		foreach($requestConfig as $key => $value) {
			$this->setMappedValue($model, $key, $value);
		}
		return $model;
	}

	/**
	 * @desc posts to the api with a supplied body
   * @param body post body for the request
   * @return array Result of the request
	 */
	public function create(Array $body=[]) {
		return $this->callResource( 'post', null, ['body'=>$body]);
	}

  /**
   * @desc Makes a put request to the api with a supplied body
   * @param body Put body for the request
   * @return array Result of the request
	 */
	public function update( $resourcePath, Array $body=[]) {
		return $this->callResource( 'put', $resourcePath, ['body'=>$body]);
	}

	/**
	 * @desc Wrapper method for issuing GET request to current API endpoint
	 *
	 * @param string $resourcePath (optional) string resource path of specific resource
	 * @param array $options (optional) query string parameters
	 * @return array Result of the request
	 */
	public function get( $resourcePath=null, Array $query=[] ) {
		return $this->callResource( 'get', $resourcePath, ['query'=>$query] );
	}

	/**
	 * @desc Wrapper method for issuing DELETE request to current API endpoint
	 *
	 * @param string $resourcePath (optional) string resource path of specific resource
	 * @param array $options (optional) query string parameters
	 * @return array Result of the request
	 */
	public function delete( $resourcePath=null, Array $query=[] ) {
		return $this->callResource( 'delete', $resourcePath, ['query'=>$query] );
	}


  /**
   * @desc assembles a URL for a request
   * @param string $resourcePath path after the initial endpoint
   * @param array options array with an optional value of query with values to build a querystring from.
   * @return string the assembled URL
   */
  private function buildUrl($resourcePath, $options) {
    $url = join(['/', $this->endpoint, '/']);
    if (!is_null($resourcePath)){
      $url .= $resourcePath;
    }

    if( !empty($options['query'])) {
      $queryString = http_build_query($options['query']);
      $url .= '?'.$queryString;
    }

    return $url;
  }


  /**
   * @desc Prepares a body for put and post requests
   * @param array options array with an optional value of body with values to build a request body from.
   * @return string|null A json encoded string or null if no body was provided
   */
  private function buildBody($options) {
    $body = null;
    if( !empty($options['body']) ) {
			$model = static::$structure;
			$requestModel = $this->buildRequestModel( $options['body'], $model );
			$body = json_encode($requestModel);
		}
    return $body;
  }


	/**
	 * @desc Private Method for issuing GET and DELETE request to current API endpoint
	 *
	 *  This method is responsible for getting the collection _and_
	 *  a specific entity from the API endpoint
	 *
	 *  If resourcePath parameter is omitted, then we fetch the collection
	 *
	 * @param string $action HTTP method type
	 * @param string $resourcePath (optional) string resource path of specific resource
	 * @param array $options (optional) query string parameters
	 * @return array Result set of action performed on resource
	 */
	private function callResource( $action, $resourcePath=null, $options=[] ) {
		$action = strtoupper($action); // normalize

		if( !in_array($action, ['POST', 'PUT', 'GET', 'DELETE'])) {
			throw new \Exception('Invalid resource action');
		}

		$url = $this->buildUrl($resourcePath, $options);
		$body = $this->buildBody($options);

		//make request
		try {
			$response = $this->httpAdapter->send($url, $action, $this->getHttpHeaders(), $body);
			return json_decode($response->getBody()->getContents(), true);
		}
		/*
		 * Handles 4XX responses
     */
    catch (HttpAdapterException $exception) {
    	$response = $exception->getResponse();
    	$statusCode = $response->getStatusCode();
			if($statusCode === 404) {
				throw new \Exception("The specified resource does not exist", 404);
			}
			throw new \Exception("Received bad response from ".ucfirst($this->endpoint)." API: ". $statusCode );
		}
		/*
		 * Handles 5XX Errors, Configuration Errors, and a catch all for other errors
		 */
		catch (\Exception $exception) {
			throw new \Exception("Unable to contact ".ucfirst($this->endpoint)." API: ". $exception->getMessage());
		}
	}

}