From 5838d443de09b5ffa4644883cdb29356859c56c3 Mon Sep 17 00:00:00 2001 From: Jon Eisen Date: Fri, 17 Sep 2021 15:04:30 -0700 Subject: [PATCH 1/3] Adding in non-default option for returning response object This should not break any existing code. It defaults to the existing behavior but allows you to set an object level property at object creation time to return response objects rather than strings and arrays from most of the class functions. --- src/DataApi.php | 88 +++++++++++++++++++++++++++++++----------------- src/Response.php | 53 +++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 30 deletions(-) diff --git a/src/DataApi.php b/src/DataApi.php index dd869cd..be7dfcd 100644 --- a/src/DataApi.php +++ b/src/DataApi.php @@ -3,6 +3,7 @@ namespace RCConsulting\FileMakerApi; use RCConsulting\FileMakerApi\Exception\Exception; +use RCConsulting\FileMakerApi\Response; /** * Class DataApi @@ -27,6 +28,7 @@ final class DataApi implements DataApiInterface protected $oAuthRequestId = null; protected $oAuthIdentifier = null; protected $hasToken = False; + protected $returnResponseObject = False; /** * DataApi constructor @@ -40,10 +42,11 @@ final class DataApi implements DataApiInterface * * @throws Exception */ - public function __construct(string $apiUrl, string $apiDatabase, string $apiUser = null, string $apiPassword = null, bool $sslVerify = True, bool $forceLegacyHTTP = False) + public function __construct(string $apiUrl, string $apiDatabase, string $apiUser = null, string $apiPassword = null, bool $sslVerify = True, bool $forceLegacyHTTP = False, bool $returnResponseObject = False) { $this->apiDatabase = $this->prepareURLpart($apiDatabase); $this->ClientRequest = new CurlClient($apiUrl, $sslVerify, $forceLegacyHTTP); + $this->returnResponseObject = $returnResponseObject; if (!is_null($apiUser)) { $this->login($apiUser, $apiPassword); @@ -71,7 +74,7 @@ public function login(string $apiUsername, string $apiPassword) 'json' => [], ] ); - $this->setApiToken($response->getBody()['response']['token']); + $this->setApiToken($response->getRawResponse()['token']); $this->storeCredentials($apiUsername, $apiPassword); return $this; @@ -98,7 +101,7 @@ public function loginOauth(string $oAuthRequestId, string $oAuthIdentifier) 'json' => [], ] ); - $this->setApiToken($response->getBody()['response']['token']); + $this->setApiToken($response->getRawResponse()['token']); $this->storeOAuth($oAuthRequestId, $oAuthIdentifier); return $this; @@ -156,8 +159,11 @@ public function createRecord(string $layout, array $data, array $scripts = [], a ), ] ); - - return $response->getBody()['response']['recordId']; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRecords()['response']['recordId']; + } } /** @@ -194,8 +200,11 @@ public function editRecord(string $layout, $recordId, array $data, $lastModifica ), ] ); - - return $response->getBody()['response']['modId']; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRawResponse()['modId']; + } } /** @@ -223,7 +232,11 @@ public function duplicateRecord(string $layout, $recordId, array $scripts = []) ), ] ); - return $response->getBody()['response']['recordId']; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRawResponse()['recordId']; + } } /** @@ -292,8 +305,11 @@ public function getRecord(string $layout, $recordId, array $portalOptions = [], ), ] ); - - return $response->getBody()['response']['data'][0]; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRecords()[0]; + } } /** @@ -343,8 +359,11 @@ public function getRecords(string $layout, $sort = null, $offset = null, $limit ), ] ); - - return $response->getBody()['response']['data']; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRecords(); + } } /** @@ -382,7 +401,6 @@ public function uploadToContainer(string $layout, $recordId, string $containerFi ] ] ); - return True; } @@ -474,8 +492,11 @@ public function findRecords(string $layout, array $query, $sort = null, $offset throw $e; } } - - return $response->getBody()['response']['data']; + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getRecords(); + } } /** @@ -492,7 +513,7 @@ public function executeScript(string $layout, string $scriptName, string $script { $layout = $this->prepareURLpart($layout); $scriptName = $this->prepareURLpart($scriptName); - + if (!empty($scriptParam)){ // Prepare options $queryParams = []; @@ -507,7 +528,7 @@ public function executeScript(string $layout, string $scriptName, string $script ]; } else { $options = [ - 'headers' => $this->getDefaultHeaders() + 'headers' => $this->getDefaultHeaders() ]; } @@ -517,14 +538,18 @@ public function executeScript(string $layout, string $scriptName, string $script "/v1/databases/$this->apiDatabase/layouts/$layout/script/$scriptName", $options ); - if ($response->getBody()['response']['scriptError'] == 0) { - if (array_key_exists('scriptResult', $response->getBody()['response'])) { - return $response->getBody()['response']['scriptResult']; + if ($this->returnResponseObject) { + return $response; + } else { + if ($response->getScriptError() == 0) { + if (array_key_exists('scriptResult', $response->getRawResponse())) { + return $response->getScriptResult(); + } else { + return True; + } } else { - return True; + return $response->getScriptError(); } - } else { - return $response->getBody()['response']['scriptError']; } } @@ -550,8 +575,11 @@ public function setGlobalFields(string $layout, array $globalFields) ], ] ); - - return $response->getBody(); + if ($this->returnResponseObject) { + return $response; + } else { + return $response->getBody(); + } } // UTILITY FUNCTIONS @@ -856,7 +884,7 @@ public function getProductInfo() 'json' => [] ] ); - return $response->getBody()['response']; + return $response->getRawResponse(); } /** @@ -874,7 +902,7 @@ public function getDatabaseNames() 'json' => [] ] ); - return $response->getBody()['response']; + return $response->getRawResponse(); } /** @@ -892,7 +920,7 @@ public function getLayoutNames() 'json' => [] ] ); - return $response->getBody()['response']; + return $response->getRawResponse(); } /** @@ -910,7 +938,7 @@ public function getScriptNames() 'json' => [] ] ); - return $response->getBody()['response']; + return $response->getRawResponse(); } /** @@ -941,7 +969,7 @@ public function getLayoutMetadata(string $layout, $recordId = null) ), ] ); - return $response->getBody()['response']; + return $response->getRawResponse(); } /** diff --git a/src/Response.php b/src/Response.php index cc33561..1f3e8b1 100644 --- a/src/Response.php +++ b/src/Response.php @@ -18,6 +18,14 @@ final class Response * @var array */ private $body = []; + /** + * @var array + */ + private $response = []; + /** + * @var array + */ + private $records = []; /** * @var int */ @@ -35,6 +43,11 @@ public function __construct(array $headers, array $body) { $this->headers = $headers; $this->body = $body; + $this->response = $this->body['response']; + //checks to see if data even exists in this response type, logins do not, for example + if (isset($this->response['data']) || array_key_exists('data', $this->response)) { + $this->records = $this->response['data']; + } // parses "HTTP/1.1 200 OK" and returns the 200 $this->responseCodeHTTP = (int)explode(" ", $this->getHeader("Status"))[1]; } @@ -82,6 +95,46 @@ public function getBody() return $this->body; } + /** + * @return array + */ + public function getRecords() + { + return $this->records; + } + + /** + * @return array + */ + public function getRawResponse() + { + return $this->response; + } + + /** + * @return string + */ + public function getScriptResult() + { + if (isset($this->response['scriptResult']) || array_key_exists('scriptResult', $this->response)){ + return $this->response['scriptResult']; + } else { + return; + } + } + + /** + * @return string + */ + public function getScriptError() + { + if (isset($this->response['scriptError']) || array_key_exists('scriptError', $this->response)){ + return $this->response['scriptError']; + } else { + return; + } + } + /** * @param string $headers * From 4518f1a648469c7092bfd3216a96e2a4c05baf43 Mon Sep 17 00:00:00 2001 From: Jon Eisen Date: Fri, 14 Apr 2023 11:17:37 -0700 Subject: [PATCH 2/3] Returns an empty string if none is available. Fixes PR comments --- src/Response.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Response.php b/src/Response.php index 1f3e8b1..1ebdc14 100644 --- a/src/Response.php +++ b/src/Response.php @@ -119,7 +119,7 @@ public function getScriptResult() if (isset($this->response['scriptResult']) || array_key_exists('scriptResult', $this->response)){ return $this->response['scriptResult']; } else { - return; + return ''; } } @@ -131,7 +131,7 @@ public function getScriptError() if (isset($this->response['scriptError']) || array_key_exists('scriptError', $this->response)){ return $this->response['scriptError']; } else { - return; + return ''; } } From e43d761041bee55ae2c6e0652370a26cb4fa6226 Mon Sep 17 00:00:00 2001 From: Jon Eisen Date: Fri, 14 Apr 2023 11:20:53 -0700 Subject: [PATCH 3/3] Extra space...oops --- src/Response.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Response.php b/src/Response.php index 1ebdc14..9058837 100644 --- a/src/Response.php +++ b/src/Response.php @@ -131,7 +131,7 @@ public function getScriptError() if (isset($this->response['scriptError']) || array_key_exists('scriptError', $this->response)){ return $this->response['scriptError']; } else { - return ''; + return ''; } }