move curl logic into own doRequest function

This commit is contained in:
Martz
2023-02-24 21:48:27 +01:00
parent 9c67639d2f
commit 47699576a4

View File

@@ -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));
}
/**
@@ -125,21 +88,7 @@ class Collection
*/
public function delete(string $recordId, 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, 'DELETE');
$output = curl_exec($ch);
curl_close($ch);
$this->doRequest($this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'DELETE');
}
/**
@@ -148,7 +97,7 @@ class Collection
* @param string $method
* @return bool|string
*/
public function doRequest(string $recordId, string $url, string $method)
public function doRequest(string $url, string $method, $bodyParams = [])
{
$ch = curl_init();
@@ -160,6 +109,10 @@ class Collection
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
if ($bodyParams) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyParams);
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
@@ -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'];
}
}