Commit fbce722d by Klimov Paul

Request HTTP headers argument added to `yii\authclient\BaseOAuth::api()` method

parent fe46caf7
...@@ -168,15 +168,17 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -168,15 +168,17 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
* @param string $method request type. * @param string $method request type.
* @param string $url request URL. * @param string $url request URL.
* @param array $params request params. * @param array $params request params.
* @param array $headers additional request headers.
* @return array response. * @return array response.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
protected function sendRequest($method, $url, array $params = []) protected function sendRequest($method, $url, array $params = [], array $headers = [])
{ {
$curlOptions = $this->mergeCurlOptions( $curlOptions = $this->mergeCurlOptions(
$this->defaultCurlOptions(), $this->defaultCurlOptions(),
$this->getCurlOptions(), $this->getCurlOptions(),
[ [
CURLOPT_HTTPHEADER => $headers,
CURLOPT_RETURNTRANSFER => true, CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url, CURLOPT_URL => $url,
], ],
...@@ -479,10 +481,11 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -479,10 +481,11 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
* @param string $apiSubUrl API sub URL, which will be append to [[apiBaseUrl]], or absolute API URL. * @param string $apiSubUrl API sub URL, which will be append to [[apiBaseUrl]], or absolute API URL.
* @param string $method request method. * @param string $method request method.
* @param array $params request parameters. * @param array $params request parameters.
* @param array $headers additional request headers.
* @return array API response * @return array API response
* @throws Exception on failure. * @throws Exception on failure.
*/ */
public function api($apiSubUrl, $method = 'GET', array $params = []) public function api($apiSubUrl, $method = 'GET', array $params = [], array $headers = [])
{ {
if (preg_match('/^https?:\\/\\//is', $apiSubUrl)) { if (preg_match('/^https?:\\/\\//is', $apiSubUrl)) {
$url = $apiSubUrl; $url = $apiSubUrl;
...@@ -494,7 +497,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -494,7 +497,7 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
throw new Exception('Invalid access token.'); throw new Exception('Invalid access token.');
} }
return $this->apiInternal($accessToken, $url, $method, $params); return $this->apiInternal($accessToken, $url, $method, $params, $headers);
} }
/** /**
...@@ -520,8 +523,9 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface ...@@ -520,8 +523,9 @@ abstract class BaseOAuth extends BaseClient implements ClientInterface
* @param string $url absolute API URL. * @param string $url absolute API URL.
* @param string $method request method. * @param string $method request method.
* @param array $params request parameters. * @param array $params request parameters.
* @param array $headers additional request headers.
* @return array API response. * @return array API response.
* @throws Exception on failure. * @throws Exception on failure.
*/ */
abstract protected function apiInternal($accessToken, $url, $method, array $params); abstract protected function apiInternal($accessToken, $url, $method, array $params, array $headers);
} }
...@@ -6,6 +6,7 @@ Yii Framework 2 authclient extension Change Log ...@@ -6,6 +6,7 @@ Yii Framework 2 authclient extension Change Log
- Bug #3633: OpenId return URL comparison advanced to prevent url encode problem (klimov-paul) - Bug #3633: OpenId return URL comparison advanced to prevent url encode problem (klimov-paul)
- Enh #3416: VKontakte OAuth support added (klimov-paul) - Enh #3416: VKontakte OAuth support added (klimov-paul)
- Enh #4076: Request HTTP headers argument added to `yii\authclient\BaseOAuth::api()` method (klimov-paul)
- Enh #4139: User attributes requesting at GoogleOAuth switched to Google+ API (klimov-paul) - Enh #4139: User attributes requesting at GoogleOAuth switched to Google+ API (klimov-paul)
......
...@@ -151,14 +151,15 @@ class OAuth1 extends BaseOAuth ...@@ -151,14 +151,15 @@ class OAuth1 extends BaseOAuth
* @param string $method request type. * @param string $method request type.
* @param string $url request URL. * @param string $url request URL.
* @param array $params request params. * @param array $params request params.
* @param array $headers additional request headers.
* @return array response. * @return array response.
*/ */
protected function sendSignedRequest($method, $url, array $params = []) protected function sendSignedRequest($method, $url, array $params = [], array $headers = [])
{ {
$params = array_merge($params, $this->generateCommonRequestParams()); $params = array_merge($params, $this->generateCommonRequestParams());
$params = $this->signRequest($method, $url, $params); $params = $this->signRequest($method, $url, $params);
return $this->sendRequest($method, $url, $params); return $this->sendRequest($method, $url, $params, $headers);
} }
/** /**
...@@ -206,19 +207,13 @@ class OAuth1 extends BaseOAuth ...@@ -206,19 +207,13 @@ class OAuth1 extends BaseOAuth
} }
/** /**
* Performs request to the OAuth API. * @inheritdoc
* @param OAuthToken $accessToken actual access token.
* @param string $url absolute API URL.
* @param string $method request method.
* @param array $params request parameters.
* @return array API response.
* @throws Exception on failure.
*/ */
protected function apiInternal($accessToken, $url, $method, array $params) protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{ {
$params['oauth_consumer_key'] = $this->consumerKey; $params['oauth_consumer_key'] = $this->consumerKey;
$params['oauth_token'] = $accessToken->getToken(); $params['oauth_token'] = $accessToken->getToken();
$response = $this->sendSignedRequest($method, $url, $params); $response = $this->sendSignedRequest($method, $url, $params, $headers);
return $response; return $response;
} }
......
...@@ -132,19 +132,13 @@ class OAuth2 extends BaseOAuth ...@@ -132,19 +132,13 @@ class OAuth2 extends BaseOAuth
} }
/** /**
* Performs request to the OAuth API. * @inheritdoc
* @param OAuthToken $accessToken actual access token.
* @param string $url absolute API URL.
* @param string $method request method.
* @param array $params request parameters.
* @return array API response.
* @throws Exception on failure.
*/ */
protected function apiInternal($accessToken, $url, $method, array $params) protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{ {
$params['access_token'] = $accessToken->getToken(); $params['access_token'] = $accessToken->getToken();
return $this->sendRequest($method, $url, $params); return $this->sendRequest($method, $url, $params, $headers);
} }
/** /**
......
...@@ -128,11 +128,11 @@ class LinkedIn extends OAuth2 ...@@ -128,11 +128,11 @@ class LinkedIn extends OAuth2
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function apiInternal($accessToken, $url, $method, array $params) protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{ {
$params['oauth2_access_token'] = $accessToken->getToken(); $params['oauth2_access_token'] = $accessToken->getToken();
return $this->sendRequest($method, $url, $params); return $this->sendRequest($method, $url, $params, $headers);
} }
/** /**
......
...@@ -80,11 +80,11 @@ class VKontakte extends OAuth2 ...@@ -80,11 +80,11 @@ class VKontakte extends OAuth2
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function apiInternal($accessToken, $url, $method, array $params) protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{ {
$params['uids'] = $accessToken->getParam('user_id'); $params['uids'] = $accessToken->getParam('user_id');
$params['access_token'] = $accessToken->getToken(); $params['access_token'] = $accessToken->getToken();
return $this->sendRequest($method, $url, $params); return $this->sendRequest($method, $url, $params, $headers);
} }
/** /**
......
...@@ -64,14 +64,14 @@ class YandexOAuth extends OAuth2 ...@@ -64,14 +64,14 @@ class YandexOAuth extends OAuth2
/** /**
* @inheritdoc * @inheritdoc
*/ */
protected function apiInternal($accessToken, $url, $method, array $params) protected function apiInternal($accessToken, $url, $method, array $params, array $headers)
{ {
if (!isset($params['format'])) { if (!isset($params['format'])) {
$params['format'] = 'json'; $params['format'] = 'json';
} }
$params['oauth_token'] = $accessToken->getToken(); $params['oauth_token'] = $accessToken->getToken();
return $this->sendRequest($method, $url, $params); return $this->sendRequest($method, $url, $params, $headers);
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment