Skip to content

Commit

Permalink
Move response parsing from oauth to base service (fix #71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodge committed Jan 13, 2016
1 parent c1ad9fa commit b345318
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 91 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
79 changes: 77 additions & 2 deletions src/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
75 changes: 1 addition & 74 deletions src/oauth/ServiceBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
15 changes: 0 additions & 15 deletions src/openid/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
}

0 comments on commit b345318

Please sign in to comment.