Skip to content

Commit

Permalink
Request Details as auth
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Jan 15, 2016
1 parent 55fe89c commit 56538bc
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
33 changes: 30 additions & 3 deletions src/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class ApiRequest implements IApiRequest
*/
private $_requestDetail;

/**
* @var IApiEndpoint
*/
protected $_endpoint;

/**
* @param IApiRequestDetail $requestDetail
*
Expand Down Expand Up @@ -59,9 +64,31 @@ public function getRawResult()
{
if($this->hasConnection())
{
$this->setRawResult(
$this->_getConnection()->load($this)->getRawResult()
);
if($this->_requestDetail->requiresAuth())
{
$this->_getConnection()->setToken($this->_endpoint->getToken());
try
{
$result = $this->_getConnection()->load($this);
}
catch(\Exception $e)
{
if($e->getCode() == 403 && stristr($e->getMessage(), 'token'))
{
$result = $this->_getConnection()->load($this);
}
else
{
throw $e;
}
}
}
else
{
$result = $this->_getConnection()->load($this);
}

$this->setRawResult($result->getRawResult());
}
else
{
Expand Down
20 changes: 20 additions & 0 deletions src/ApiRequestDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApiRequestDetail implements IApiRequestDetail
protected $_body = '';
protected $_method = 'GET';
protected $_isAsync = false;
protected $_requireAuth = true;

/**
* @inheritDoc
Expand Down Expand Up @@ -230,4 +231,23 @@ public function setAsync($isAsync)
return $this;
}

/**
* @param boolean $requireAuth
*
* @return ApiRequestDetail
*/
public function setRequireAuth($requireAuth)
{
$this->_requireAuth = $requireAuth;
return $this;
}

/**
* @inheritDoc
*/
public function requiresAuth()
{
return (bool)$this->_requireAuth;
}

}
12 changes: 6 additions & 6 deletions src/IApiEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

interface IApiEndpoint extends IApiConnectionAware
{
public function setApiDefinition(IApiDefinition $definition);

/**
* @return IApiDefinition
* @param IApiDefinition $definition
*
* @return IApiEndpoint|static
*/
public function getApiDefinition();
public function setApiDefinition(IApiDefinition $definition);

/**
* @return bool
* @return OAuth\Tokens\IToken|null
*/
public function hasConnection();
public function getToken();
}
5 changes: 5 additions & 0 deletions src/IApiRequestDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ public function getMethod();
* @return bool
*/
public function isAync();

/**
* @return bool
*/
public function requiresAuth();
}

0 comments on commit 56538bc

Please sign in to comment.