Skip to content

Commit

Permalink
Fixed makeRequest method (fix #68)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodge committed Jan 3, 2016
1 parent a00e9d5 commit 176f3fe
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 82 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii2 EAuth Change Log
### dev
* Fixed error param names for Facebook (#63)
* Use the latest Graph Api v2.5 for Facebook (#65)
* Fixed `makeRequest` method (#68)

### 2.3.0 (17.10.2015)
* Added InstagramOAuth2Service (#61)
Expand Down
154 changes: 82 additions & 72 deletions src/oauth/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,78 +155,78 @@ public function getTokenDefaultLifetime()
return $this->tokenDefaultLifetime;
}

/**
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
* @throws ErrorException
*/
public function makeSignedRequest($url, $options = [], $parseResponse = true)
{
if (!$this->getIsAuthenticated()) {
throw new ErrorException(Yii::t('eauth', 'Unable to complete the signed request because the user was not authenticated.'), 401);
}

if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}

$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}

$data = isset($options['data']) ? $options['data'] : [];
$method = !empty($data) ? 'POST' : 'GET';
$headers = isset($options['headers']) ? $options['headers'] : [];

$response = $this->getProxy()->request($url, $method, $data, $headers);

if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}

return $response;
}

/**
* Returns the public resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
*/
public function makeRequest($url, $options = [], $parseResponse = true) {
if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}

$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}

$data = isset($options['data']) ? $options['data'] : [];
$method = !empty($data) ? 'POST' : 'GET';

$headers = isset($options['headers']) ? $options['headers'] : [];
$headers = array_merge($this->getProxy()->getExtraApiHeaders(), $headers);

$response = $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method);
if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}

return $response;
}
/**
* Returns the protected resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
* @throws ErrorException
*/
public function makeSignedRequest($url, $options = [], $parseResponse = true)
{
if (!$this->getIsAuthenticated()) {
throw new ErrorException(Yii::t('eauth', 'Unable to complete the signed request because the user was not authenticated.'), 401);
}

return $this->request($url, $options, $parseResponse, function ($url, $method, $headers, $data) {
return $this->getProxy()->request($url, $method, $data, $headers);
});
}

/**
* Returns the public resource.
*
* @param string $url url to request.
* @param array $options HTTP request options. Keys: query, data, headers.
* @param boolean $parseResponse Whether to parse response.
* @return mixed the response.
* @throws ErrorException
*/
public function makeRequest($url, $options = [], $parseResponse = true)
{
$headers = isset($options['headers']) ? $options['headers'] : [];
$options['headers'] = array_merge($this->getExtraApiHeaders(), $headers);

return $this->request($url, $options, $parseResponse, function ($url, $method, $headers, $data) {
return $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method);
});
}

/**
* @param string $url
* @param array $options
* @param boolean $parseResponse
* @param callable $fn
* @return mixed
* @throws ErrorException
*/
protected function request($url, $options, $parseResponse, $fn)
{
if (stripos($url, 'http') !== 0) {
$url = $this->baseApiUrl . $url;
}

$url = new Uri($url);
if (isset($options['query'])) {
foreach ($options['query'] as $key => $value) {
$url->addToQuery($key, $value);
}
}

$data = isset($options['data']) ? $options['data'] : [];
$method = !empty($data) ? 'POST' : 'GET';
$headers = isset($options['headers']) ? $options['headers'] : [];

$response = $fn($url, $method, $headers, $data);

if ($parseResponse) {
$response = $this->parseResponseInternal($response);
}

return $response;
}

/**
* Parse response and check for errors.
Expand Down Expand Up @@ -310,4 +310,14 @@ public function getAccessTokenResponseError($data)
{
return isset($data['error']) ? $data['error'] : null;
}

/**
* Return any additional headers always needed for this service implementation's API calls.
*
* @return array
*/
public function getExtraApiHeaders()
{
return [];
}
}
10 changes: 10 additions & 0 deletions src/oauth1/ServiceProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,14 @@ protected function parseAccessTokenResponse($responseBody)

return $token;
}

/**
* Return any additional headers always needed for this service implementation's API calls.
*
* @return array
*/
protected function getExtraApiHeaders()
{
return $this->service->getExtraApiHeaders();
}
}
10 changes: 0 additions & 10 deletions src/oauth2/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,16 +306,6 @@ public function getExtraOAuthHeaders()
return [];
}

/**
* Return any additional headers always needed for this service implementation's API calls.
*
* @return array
*/
public function getExtraApiHeaders()
{
return [];
}

/**
* Returns a class constant from ServiceInterface defining the authorization method used for the API
* Header is the sane default.
Expand Down

0 comments on commit 176f3fe

Please sign in to comment.