You are here

public function SDKConnector::testConnection in Apigee Edge 8

Test connection with the Edge Management Server.

Parameters

\Drupal\key\KeyInterface|null $key: Key entity to check connection with Edge, if NULL, then use the stored key.

Throws

\Exception

Overrides SDKConnectorInterface::testConnection

File

src/SDKConnector.php, line 268

Class

SDKConnector
Provides an Apigee Edge SDK connector.

Namespace

Drupal\apigee_edge

Code

public function testConnection(KeyInterface $key = NULL) {
  if ($key !== NULL) {
    $credentials = $this
      ->buildCredentials($key);
    $client = $this
      ->buildClient($credentials
      ->getAuthentication(), $credentials
      ->getKeyType()
      ->getEndpoint($credentials
      ->getKey()));
  }
  else {
    $client = $this
      ->getClient();
    $credentials = $this
      ->getCredentials();
  }
  try {

    // We use the original, non-decorated organization controller here.
    $oc = new OrganizationController($client);

    /* @var \Apigee\Edge\Api\Management\Entity\Organization $org */
    $org = $oc
      ->load($credentials
      ->getKeyType()
      ->getOrganization($credentials
      ->getKey()));

    // Calling an invalid endpoint under some circumstances might return an
    // empty organization object, so we check if it indeed loaded an org.
    // @see https://github.com/apigee/apigee-edge-drupal/issues/250
    if (empty($org
      ->id())) {
      throw new InvalidArgumentException('Failed to load a valid organization.');
    }
  } catch (\Exception $e) {
    throw $e;
  } finally {
    if (isset($original_credentials)) {
      self::$credentials = $this
        ->setCredentials($original_credentials);
    }
  }
}