Skip to content
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

Added ability to specify "Query Parameters" for each API call #222

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
30 changes: 29 additions & 1 deletion src/Jira/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ public function api(
) {
$result = $this->client->sendRequest(
$method,
$url,
$this->addQueryParametersToUrl($method, $url, $data),
$data,
$this->getEndpoint(),
$this->authentication,
Expand Down Expand Up @@ -799,6 +799,34 @@ public function api(
return false;
}

/**
* Adds the query parameters to the URL.
*
* @param string $method Request method.
* @param string $url URL.
* @param array|string $data Data.
*
* @return string
*/
protected function addQueryParametersToUrl($method, $url, array &$data)
{
if ( !array_key_exists('_query', $data) ) {
return $url;
}

$query_parameters = $data['_query'];
unset($data['_query']);

// For GET requests all given parameters end up in Query parameters.
if ( $method === self::REQUEST_GET ) {
$data = $query_parameters + $data;

return $url;
}

return $url . (strpos($url, '?') === false ? '?' : ':') . http_build_query($query_parameters);
}

/**
* Downloads attachment.
*
Expand Down
68 changes: 68 additions & 0 deletions tests/Jira/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,74 @@ public function testDeleteWorkLogWithCustomParams()
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
}

public function testQueryParametersHandlingForGetRequestMethod()
{
$this->expectClientCall(
Api::REQUEST_GET,
'/rest/api/2/something',
array(
'q_p1' => 'q_p1_v',
'q_p2' => 'q_p2_v',
'rb_p1' => 'rb_p1_v',
'rb_p2' => 'rb_p2_v',
'rb_p3' => 'rb_p3_v',
),
'{}'
);

$this->api->api(
Api::REQUEST_GET,
'/rest/api/2/something',
array(
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
'rb_p1' => 'rb_p1_v',
'rb_p2' => 'rb_p2_v',
'rb_p3' => 'rb_p3_v',
),
true
);
}

/**
* @dataProvider queryParametersHandlingForOtherRequestMethodsDataProvider
*
* @param string $request_method Request method.
*/
public function testQueryParametersHandlingForOtherRequestMethods($request_method)
{
$this->expectClientCall(
$request_method,
'/rest/api/2/something?q_p1=q_p1_v&q_p2=q_p2_v',
array(
'rb_p1' => 'rb_p1_v',
'rb_p2' => 'rb_p2_v',
'rb_p3' => 'rb_p3_v',
),
'{}'
);

$this->api->api(
$request_method,
'/rest/api/2/something',
array(
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
'rb_p1' => 'rb_p1_v',
'rb_p2' => 'rb_p2_v',
'rb_p3' => 'rb_p3_v',
),
true
);
}

public static function queryParametersHandlingForOtherRequestMethodsDataProvider()
{
return array(
'delete' => array(Api::REQUEST_DELETE),
'post' => array(Api::REQUEST_POST),
'put' => array(Api::REQUEST_PUT),
);
}

/**
* Expects a particular client call.
*
Expand Down
Loading