diff --git a/CHANGELOG.md b/CHANGELOG.md index dc7cd13..aa376c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Yii2 EAuth Change Log ===================== +### dev (13.01.2016) +* Move response parsing from oauth to base service (fix #71) + ### 2.4.0 (03.01.2016) * Fixed error param names for Facebook (#63) * Use the latest Graph Api v2.5 for Facebook (#65) diff --git a/src/ServiceBase.php b/src/ServiceBase.php index 14bc13b..45b9b38 100644 --- a/src/ServiceBase.php +++ b/src/ServiceBase.php @@ -393,14 +393,31 @@ public function setHttpClient(array $config) $this->httpClient = ArrayHelper::merge($this->httpClient, $config); } + /** + * 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) + { + 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, $fn) + protected function request($url, $options, $parseResponse, $fn) { if (stripos($url, 'http') !== 0) { $url = $this->baseApiUrl . $url; @@ -417,6 +434,64 @@ protected function request($url, $options, $fn) $method = !empty($data) ? 'POST' : 'GET'; $headers = isset($options['headers']) ? $options['headers'] : []; - return $fn($url, $method, $headers, $data); + $response = $fn($url, $method, $headers, $data); + + if ($parseResponse) { + $response = $this->parseResponseInternal($response); + } + + return $response; + } + + /** + * Parse response and check for errors. + * + * @param string $response + * @return mixed + * @throws ErrorException + */ + protected function parseResponseInternal($response) + { + try { + $result = $this->parseResponse($response); + if (!isset($result)) { + throw new ErrorException(Yii::t('eauth', 'Invalid response format.'), 500); + } + + $error = $this->fetchResponseError($result); + if (isset($error) && !empty($error['message'])) { + throw new ErrorException($error['message'], $error['code']); + } + + return $result; + } catch (\Exception $e) { + throw new ErrorException($e->getMessage(), $e->getCode()); + } + } + + /** + * @param string $response + * @return mixed + */ + protected function parseResponse($response) + { + return json_decode($response, true); + } + + /** + * Returns the error array. + * + * @param array $response + * @return array the error array with 2 keys: code and message. Should be null if no errors. + */ + protected function fetchResponseError($response) + { + if (isset($response['error'])) { + return [ + 'code' => 500, + 'message' => 'Unknown error occurred.', + ]; + } + return null; } } diff --git a/src/oauth/ServiceBase.php b/src/oauth/ServiceBase.php index 4fcca2c..3f80371 100644 --- a/src/oauth/ServiceBase.php +++ b/src/oauth/ServiceBase.php @@ -153,82 +153,9 @@ 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) - { - $response = parent::request($url, $options, $fn); - - if ($parseResponse) { - $response = $this->parseResponseInternal($response); - } - - return $response; + return parent::makeRequest($url, $options, $parseResponse); } - /** - * Parse response and check for errors. - * - * @param string $response - * @return mixed - * @throws ErrorException - */ - protected function parseResponseInternal($response) - { - try { - $result = $this->parseResponse($response); - if (!isset($result)) { - throw new ErrorException(Yii::t('eauth', 'Invalid response format.'), 500); - } - - $error = $this->fetchResponseError($result); - if (isset($error) && !empty($error['message'])) { - throw new ErrorException($error['message'], $error['code']); - } - - return $result; - } catch (\Exception $e) { - throw new ErrorException($e->getMessage(), $e->getCode()); - } - } - - /** - * @param string $response - * @return mixed - */ - protected function parseResponse($response) - { - return json_decode($response, true); - } - - /** - * Returns the error array. - * - * @param array $response - * @return array the error array with 2 keys: code and message. Should be null if no errors. - */ - protected function fetchResponseError($response) - { - if (isset($response['error'])) { - return [ - 'code' => 500, - 'message' => 'Unknown error occurred.', - ]; - } - return null; - } - /** * @return array|null An array with valid access_token information. */ diff --git a/src/openid/Service.php b/src/openid/Service.php index f243deb..46d1b03 100644 --- a/src/openid/Service.php +++ b/src/openid/Service.php @@ -180,19 +180,4 @@ protected function getRealm() return Yii::$app->getRequest()->getHostInfo(); } } - - /** - * Returns the public resource. - * - * @param string $url url to request. - * @param array $options HTTP request options. Keys: query, data, headers. - * @return mixed the response. - * @throws ErrorException - */ - public function makeRequest($url, $options = []) - { - return $this->request($url, $options, function ($url, $method, $headers, $data) { - return $this->getHttpClient()->retrieveResponse($url, $data, $headers, $method); - }); - } }