summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--examples/unwrapped/create_template.php6
-rw-r--r--lib/SparkPost/APIResource.php108
-rw-r--r--lib/SparkPost/SparkPost.php119
3 files changed, 126 insertions, 107 deletions
diff --git a/examples/unwrapped/create_template.php b/examples/unwrapped/create_template.php
index b092340..c0e52b7 100644
--- a/examples/unwrapped/create_template.php
+++ b/examples/unwrapped/create_template.php
@@ -6,11 +6,11 @@ use Ivory\HttpAdapter\Guzzle6HttpAdapter;
$key = 'YOUR API KEY';
$httpAdapter = new Guzzle6HttpAdapter(new Client());
-$resource = new \SparkPost\APIResource($httpAdapter, ['key'=>$key]);
+$sparky = new SparkPost($httpAdapter, ['key'=>$key]);
try {
// define the endpoint
- $resource->endpoint = 'templates';
+ $sparky->setupUnwrapped('templates');
$templateConfig = [
'name' => 'Summer Sale!',
@@ -21,7 +21,7 @@ try {
'html' => '<b>Check out these deals!</b>'
]
];
- $results = $resource->create($templateConfig);
+ $results = $sparky->templates->create($templateConfig);
echo 'Congrats you can use your SDK!';
} catch (\Exception $exception) {
echo $exception->getMessage();
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index 6a1be7f..7ec958b 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -1,8 +1,7 @@
<?php
namespace SparkPost;
-use Ivory\HttpAdapter\Configuration;
-use Ivory\HttpAdapter\HttpAdapterInterface;
use Ivory\HttpAdapter\HttpAdapterException;
+use SparkPost\SparkPost;
/**
* @desc SDK interface for managing SparkPost API endpoints
@@ -29,111 +28,14 @@ class APIResource {
*/
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.
+ * @param $sparkpost SparkPost\SparkPost provides api configuration information
*/
- public function __construct($httpAdapter, $config) {
- //config needs to be setup before adapter because of default adapter settings
- $this->setConfig($config);
- $this->setHttpAdapter($httpAdapter);
+ public function __construct(SparkPost $sparkpost) {
+ $this->sparkpost = $sparkpost;
}
- /**
- * @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
*
@@ -282,7 +184,7 @@ class APIResource {
//make request
try {
- $response = $this->httpAdapter->send($url, $action, $this->getHttpHeaders(), $body);
+ $response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
return json_decode($response->getBody()->getContents(), true);
}
/*
diff --git a/lib/SparkPost/SparkPost.php b/lib/SparkPost/SparkPost.php
index e64ff9f..aac352b 100644
--- a/lib/SparkPost/SparkPost.php
+++ b/lib/SparkPost/SparkPost.php
@@ -1,10 +1,35 @@
<?php
namespace SparkPost;
+use Ivory\HttpAdapter\Configuration;
+use Ivory\HttpAdapter\HttpAdapterInterface;
+
class SparkPost {
public $transmission;
/**
+ * @dec connection config for making requests.
+ */
+ private $config;
+
+ /**
+ * @desc Ivory\HttpAdapter\HttpAdapterInterface to make requests through.
+ */
+ public $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 sets up httpAdapter and config
*
* Sets up instances of sub libraries.
@@ -13,7 +38,99 @@ class SparkPost {
* @param Array $settingsConfig - Hashmap that contains config values for the SDK to connect to SparkPost
*/
public function __construct($httpAdapter, $settingsConfig) {
- $this->transmission = new Transmission($httpAdapter, $settingsConfig);
+ //config needs to be setup before adapter because of default adapter settings
+ $this->setConfig($settingsConfig);
+ $this->setHttpAdapter($httpAdapter);
+
+ $this->transmission = new Transmission($this);
+ }
+
+
+
+ /**
+ * Creates an unwrapped api interface for endpoints that aren't yet supported.
+ * The new resource is attached to this object as well as returned
+ * @return SparkPost\APIResource - the unwrapped resource
+ */
+ public function setupUnwrapped (string $endpoint) {
+ $this->{$endpoint} = new APIResource($this);
+ $this->{$endpoint}->endpoint = $endpoint;
+
+ return $this->{$endpoint};
+ }
+
+ /**
+ * @desc Merges passed in headers with default headers for http requests
+ * @return Array - headers to be set on http requests
+ */
+ public 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;
+ }
+ }
}
}