protected function ApiSync::getAdminForms in FormAssembly 8
Helper method for getting forms from the admin index.
Throws
\Drupal\formassembly\Exception\FormAssemblyException If the request is not returned in proper form.
\UnexpectedValueException If an http return code other than 200 is received.
\Exception Any exception passed through from getting a token or the request.
1 call to ApiSync::getAdminForms()
- ApiSync::getForms in src/
ApiSync.php - Get data about available forms.
File
- src/
ApiSync.php, line 215
Class
- ApiSync
- Service class for FormAssembly API: Handles form sync.
Namespace
Drupal\formassemblyCode
protected function getAdminForms() {
$finished = FALSE;
$url = $this
->getUrl('forms');
$url
->setOptions([
'query' => [
'access_token' => $this->authorize
->getToken(),
'show' => 50,
'page' => $this->page,
],
]);
$request = $this->httpClient
->get($url
->toString(TRUE)
->getGeneratedUrl());
// Guzzle throws an Exception on http 400/500 errors.
// Ensure we have a 200.
if ($request
->getStatusCode() != 200) {
throw new \UnexpectedValueException('Http return code 200 expected. Code ' . $request
->getStatusCode() . ' received.');
}
$raw = $request
->getBody()
->getContents();
// Hash request to see if it has changed.
$current_hash = sha1($raw);
if ($this->lastHash === $current_hash || empty($raw)) {
// Observed behavior from formassembly is if n pages are needed to
// iterate the admin index page n+1 returns the same response as page n.
// But formassembly advises this could change to page n+1 returns empty.
// Neither of these responses should be processed and we are finished.
$finished = TRUE;
$this->page = 1;
}
else {
$this->lastHash = $current_hash;
// Process response:
$response = json_decode($raw, TRUE);
// If we don't get JSON there is an error.
if (json_last_error() != JSON_ERROR_NONE) {
throw new FormAssemblyException('The message body ' . $request
->getBody() . ' is not JSON');
}
$this
->processResponse($response);
// Increment page:
++$this->page;
}
return $finished;
}