Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Send access token on header authorization #106

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
71 changes: 25 additions & 46 deletions lib/Api/ItemsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ public function getConfig()
* @throws \InvalidArgumentException
* @return object
*/
public function itemsIdGet($id)
public function itemsIdGet($id, $access_token)
{
list($response) = $this->itemsIdGetWithHttpInfo($id);
list($response) = $this->itemsIdGetWithHttpInfo($id, $access_token);
return $response;
}

Expand All @@ -138,14 +138,14 @@ public function itemsIdGet($id)
* Return a Item.
*
* @param string $id (required)
*
* @param string $access_token (required)
* @throws \Meli\ApiException on non-2xx response
* @throws \InvalidArgumentException
* @return array of object, HTTP status code, HTTP response headers (array of strings)
*/
public function itemsIdGetWithHttpInfo($id)
public function itemsIdGetWithHttpInfo($id, $access_token)
{
$request = $this->itemsIdGetRequest($id);
$request = $this->itemsIdGetRequest($id, $access_token);

try {
$options = $this->createHttpClientOption();
Expand Down Expand Up @@ -226,13 +226,13 @@ public function itemsIdGetWithHttpInfo($id)
* Return a Item.
*
* @param string $id (required)
*
* @param string $access_token (required)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function itemsIdGetAsync($id)
public function itemsIdGetAsync($id, $access_token)
{
return $this->itemsIdGetAsyncWithHttpInfo($id)
return $this->itemsIdGetAsyncWithHttpInfo($id, $access_token)
->then(
function ($response) {
return $response[0];
Expand All @@ -246,14 +246,14 @@ function ($response) {
* Return a Item.
*
* @param string $id (required)
*
* @param string $access_token (required)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Promise\PromiseInterface
*/
public function itemsIdGetAsyncWithHttpInfo($id)
public function itemsIdGetAsyncWithHttpInfo($id, $access_token)
{
$returnType = 'object';
$request = $this->itemsIdGetRequest($id);
$request = $this->itemsIdGetRequest($id, $access_token);

return $this->client
->sendAsync($request, $this->createHttpClientOption())
Expand Down Expand Up @@ -293,11 +293,11 @@ function ($exception) {
* Create request for operation 'itemsIdGet'
*
* @param string $id (required)
*
* @param string $access_token (required)
* @throws \InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
protected function itemsIdGetRequest($id)
protected function itemsIdGetRequest($id, $access_token)
{
// verify the required parameter 'id' is set
if ($id === null || (is_array($id) && count($id) === 0)) {
Expand Down Expand Up @@ -329,12 +329,14 @@ protected function itemsIdGetRequest($id)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
[]
[],
$access_token
);
}

Expand Down Expand Up @@ -608,19 +610,6 @@ protected function itemsIdPutRequest($id, $access_token, $item)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}


// path params
if ($id !== null) {
$resourcePath = str_replace(
Expand All @@ -638,12 +627,14 @@ protected function itemsIdPutRequest($id, $access_token, $item)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['application/json']
['application/json'],
$access_token
);
}

Expand Down Expand Up @@ -906,20 +897,6 @@ protected function itemsPostRequest($access_token, $item)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}



// body params
$_tempBody = null;
if (isset($item)) {
Expand All @@ -928,12 +905,14 @@ protected function itemsPostRequest($access_token, $item)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['application/json']
['application/json'],
$access_token
);
}

Expand Down
12 changes: 8 additions & 4 deletions lib/Api/OAuth20Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,12 +336,14 @@ protected function authRequest($response_type, $client_id, $redirect_uri)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
''
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
[]
[],
''
);
}

Expand Down Expand Up @@ -644,12 +646,14 @@ protected function getTokenRequest($grant_type = null, $client_id = null, $clien

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
''
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['application/x-www-form-urlencoded']
['application/x-www-form-urlencoded'],
''
);
}

Expand Down
76 changes: 16 additions & 60 deletions lib/Api/RestClientApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,19 +324,6 @@ protected function resourceDeleteRequest($resource, $access_token)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}


// path params
if ($resource !== null) {
$resourcePath = str_replace(
Expand All @@ -351,12 +338,14 @@ protected function resourceDeleteRequest($resource, $access_token)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
[]
[],
$access_token
);
}

Expand Down Expand Up @@ -629,19 +618,6 @@ protected function resourceGetRequest($resource, $access_token)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}


// path params
if ($resource !== null) {
$resourcePath = str_replace(
Expand All @@ -656,12 +632,14 @@ protected function resourceGetRequest($resource, $access_token)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
[]
[],
$access_token
);
}

Expand Down Expand Up @@ -945,19 +923,6 @@ protected function resourcePostRequest($resource, $access_token, $body)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}


// path params
if ($resource !== null) {
$resourcePath = str_replace(
Expand All @@ -975,12 +940,14 @@ protected function resourcePostRequest($resource, $access_token, $body)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['application/json']
['application/json'],
$access_token
);
}

Expand Down Expand Up @@ -1264,19 +1231,6 @@ protected function resourcePutRequest($resource, $access_token, $body)
$httpBody = '';
$multipart = false;

// query params
if ($access_token !== null) {
if('form' === 'form' && is_array($access_token)) {
foreach($access_token as $key => $value) {
$queryParams[$key] = $value;
}
}
else {
$queryParams['access_token'] = $access_token;
}
}


// path params
if ($resource !== null) {
$resourcePath = str_replace(
Expand All @@ -1294,12 +1248,14 @@ protected function resourcePutRequest($resource, $access_token, $body)

if ($multipart) {
$headers = $this->headerSelector->selectHeadersForMultipart(
['application/json']
['application/json'],
$access_token
);
} else {
$headers = $this->headerSelector->selectHeaders(
['application/json'],
['application/json']
['application/json'],
$access_token
);
}

Expand Down
7 changes: 4 additions & 3 deletions lib/HeaderSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class HeaderSelector
* @param string[] $contentTypes
* @return array
*/
public function selectHeaders($accept, $contentTypes)
public function selectHeaders($accept, $contentTypes, $accessToken)
{
$headers = [];

Expand All @@ -55,6 +55,7 @@ public function selectHeaders($accept, $contentTypes)
$headers['Accept'] = $accept;
}

$headers['Authorization'] = 'Bearer '. $accessToken;
$headers['Content-Type'] = $this->selectContentTypeHeader($contentTypes);
return $headers;
}
Expand All @@ -63,9 +64,9 @@ public function selectHeaders($accept, $contentTypes)
* @param string[] $accept
* @return array
*/
public function selectHeadersForMultipart($accept)
public function selectHeadersForMultipart($accept, $accessToken)
{
$headers = $this->selectHeaders($accept, []);
$headers = $this->selectHeaders($accept, [], $accessToken);

unset($headers['Content-Type']);
return $headers;
Expand Down