Skip to content

Commit

Permalink
Added ability to specify "Query Parameters" for each API call
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Jan 2, 2025
1 parent c94011b commit 3fed4dd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Jira/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ public function api(
) {
$result = $this->client->sendRequest(
$method,
$url,
$this->addQueryParametersToUrl($method, $url, $data),
$data,
$this->getEndpoint(),
$this->authentication,
Expand Down Expand Up @@ -801,6 +801,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 @@ -392,6 +392,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

0 comments on commit 3fed4dd

Please sign in to comment.