Skip to content

Commit 56538bc

Browse files
committed
Request Details as auth
1 parent 55fe89c commit 56538bc

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

src/ApiRequest.php

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ class ApiRequest implements IApiRequest
2929
*/
3030
private $_requestDetail;
3131

32+
/**
33+
* @var IApiEndpoint
34+
*/
35+
protected $_endpoint;
36+
3237
/**
3338
* @param IApiRequestDetail $requestDetail
3439
*
@@ -59,9 +64,31 @@ public function getRawResult()
5964
{
6065
if($this->hasConnection())
6166
{
62-
$this->setRawResult(
63-
$this->_getConnection()->load($this)->getRawResult()
64-
);
67+
if($this->_requestDetail->requiresAuth())
68+
{
69+
$this->_getConnection()->setToken($this->_endpoint->getToken());
70+
try
71+
{
72+
$result = $this->_getConnection()->load($this);
73+
}
74+
catch(\Exception $e)
75+
{
76+
if($e->getCode() == 403 && stristr($e->getMessage(), 'token'))
77+
{
78+
$result = $this->_getConnection()->load($this);
79+
}
80+
else
81+
{
82+
throw $e;
83+
}
84+
}
85+
}
86+
else
87+
{
88+
$result = $this->_getConnection()->load($this);
89+
}
90+
91+
$this->setRawResult($result->getRawResult());
6592
}
6693
else
6794
{

src/ApiRequestDetail.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class ApiRequestDetail implements IApiRequestDetail
1111
protected $_body = '';
1212
protected $_method = 'GET';
1313
protected $_isAsync = false;
14+
protected $_requireAuth = true;
1415

1516
/**
1617
* @inheritDoc
@@ -230,4 +231,23 @@ public function setAsync($isAsync)
230231
return $this;
231232
}
232233

234+
/**
235+
* @param boolean $requireAuth
236+
*
237+
* @return ApiRequestDetail
238+
*/
239+
public function setRequireAuth($requireAuth)
240+
{
241+
$this->_requireAuth = $requireAuth;
242+
return $this;
243+
}
244+
245+
/**
246+
* @inheritDoc
247+
*/
248+
public function requiresAuth()
249+
{
250+
return (bool)$this->_requireAuth;
251+
}
252+
233253
}

src/IApiEndpoint.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55

66
interface IApiEndpoint extends IApiConnectionAware
77
{
8-
public function setApiDefinition(IApiDefinition $definition);
9-
108
/**
11-
* @return IApiDefinition
9+
* @param IApiDefinition $definition
10+
*
11+
* @return IApiEndpoint|static
1212
*/
13-
public function getApiDefinition();
13+
public function setApiDefinition(IApiDefinition $definition);
1414

1515
/**
16-
* @return bool
16+
* @return OAuth\Tokens\IToken|null
1717
*/
18-
public function hasConnection();
18+
public function getToken();
1919
}

src/IApiRequestDetail.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public function getMethod();
5858
* @return bool
5959
*/
6060
public function isAync();
61+
62+
/**
63+
* @return bool
64+
*/
65+
public function requiresAuth();
6166
}

0 commit comments

Comments
 (0)