Skip to content

Commit 6db0559

Browse files
committed
Added ability to specify "Query Parameters" for each API call
1 parent 78fc187 commit 6db0559

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/Jira/Api.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ public function api(
768768
) {
769769
$result = $this->client->sendRequest(
770770
$method,
771-
$url,
771+
$this->addQueryParametersToUrl($method, $url, $data),
772772
$data,
773773
$this->getEndpoint(),
774774
$this->authentication,
@@ -801,6 +801,34 @@ public function api(
801801
return false;
802802
}
803803

804+
/**
805+
* Adds the query parameters to the URL.
806+
*
807+
* @param string $method Request method.
808+
* @param string $url URL.
809+
* @param array|string $data Data.
810+
*
811+
* @return string
812+
*/
813+
protected function addQueryParametersToUrl($method, $url, array &$data)
814+
{
815+
if ( !array_key_exists('_query', $data) ) {
816+
return $url;
817+
}
818+
819+
$query_parameters = $data['_query'];
820+
unset($data['_query']);
821+
822+
// For GET requests all given parameters end up in Query parameters.
823+
if ( $method === self::REQUEST_GET ) {
824+
$data = $query_parameters + $data;
825+
826+
return $url;
827+
}
828+
829+
return $url . (strpos($url, '?') === false ? '?' : ':') . http_build_query($query_parameters);
830+
}
831+
804832
/**
805833
* Downloads attachment.
806834
*

tests/Jira/ApiTest.php

+68
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,74 @@ public function testDeleteWorkLogWithCustomParams()
392392
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
393393
}
394394

395+
public function testQueryParametersHandlingForGetRequestMethod()
396+
{
397+
$this->expectClientCall(
398+
Api::REQUEST_GET,
399+
'/rest/api/2/something',
400+
array(
401+
'q_p1' => 'q_p1_v',
402+
'q_p2' => 'q_p2_v',
403+
'rb_p1' => 'rb_p1_v',
404+
'rb_p2' => 'rb_p2_v',
405+
'rb_p3' => 'rb_p3_v',
406+
),
407+
'{}'
408+
);
409+
410+
$this->api->api(
411+
Api::REQUEST_GET,
412+
'/rest/api/2/something',
413+
array(
414+
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
415+
'rb_p1' => 'rb_p1_v',
416+
'rb_p2' => 'rb_p2_v',
417+
'rb_p3' => 'rb_p3_v',
418+
),
419+
true
420+
);
421+
}
422+
423+
/**
424+
* @dataProvider queryParametersHandlingForOtherRequestMethodsDataProvider
425+
*
426+
* @param string $request_method Request method.
427+
*/
428+
public function testQueryParametersHandlingForOtherRequestMethods($request_method)
429+
{
430+
$this->expectClientCall(
431+
$request_method,
432+
'/rest/api/2/something?q_p1=q_p1_v&q_p2=q_p2_v',
433+
array(
434+
'rb_p1' => 'rb_p1_v',
435+
'rb_p2' => 'rb_p2_v',
436+
'rb_p3' => 'rb_p3_v',
437+
),
438+
'{}'
439+
);
440+
441+
$this->api->api(
442+
$request_method,
443+
'/rest/api/2/something',
444+
array(
445+
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
446+
'rb_p1' => 'rb_p1_v',
447+
'rb_p2' => 'rb_p2_v',
448+
'rb_p3' => 'rb_p3_v',
449+
),
450+
true
451+
);
452+
}
453+
454+
public static function queryParametersHandlingForOtherRequestMethodsDataProvider()
455+
{
456+
return array(
457+
'delete' => array(Api::REQUEST_DELETE),
458+
'post' => array(Api::REQUEST_POST),
459+
'put' => array(Api::REQUEST_PUT),
460+
);
461+
}
462+
395463
/**
396464
* Expects a particular client call.
397465
*

0 commit comments

Comments
 (0)