implement doReqeust for dublicated code on request curl

This commit is contained in:
Martz
2023-02-18 20:22:40 +01:00
parent 8b748f4d73
commit 9c67639d2f

View File

@@ -37,14 +37,15 @@ class Collection
* @param array $queryParams * @param array $queryParams
* @return mixed * @return mixed
*/ */
public function getFullList(int $batch = 200, array $queryParams = []){ public function getFullList(int $batch = 200, array $queryParams = [])
{
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records"); curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(self::$token != ''){ if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json', 'Content-Type:application/json',
'Authorization: '.self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
@@ -59,7 +60,8 @@ class Collection
* @param array $queryParams * @param array $queryParams
* @return void * @return void
*/ */
public function getFirstListItem(string $filter, array $queryParams = []){ public function getFirstListItem(string $filter, array $queryParams = [])
{
} }
@@ -68,18 +70,19 @@ class Collection
* @param array $queryParams * @param array $queryParams
* @return void * @return void
*/ */
public function create(array $bodyParams = [], array $queryParams = []){ public function create(array $bodyParams = [], array $queryParams = [])
{
$ch = curl_init(); $ch = curl_init();
if(self::$token != ''){ if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json', 'Content-Type:application/json',
'Authorization: '.self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records"); curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams));
@@ -94,18 +97,19 @@ class Collection
* @param array $queryParams * @param array $queryParams
* @return void * @return void
*/ */
public function update(string $recordId, array $bodyParams = [], array $queryParams = []){ public function update(string $recordId, array $bodyParams = [], array $queryParams = [])
{
$ch = curl_init(); $ch = curl_init();
if(self::$token != ''){ if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json', 'Content-Type:application/json',
'Authorization: '.self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records/".$recordId); curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams)); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($bodyParams));
@@ -119,18 +123,19 @@ class Collection
* @param array $queryParams * @param array $queryParams
* @return void * @return void
*/ */
public function delete(string $recordId, array $queryParams = []){ public function delete(string $recordId, array $queryParams = [])
{
$ch = curl_init(); $ch = curl_init();
if(self::$token != ''){ if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json', 'Content-Type:application/json',
'Authorization: '.self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records/".$recordId); curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
$output = curl_exec($ch); $output = curl_exec($ch);
@@ -139,26 +144,39 @@ class Collection
/** /**
* @param string $recordId * @param string $recordId
* @param array $queryParams * @param string $url
* @return mixed * @param string $method
* @return bool|string
*/ */
public function getOne(string $recordId, array $queryParams = []){ public function doRequest(string $recordId, string $url, string $method)
{
$ch = curl_init(); $ch = curl_init();
if(self::$token != ''){ if (self::$token != '') {
$headers = array( $headers = array(
'Content-Type:application/json', 'Content-Type:application/json',
'Authorization: '.self::$token 'Authorization: ' . self::$token
); );
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
} }
curl_setopt($ch, CURLOPT_URL, $this->url . "/api/collections/".$this->collection."/records/".$recordId); curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
$output = curl_exec($ch); $output = curl_exec($ch);
curl_close($ch); curl_close($ch);
return $output;
}
/**
* @param string $recordId
* @param array $queryParams
* @return mixed
*/
public function getOne(string $recordId, array $queryParams = [])
{
$output = $this->doRequest($recordId, $this->url . "/api/collections/" . $this->collection . "/records/" . $recordId, 'GET');
return json_decode($output, JSON_FORCE_OBJECT); return json_decode($output, JSON_FORCE_OBJECT);
} }