You are here

protected function ApiSync::getStandardForms in FormAssembly 8

Helper method for getting forms from the standard 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::getStandardForms()
ApiSync::getForms in src/ApiSync.php
Get data about available forms.

File

src/ApiSync.php, line 273

Class

ApiSync
Service class for FormAssembly API: Handles form sync.

Namespace

Drupal\formassembly

Code

protected function getStandardForms() {
  $url = $this
    ->getUrl('forms');
  $url
    ->setOptions([
    'query' => [
      'access_token' => $this->authorize
        ->getToken(),
    ],
  ]);
  $request = $this->httpClient
    ->get($url
    ->toUriString());

  // 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.');
  }

  // Process response:
  $response = json_decode($request
    ->getBody(), 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);
  return TRUE;
}