diff --git a/CHANGELOG.md b/CHANGELOG.md index 4063c4c..ba4fe91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/src/oauth/ServiceBase.php b/src/oauth/ServiceBase.php index 26f9126..4f33221 100644 --- a/src/oauth/ServiceBase.php +++ b/src/oauth/ServiceBase.php @@ -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. @@ -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 []; + } } \ No newline at end of file diff --git a/src/oauth1/ServiceProxy.php b/src/oauth1/ServiceProxy.php index 330f91a..acd1e9e 100644 --- a/src/oauth1/ServiceProxy.php +++ b/src/oauth1/ServiceProxy.php @@ -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(); + } } \ No newline at end of file diff --git a/src/oauth2/Service.php b/src/oauth2/Service.php index f42bb16..cb56dc4 100644 --- a/src/oauth2/Service.php +++ b/src/oauth2/Service.php @@ -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.