Skip to content

Allow string arguments for the $body parameter in httpRequest #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Common/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,26 @@ public function getHTTPTransporter()
/**
* {@inheritdoc}
*/
public function requestJSON($uri, array $body = [], $method = 'GET', array $extraHeaders = [])
public function requestJSON($uri, $body = [], $method = 'GET', array $extraHeaders = [])
{
return json_decode($this->request($uri, $body, $method, $extraHeaders), TRUE);
}

/**
* {@inheritdoc}
*/
public function httpRequest($uri, array $body = [], array $headers = [], $method = 'POST')
public function httpRequest($uri, $body = [], array $headers = [], $method = 'POST')
{
try
{
$response = $this->httpTransporter->submit($uri, $body, $method, $headers);
if (is_array($body))
{
$response = $this->httpTransporter->submit($uri, $body, $method, $headers);
} else
{
$response = $this->httpTransporter->call($uri, $method, $headers, $body);
}

} catch (RequestException $e)
{
throw new TokenResponseException($e->getMessage() ? $e->getMessage() : 'Failed to request resource.');
Expand Down
17 changes: 8 additions & 9 deletions src/Common/Service/ServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,37 @@ interface ServiceInterface {
* If the path provided is not an absolute URI, the base API Uri (service-specific) will be used.
*
* @param string|Url $path
* @param array $body Request body if applicable (an associative array will
* @param array|string $body Request body if applicable (an associative array will automatically be converted into a urlencoded body)
* @param string $method HTTP method
* automatically be converted into a urlencoded body)
* @param array $extraHeaders Extra headers if applicable. These will override service-specific
* any defaults.
*
* @param array $extraHeaders Extra headers if applicable. These will override service-specific defaults.
* @return string
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = []);
public function request($path, $body = [], $method = 'GET', array $extraHeaders = []);

/**
* Shortcut for json_decode($this->request(...
*
* @param $uri
* @param array $body
* @param array|string $body
* @param string $method
* @param array $extraHeaders
* @return array
*/
public function requestJSON($uri, array $body = [], $method = 'GET', array $extraHeaders = []);
public function requestJSON($uri, $body = [], $method = 'GET', array $extraHeaders = []);

/**
* Sends an authenticated API request to the path provided.
* If the path provided is not an absolute URI, the base API Uri (must be passed into constructor) will be used.
*
* @param Url|string $uri
* @param array $body Request body if applicable (key/value pairs)
* @param array|string $body Request body if applicable
* @param array $headers Extra headers if applicable.
* @param string $method HTTP method
* @throws TokenResponseException
* @return string
*/
public function httpRequest($uri, array $body = [], array $headers = [], $method = 'POST');
public function httpRequest($uri, $body = [], array $headers = [], $method = 'POST');

/**
* Returns the url to redirect to for authorization purposes.
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth1/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public function requestAccessToken($token, $verifier, $tokenSecret = NULL)
/**
* {@inheritdoc}
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
$uri = $this->determineRequestUriFromPath($path, $this->baseApiUri);

Expand Down
2 changes: 1 addition & 1 deletion src/OAuth1/Service/Flickr.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ protected function parseAccessTokenResponse($responseBody)
return $token;
}

public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
$uri = $this->determineRequestUriFromPath('/');
$uri->getQuery()->modify(['method' => $path]);
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Service/AbstractService.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function getAuthorizationUri(array $additionalParameters = [])
* @throws ExpiredTokenException
* @throws Exception
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
$uri = $this->determineRequestUriFromPath($path);
$token = $this->storage->retrieveAccessToken($this->service());
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Service/Foursquare.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected function parseAccessTokenResponse($responseBody)
/**
* {@inheritdoc}
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
$uri = $this->determineRequestUriFromPath($path);
$uri->getQuery()->modify(['v' => $this->apiVersionDate]);
Expand Down
2 changes: 1 addition & 1 deletion src/OAuth2/Service/Mailchimp.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected function parseAccessTokenResponse($responseBody)
/**
* {@inheritdoc}
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
if (is_null($this->baseApiUri))
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Common/Service/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(
/**
* {@inheritdoc}
*/
public function request($path, array $body = [], $method = 'GET', array $extraHeaders = [])
public function request($path, $body = [], $method = 'GET', array $extraHeaders = [])
{
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/OAuth1/Service/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Mock extends AbstractService
protected $authorizationEndpoint = 'http://pieterhordijk.com/auth';
protected $accessTokenEndpoint = 'http://pieterhordijk.com/access';

public function httpRequest($uri, array $body = [], array $headers = [], $method = 'POST')
public function httpRequest($uri, $body = [], array $headers = [], $method = 'POST')
{
return [
'uri' => $uri,
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/OAuth2/Service/Mock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function setAuthorizationMethod($method)
$this->authorizationMethod = $method;
}

public function httpRequest($uri, array $body = [], array $headers = [], $method = 'POST')
public function httpRequest($uri, $body = [], array $headers = [], $method = 'POST')
{
return [
'uri' => $uri,
Expand Down