summaryrefslogtreecommitdiffstats
path: root/lib/SparkPost/APIResource.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/SparkPost/APIResource.php')
-rw-r--r--lib/SparkPost/APIResource.php104
1 files changed, 47 insertions, 57 deletions
diff --git a/lib/SparkPost/APIResource.php b/lib/SparkPost/APIResource.php
index 0677b38..d02b91e 100644
--- a/lib/SparkPost/APIResource.php
+++ b/lib/SparkPost/APIResource.php
@@ -4,12 +4,12 @@ namespace SparkPost;
/**
* SDK interface for managing SparkPost API endpoints
*/
-class APIResource
-{
+class APIResource {
/**
- * Name of the API endpoint, mainly used for URL construction.
+ * name of the API endpoint, mainly used for URL construction.
* This is public to provide an interface
+ *
* @var string
*/
public $endpoint;
@@ -33,11 +33,9 @@ class APIResource
/**
* Initializes config and httpAdapter for use later.
- *
* @param $sparkpost \SparkPost\SparkPost provides api configuration information
*/
- public function __construct(SparkPost $sparkpost)
- {
+ public function __construct(SparkPost $sparkpost) {
$this->sparkpost = $sparkpost;
}
@@ -48,14 +46,13 @@ class APIResource
* @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)) {
+ 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)) {
+ }elseif(array_key_exists($mapKey, static::$parameterMappings)) {
// use only defined parameter mappings to construct $model
$mapPath = static::$parameterMappings[$mapKey];
} else {
@@ -64,52 +61,47 @@ class APIResource
$path = explode('.', $mapPath);
$temp = &$model;
- foreach ($path as $key) {
- if (!isset($temp[$key])) {
+ foreach( $path as $key ) {
+ if( !isset($temp[$key]) ){
$temp[$key] = null;
}
$temp = &$temp[$key];
}
$temp = $value;
+
}
/**
- * Maps values from the passed in model to those needed for the request
- *
+ * maps values from the passed in model to those needed for the request
* @param array $requestConfig the passed in model
* @param array $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) {
+ protected function buildRequestModel(Array $requestConfig, Array $model=[] ) {
+ foreach($requestConfig as $key => $value) {
$this->setMappedValue($model, $key, $value);
}
-
return $model;
}
/**
- * Posts to the api with a supplied body
- *
+ * posts to the api with a supplied body
* @param array $body post body for the request
* @return array Result of the request
*/
- public function create(array $body = [])
- {
- return $this->callResource('post', null, ['body' => $body]);
+ public function create(Array $body=[]) {
+ return $this->callResource( 'post', null, ['body'=>$body]);
}
/**
* Makes a put request to the api with a supplied body
- *
- * @param string $resourcePath string resource path of specific resource
+ * @param $resourcePath
* @param array $body Put body for the request
* @return array Result of the request
+ * @throws APIResponseException
*/
- public function update($resourcePath, array $body = [])
- {
- return $this->callResource('put', $resourcePath, ['body' => $body]);
+ public function update( $resourcePath, Array $body=[]) {
+ return $this->callResource( 'put', $resourcePath, ['body'=>$body]);
}
/**
@@ -119,9 +111,8 @@ class APIResource
* @param array $query (optional) query string parameters
* @return array Result of the request
*/
- public function get($resourcePath = null, array $query = [])
- {
- return $this->callResource('get', $resourcePath, ['query' => $query]);
+ public function get( $resourcePath=null, Array $query=[] ) {
+ return $this->callResource( 'get', $resourcePath, ['query'=>$query] );
}
/**
@@ -131,48 +122,44 @@ class APIResource
* @param array $query (optional) query string parameters
* @return array Result of the request
*/
- public function delete($resourcePath = null, array $query = [])
- {
- return $this->callResource('delete', $resourcePath, ['query' => $query]);
+ public function delete( $resourcePath=null, Array $query=[] ) {
+ return $this->callResource( 'delete', $resourcePath, ['query'=>$query] );
}
+
/**
- * Assembles a URL for a request
- *
+ * 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)
- {
+ private function buildUrl($resourcePath, $options) {
$url = "/{$this->endpoint}/";
- if (!is_null($resourcePath)) {
+ if (!is_null($resourcePath)){
$url .= $resourcePath;
}
- if (!empty($options['query'])) {
+ if( !empty($options['query'])) {
$queryString = http_build_query($options['query']);
- $url .= '?' . $queryString;
+ $url .= '?'.$queryString;
}
return $url;
}
+
/**
* 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)
- {
+ private function buildBody($options) {
$body = null;
- if (!empty($options['body'])) {
+ if( !empty($options['body']) ) {
$model = static::$structure;
- $requestModel = $this->buildRequestModel($options['body'], $model);
+ $requestModel = $this->buildRequestModel( $options['body'], $model );
$body = json_encode($requestModel);
}
-
return $body;
}
@@ -190,14 +177,13 @@ class APIResource
* @return array Result set of action performed on resource
* @throws APIResponseException
*/
- private function callResource($action, $resourcePath = null, $options = [])
- {
+ private function callResource( $action, $resourcePath=null, $options=[] ) {
$action = strtoupper($action); // normalize
$url = $this->buildUrl($resourcePath, $options);
$body = $this->buildBody($options);
- // make request
+ //make request
try {
$response = $this->sparkpost->httpAdapter->send($url, $action, $this->sparkpost->getHttpHeaders(), $body);
@@ -210,16 +196,20 @@ class APIResource
if ($statusCode === 404) {
throw new APIResponseException('The specified resource does not exist', 404);
}
- throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: ' .
- $statusCode);
+ throw new APIResponseException('Received bad response from ' . ucfirst($this->endpoint) . ' API: '. $statusCode );
}
- } catch (\Exception $exception) { // Configuration Errors, and a catch all for other errors
- if ($exception instanceof APIResponseException) {
+ }
+
+ /*
+ * Configuration Errors, and a catch all for other errors
+ */
+ catch (\Exception $exception) {
+ if($exception instanceof APIResponseException) {
throw $exception;
}
- throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: ' .
- $exception->getMessage());
+ throw new APIResponseException('Unable to contact ' . ucfirst($this->endpoint) . ' API: '. $exception->getMessage());
}
}
+
}