Skip to content

Commit 891ee94

Browse files
committed
Added ability to specify "Query Parameters" for each API call
1 parent 52b5bd6 commit 891ee94

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

src/Jira/Api.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ public function api(
766766
) {
767767
$result = $this->client->sendRequest(
768768
$method,
769-
$url,
769+
$this->addQueryParametersToUrl($method, $url, $data),
770770
$data,
771771
$this->getEndpoint(),
772772
$this->authentication,
@@ -799,6 +799,34 @@ public function api(
799799
return false;
800800
}
801801

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

tests/Jira/ApiTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,74 @@ public function testDeleteWorkLogWithCustomParams()
495495
$this->assertEquals(json_decode($response, true), $actual, 'The response is json-decoded.');
496496
}
497497

498+
public function testQueryParametersHandlingForGetRequestMethod()
499+
{
500+
$this->expectClientCall(
501+
Api::REQUEST_GET,
502+
'/rest/api/2/something',
503+
array(
504+
'q_p1' => 'q_p1_v',
505+
'q_p2' => 'q_p2_v',
506+
'rb_p1' => 'rb_p1_v',
507+
'rb_p2' => 'rb_p2_v',
508+
'rb_p3' => 'rb_p3_v',
509+
),
510+
'{}'
511+
);
512+
513+
$this->api->api(
514+
Api::REQUEST_GET,
515+
'/rest/api/2/something',
516+
array(
517+
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
518+
'rb_p1' => 'rb_p1_v',
519+
'rb_p2' => 'rb_p2_v',
520+
'rb_p3' => 'rb_p3_v',
521+
),
522+
true
523+
);
524+
}
525+
526+
/**
527+
* @dataProvider queryParametersHandlingForOtherRequestMethodsDataProvider
528+
*
529+
* @param string $request_method Request method.
530+
*/
531+
public function testQueryParametersHandlingForOtherRequestMethods($request_method)
532+
{
533+
$this->expectClientCall(
534+
$request_method,
535+
'/rest/api/2/something?q_p1=q_p1_v&q_p2=q_p2_v',
536+
array(
537+
'rb_p1' => 'rb_p1_v',
538+
'rb_p2' => 'rb_p2_v',
539+
'rb_p3' => 'rb_p3_v',
540+
),
541+
'{}'
542+
);
543+
544+
$this->api->api(
545+
$request_method,
546+
'/rest/api/2/something',
547+
array(
548+
'_query' => array('q_p1' => 'q_p1_v', 'q_p2' => 'q_p2_v'),
549+
'rb_p1' => 'rb_p1_v',
550+
'rb_p2' => 'rb_p2_v',
551+
'rb_p3' => 'rb_p3_v',
552+
),
553+
true
554+
);
555+
}
556+
557+
public static function queryParametersHandlingForOtherRequestMethodsDataProvider()
558+
{
559+
return array(
560+
'delete' => array(Api::REQUEST_DELETE),
561+
'post' => array(Api::REQUEST_POST),
562+
'put' => array(Api::REQUEST_PUT),
563+
);
564+
}
565+
498566
/**
499567
* Expects a particular client call.
500568
*

0 commit comments

Comments
 (0)