Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding in non-default option for returning response object #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 58 additions & 30 deletions src/DataApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace RCConsulting\FileMakerApi;

use RCConsulting\FileMakerApi\Exception\Exception;
use RCConsulting\FileMakerApi\Response;

/**
* Class DataApi
Expand All @@ -27,6 +28,7 @@ final class DataApi implements DataApiInterface
protected $oAuthRequestId = null;
protected $oAuthIdentifier = null;
protected $hasToken = False;
protected $returnResponseObject = False;

/**
* DataApi constructor
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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'];
}
}

/**
Expand Down Expand Up @@ -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'];
}
}

/**
Expand Down Expand Up @@ -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'];
}
}

/**
Expand Down Expand Up @@ -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];
}
}

/**
Expand Down Expand Up @@ -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();
}
}

/**
Expand Down Expand Up @@ -382,7 +401,6 @@ public function uploadToContainer(string $layout, $recordId, string $containerFi
]
]
);

return True;
}

Expand Down Expand Up @@ -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();
}
}

/**
Expand All @@ -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 = [];
Expand All @@ -507,7 +528,7 @@ public function executeScript(string $layout, string $scriptName, string $script
];
} else {
$options = [
'headers' => $this->getDefaultHeaders()
'headers' => $this->getDefaultHeaders()
];
}

Expand All @@ -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'];
}
}

Expand All @@ -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
Expand Down Expand Up @@ -856,7 +884,7 @@ public function getProductInfo()
'json' => []
]
);
return $response->getBody()['response'];
return $response->getRawResponse();
}

/**
Expand All @@ -874,7 +902,7 @@ public function getDatabaseNames()
'json' => []
]
);
return $response->getBody()['response'];
return $response->getRawResponse();
}

/**
Expand All @@ -892,7 +920,7 @@ public function getLayoutNames()
'json' => []
]
);
return $response->getBody()['response'];
return $response->getRawResponse();
}

/**
Expand All @@ -910,7 +938,7 @@ public function getScriptNames()
'json' => []
]
);
return $response->getBody()['response'];
return $response->getRawResponse();
}

/**
Expand Down Expand Up @@ -941,7 +969,7 @@ public function getLayoutMetadata(string $layout, $recordId = null)
),
]
);
return $response->getBody()['response'];
return $response->getRawResponse();
}

/**
Expand Down
53 changes: 53 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ final class Response
* @var array
*/
private $body = [];
/**
* @var array
*/
private $response = [];
/**
* @var array
*/
private $records = [];
/**
* @var int
*/
Expand All @@ -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];
}
Expand Down Expand Up @@ -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
*
Expand Down