SDKConnector.php in Apigee Edge 8
File
src/SDKConnector.php
View source
<?php
namespace Drupal\apigee_edge;
use Apigee\Edge\Api\Management\Controller\OrganizationController;
use Apigee\Edge\Client;
use Apigee\Edge\ClientInterface;
use Apigee\Edge\HttpClient\Utility\Builder;
use Drupal\apigee_edge\Connector\HybridCredentials;
use Drupal\apigee_edge\Exception\AuthenticationKeyException;
use Drupal\apigee_edge\Exception\AuthenticationKeyNotFoundException;
use Drupal\apigee_edge\Exception\InvalidArgumentException;
use Drupal\apigee_edge\Plugin\EdgeKeyTypeInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Http\ClientFactory;
use Drupal\key\KeyInterface;
use Drupal\key\KeyRepositoryInterface;
use Http\Adapter\Guzzle6\Client as GuzzleClientAdapter;
use Http\Message\Authentication;
class SDKConnector implements SDKConnectorInterface {
private static $client = NULL;
private static $credentials = NULL;
private static $userAgentPrefix = NULL;
protected $configFactory;
protected $keyRepository;
protected $entityTypeManager;
protected $moduleHandler;
protected $infoParser;
private $clientFactory;
public function __construct(ClientFactory $client_factory, KeyRepositoryInterface $key_repository, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, InfoParserInterface $info_parser) {
$this->clientFactory = $client_factory;
$this->entityTypeManager = $entity_type_manager;
$this->keyRepository = $key_repository;
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->infoParser = $info_parser;
}
protected function httpClientConfiguration() : array {
return [
'connect_timeout' => $this->configFactory
->get('apigee_edge.client')
->get('http_client_connect_timeout') ?? 30,
'timeout' => $this->configFactory
->get('apigee_edge.client')
->get('http_client_timeout') ?? 30,
'proxy' => $this->configFactory
->get('apigee_edge.client')
->get('http_client_proxy') ?? '',
];
}
public function getOrganization() : string {
$credentials = $this
->getCredentials();
return $credentials
->getKeyType()
->getOrganization($credentials
->getKey());
}
public function getClient(?Authentication $authentication = NULL, ?string $endpoint = NULL) : ClientInterface {
if ($authentication === NULL) {
if (self::$client === NULL) {
$credentials = $this
->getCredentials();
self::$client = $this
->buildClient($credentials
->getAuthentication(), $credentials
->getKeyType()
->getEndpoint($credentials
->getKey()));
}
return self::$client;
}
else {
return $this
->buildClient($authentication, $endpoint);
}
}
public function buildClient(Authentication $authentication, ?string $endpoint = NULL, array $options = []) : ClientInterface {
$options += [
Client::CONFIG_HTTP_CLIENT_BUILDER => new Builder(new GuzzleClientAdapter($this->clientFactory
->fromOptions($this
->httpClientConfiguration()))),
Client::CONFIG_USER_AGENT_PREFIX => $this
->userAgentPrefix(),
];
return new Client($authentication, $endpoint, $options);
}
private function getCredentials() : CredentialsInterface {
if (self::$credentials === NULL) {
$active_key = $this->configFactory
->get('apigee_edge.auth')
->get('active_key');
if (empty($active_key)) {
throw new AuthenticationKeyException('Apigee Edge API authentication key is not set.');
}
if (!($key = $this->keyRepository
->getKey($active_key))) {
throw new AuthenticationKeyNotFoundException($active_key, 'Apigee Edge API authentication key not found with "@id" id.');
}
self::$credentials = $this
->buildCredentials($key);
}
return self::$credentials;
}
private function setCredentials(CredentialsInterface $credentials) {
self::$credentials = $credentials;
self::$client = NULL;
}
private function buildCredentials(KeyInterface $key) : CredentialsInterface {
if ($key
->getKeyType() instanceof EdgeKeyTypeInterface) {
if ($key
->getKeyType()
->getInstanceType($key) === EdgeKeyTypeInterface::INSTANCE_TYPE_HYBRID) {
return new HybridCredentials($key);
}
elseif ($key
->getKeyType()
->getAuthenticationType($key) === EdgeKeyTypeInterface::EDGE_AUTH_TYPE_OAUTH) {
return new OauthCredentials($key);
}
return new Credentials($key);
}
else {
throw new AuthenticationKeyException("Type of {$key->id()} key does not implement EdgeKeyTypeInterface.");
}
}
protected function userAgentPrefix() : string {
if (NULL === self::$userAgentPrefix) {
$module_info = $this->infoParser
->parse($this->moduleHandler
->getModule('apigee_edge')
->getPathname());
if (!isset($module_info['version'])) {
$module_info['version'] = '8.x-1.x-dev';
}
self::$userAgentPrefix = $module_info['name'] . ' DevPortal ' . $module_info['version'];
}
return self::$userAgentPrefix;
}
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 {
$oc = new OrganizationController($client);
$org = $oc
->load($credentials
->getKeyType()
->getOrganization($credentials
->getKey()));
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);
}
}
}
}