Added php docblock comments and changed constants to use newly created constant classes

This commit is contained in:
Michael Crawford 2014-11-20 12:12:06 -06:00
parent b34805d86b
commit e1e2ed796f

View File

@ -3,35 +3,62 @@
namespace Mailgun\Connection; namespace Mailgun\Connection;
use Guzzle\Http\Client as Guzzle; use Guzzle\Http\Client as Guzzle;
use Mailgun\MailgunClient;
use Mailgun\Connection\Exceptions\GenericHTTPError; use Mailgun\Connection\Exceptions\GenericHTTPError;
use Guzzle\Http\QueryAggregator\DuplicateAggregator; use Guzzle\Http\QueryAggregator\DuplicateAggregator;
use Guzzle\Http\QueryAggregator\PhpAggregator; use Guzzle\Http\QueryAggregator\PhpAggregator;
use Mailgun\Connection\Exceptions\InvalidCredentials; use Mailgun\Connection\Exceptions\InvalidCredentials;
use Mailgun\Connection\Exceptions\NoDomainsConfigured;
use Mailgun\Connection\Exceptions\MissingRequiredParameters; use Mailgun\Connection\Exceptions\MissingRequiredParameters;
use Mailgun\Connection\Exceptions\MissingEndpoint; use Mailgun\Connection\Exceptions\MissingEndpoint;
use Mailgun\Constants\Api;
use Mailgun\Constants\ExceptionMessages;
/* /*
This class is a wrapper for the Guzzle (HTTP Client Library). This class is a wrapper for the Guzzle (HTTP Client Library).
*/ */
class RestClient{ class RestClient {
/**
* @var string
*/
private $apiKey; private $apiKey;
/**
* @var Guzzle
*/
protected $mgClient; protected $mgClient;
/**
* @var bool
*/
protected $hasFiles = False; protected $hasFiles = False;
/**
* @param string $apiKey
* @param string $apiEndpoint
* @param string $apiVersion
* @param bool $ssl
*/
public function __construct($apiKey, $apiEndpoint, $apiVersion, $ssl){ public function __construct($apiKey, $apiEndpoint, $apiVersion, $ssl){
$this->apiKey = $apiKey; $this->apiKey = $apiKey;
$this->mgClient = new Guzzle($this->generateEndpoint($apiEndpoint, $apiVersion, $ssl)); $this->mgClient = new Guzzle($this->generateEndpoint($apiEndpoint, $apiVersion, $ssl));
$this->mgClient->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true)); $this->mgClient->setDefaultOption('curl.options', array('CURLOPT_FORBID_REUSE' => true));
$this->mgClient->setDefaultOption('auth', array (API_USER, $this->apiKey)); $this->mgClient->setDefaultOption('auth', array (Api::API_USER, $this->apiKey));
$this->mgClient->setDefaultOption('exceptions', false); $this->mgClient->setDefaultOption('exceptions', false);
$this->mgClient->setUserAgent(SDK_USER_AGENT . '/' . SDK_VERSION); $this->mgClient->setUserAgent(Api::SDK_USER_AGENT . '/' . Api::SDK_VERSION);
} }
/**
* @param string $endpointUrl
* @param array $postData
* @param array $files
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function post($endpointUrl, $postData = array(), $files = array()){ public function post($endpointUrl, $postData = array(), $files = array()){
$request = $this->mgClient->post($endpointUrl, array(), $postData); $request = $this->mgClient->post($endpointUrl, array(), $postData);
@ -90,6 +117,15 @@ class RestClient{
return $this->responseHandler($response); return $this->responseHandler($response);
} }
/**
* @param string $endpointUrl
* @param array $queryString
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function get($endpointUrl, $queryString = array()){ public function get($endpointUrl, $queryString = array()){
$request = $this->mgClient->get($endpointUrl); $request = $this->mgClient->get($endpointUrl);
if(isset($queryString)){ if(isset($queryString)){
@ -101,12 +137,29 @@ class RestClient{
return $this->responseHandler($response); return $this->responseHandler($response);
} }
/**
* @param string $endpointUrl
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function delete($endpointUrl){ public function delete($endpointUrl){
$request = $this->mgClient->delete($endpointUrl); $request = $this->mgClient->delete($endpointUrl);
$response = $request->send(); $response = $request->send();
return $this->responseHandler($response); return $this->responseHandler($response);
} }
/**
* @param string $endpointUrl
* @param array $putData
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function put($endpointUrl, $putData){ public function put($endpointUrl, $putData){
$request = $this->mgClient->put($endpointUrl, array(), $putData); $request = $this->mgClient->put($endpointUrl, array(), $putData);
$request->getPostFields()->setAggregator(new DuplicateAggregator()); $request->getPostFields()->setAggregator(new DuplicateAggregator());
@ -114,6 +167,14 @@ class RestClient{
return $this->responseHandler($response); return $this->responseHandler($response);
} }
/**
* @param \Guzzle\Http\Message\Response $responseObj
* @return \stdClass
* @throws GenericHTTPError
* @throws InvalidCredentials
* @throws MissingEndpoint
* @throws MissingRequiredParameters
*/
public function responseHandler($responseObj){ public function responseHandler($responseObj){
$httpResponseCode = $responseObj->getStatusCode(); $httpResponseCode = $responseObj->getStatusCode();
if($httpResponseCode === 200){ if($httpResponseCode === 200){
@ -124,21 +185,27 @@ class RestClient{
$result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData; $result->http_response_body = $data && $jsonResponseData === null ? $data : $jsonResponseData;
} }
elseif($httpResponseCode == 400){ elseif($httpResponseCode == 400){
throw new MissingRequiredParameters(EXCEPTION_MISSING_REQUIRED_PARAMETERS); throw new MissingRequiredParameters(ExceptionMessages::EXCEPTION_MISSING_REQUIRED_PARAMETERS);
} }
elseif($httpResponseCode == 401){ elseif($httpResponseCode == 401){
throw new InvalidCredentials(EXCEPTION_INVALID_CREDENTIALS); throw new InvalidCredentials(ExceptionMessages::EXCEPTION_INVALID_CREDENTIALS);
} }
elseif($httpResponseCode == 404){ elseif($httpResponseCode == 404){
throw new MissingEndpoint(EXCEPTION_MISSING_ENDPOINT); throw new MissingEndpoint(ExceptionMessages::EXCEPTION_MISSING_ENDPOINT);
} }
else{ else{
throw new GenericHTTPError(EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody()); throw new GenericHTTPError(ExceptionMessages::EXCEPTION_GENERIC_HTTP_ERROR, $httpResponseCode, $responseObj->getBody());
} }
$result->http_response_code = $httpResponseCode; $result->http_response_code = $httpResponseCode;
return $result; return $result;
} }
/**
* @param string $apiEndpoint
* @param string $apiVersion
* @param bool $ssl
* @return string
*/
private function generateEndpoint($apiEndpoint, $apiVersion, $ssl){ private function generateEndpoint($apiEndpoint, $apiVersion, $ssl){
if(!$ssl){ if(!$ssl){
return "http://" . $apiEndpoint . "/" . $apiVersion . "/"; return "http://" . $apiEndpoint . "/" . $apiVersion . "/";