Skip to content

Commit db52956

Browse files
committed
Refactor api method
1 parent ff21b13 commit db52956

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

src/Client.php

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,11 @@ protected function buildUrl($endpoint, $params)
481481
*/
482482
public function api($endpoint, array $params = [], $method = Method::GET)
483483
{
484-
$options = [];
485484
$headers = $this->getApiHeaders();
485+
$options = $this->prepareOptions($params, $method);
486+
if (!in_array($method, [Method::GET, Method::POST])) {
487+
throw new \InvalidArgumentException('The method is not correct');
488+
}
486489
if ($this->isUsingTokenParam()) {
487490
$params['oauth2_access_token'] = $this->accessToken->getToken();
488491
} else {
@@ -492,17 +495,8 @@ public function api($endpoint, array $params = [], $method = Method::GET)
492495
'base_uri' => $this->getApiRoot(),
493496
'headers' => $headers,
494497
]);
495-
switch ($method) {
496-
case Method::GET:
497-
if (!empty($params)) {
498-
$endpoint .= '?' . build_query($params);
499-
}
500-
break;
501-
case Method::POST:
502-
$options['body'] = \GuzzleHttp\json_encode($params);
503-
break;
504-
default:
505-
throw new \InvalidArgumentException('The method is not correct');
498+
if (!empty($params) && Method::GET === $method) {
499+
$endpoint .= '?' . build_query($params);
506500
}
507501
try {
508502
$response = $guzzle->request($method, $endpoint, $options);
@@ -539,4 +533,18 @@ public function post($endpoint, array $params = [])
539533
{
540534
return $this->api($endpoint, $params, Method::POST);
541535
}
536+
537+
/**
538+
* @param array $params
539+
* @param $method
540+
* @return mixed
541+
*/
542+
protected function prepareOptions(array $params, $method)
543+
{
544+
$options = [];
545+
if ($method === Method::POST) {
546+
$options['body'] = \GuzzleHttp\json_encode($params);
547+
}
548+
return $options;
549+
}
542550
}

0 commit comments

Comments
 (0)