From 47699576a4c548a9bdf79ef132c6dec3356e523f Mon Sep 17 00:00:00 2001 From: Martz Date: Fri, 24 Feb 2023 21:48:27 +0100 Subject: [PATCH] move curl logic into own doRequest function --- src/Collection.php | 114 ++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 85 deletions(-) diff --git a/src/Collection.php b/src/Collection.php index bee1dda..00c3906 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -20,7 +20,7 @@ class Collection /** * @var string */ - private static string $token = ''; + public static string $token = ''; /** * @param string $url @@ -32,6 +32,7 @@ class Collection $this->collection = $collection; } + /** * @param int $batch * @param array $queryParams @@ -39,30 +40,23 @@ class Collection */ public function getFullList(int $batch = 200, array $queryParams = []) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records"); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - if (self::$token != '') { - $headers = array( - 'Content-Type:application/json', - 'Authorization: ' . self::$token - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } - $output = curl_exec($ch); - curl_close($ch); + $getParams = !empty($queryParams) ? http_build_query($queryParams) : ""; + $response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET'); - return json_decode($output, JSON_FORCE_OBJECT); + return json_decode($response, JSON_FORCE_OBJECT); } /** * @param string $filter * @param array $queryParams - * @return void + * @return array */ public function getFirstListItem(string $filter, array $queryParams = []) { - + $queryParams['perPage'] = 1; + $getParams = !empty($queryParams) ? http_build_query($queryParams) : ""; + $response = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records?" . $getParams, 'GET'); + return json_decode($response, JSON_FORCE_OBJECT)['items'][0]; } /** @@ -72,23 +66,7 @@ class Collection */ public function create(array $bodyParams = [], array $queryParams = []) { - $ch = curl_init(); - - if (self::$token != '') { - $headers = array( - 'Content-Type:application/json', - 'Authorization: ' . self::$token - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } - - curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records"); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams)); - $output = curl_exec($ch); - var_dump($output); - curl_close($ch); + $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records", 'POST', json_encode($bodyParams)); } /** @@ -99,23 +77,8 @@ class Collection */ public function update(string $recordId, array $bodyParams = [], array $queryParams = []) { - $ch = curl_init(); - - if (self::$token != '') { - $headers = array( - 'Content-Type:application/json', - 'Authorization: ' . self::$token - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); - } - - curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); - curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams)); - $output = curl_exec($ch); - var_dump($output); - curl_close($ch); + // Todo bodyParams equals json, currently workaround + $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'PATCH', json_encode($bodyParams)); } /** @@ -124,6 +87,17 @@ class Collection * @return void */ public function delete(string $recordId, array $queryParams = []) + { + $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE'); + } + + /** + * @param string $recordId + * @param string $url + * @param string $method + * @return bool|string + */ + public function doRequest(string $url, string $method, $bodyParams = []) { $ch = curl_init(); @@ -135,29 +109,8 @@ class Collection curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } - curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); - $output = curl_exec($ch); - curl_close($ch); - } - - /** - * @param string $recordId - * @param string $url - * @param string $method - * @return bool|string - */ - public function doRequest(string $recordId, string $url, string $method) - { - $ch = curl_init(); - - if (self::$token != '') { - $headers = array( - 'Content-Type:application/json', - 'Authorization: ' . self::$token - ); - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + if ($bodyParams) { + curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams); } curl_setopt($ch, CURLOPT_URL, $url); @@ -176,7 +129,7 @@ class Collection */ public function getOne(string $recordId, array $queryParams = []) { - $output = $this->doRequest($recordId, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET'); + $output = $this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET'); return json_decode($output, JSON_FORCE_OBJECT); } @@ -187,18 +140,9 @@ class Collection */ public function authAsAdmin(string $email, string $password) { - $ch = curl_init(); - $bodyParams['identity'] = $email; $bodyParams['password'] = $password; - - curl_setopt($ch, CURLOPT_URL, $this->url . "/api/admins/auth-with-password"); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); - curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams); - $output = curl_exec($ch); - $json = json_decode($output, JSON_FORCE_OBJECT); - self::$token = $json['token']; - curl_close($ch); + $output = $this->doRequest($this->url . "/api/admins/auth-with-password", 'POST', $bodyParams); + self::$token = json_decode($output, true)['token']; } }