Apigee Edge Developer Guide

Last updated on
3 September 2020

This page has information to help explain how to extend or modify functionality using custom code.

Working with Apigee entities

Apigee Edge adds Drupal entities for each of the corresponding needed entities in the Apigee management APIs.

If you want to create a custom module that interacts with these entities, follow Drupal.org’s documentation on how to use the Entity API.

Another good way to get started is to download the Examples module, which has examples on Drupal best practices on using Drupal APIs:

composer require drupal/examples --optimize-autoloader

The AjaxExample module shows an example of loading an entity using the EntityTypeManager service via injection, which is the preferred approach:

  1. Add an EntityType attribute to your class
  2. Add a class constructor that takes the EntityTypeManager as a parameter
  3. Add a static create function that creates the object, passing the entityTypeManager service

Calling other Management API endpoints

The Apigee Edge module provides endpoints for the most commonly used calls to the Apigee Management API. However, in some cases a custom module requires an endpoint that is not provided by the module. In that case, custom modules can leverage the Drupal Apigee Edge client and connection settings to connect to additional Management API endpoints.

The following code snippet demonstrates how to use the Edge client to make calls from your developer portal to a new Management API Endpoint:

/** @var \Drupal\apigee_edge\SDKConnectorInterface $sdk_connector */
$sdk_connector = \Drupal::service('apigee_edge.sdk_connector');
try {
  $sdk_connector->testConnection();
  $client = $sdk_connector->getClient();
}
catch (\Exception $exception) {
  // Handle error in the connection to Apigee Edge.
}

$api_product = 'binproduct';
$query_params = [
  'query' =>'list',
  'entity' => 'apps',
];
$endpoint = $client->getUriFactory()
  ->createUri("/organizations/{$sdk_connector->getOrganization()}/apiproducts/{$api_product}")
  ->withQuery(http_build_query($query_params));
try {
  $response = $client->get($client->getEndpoint() . $endpoint);
}
catch (\Exception $exception) {
  // Handle exception.
}

$results = json_decode((string) $response->getBody());

Note: This approach is only recommended for cases where the Management API endpoints are not already accessible in Drupal through controllers, entity storage, or the apigee-client-php library!

Loading all approved apps from a developer

The following example shows how to load all approved apps from a developer, using the developer’s account details:

/** @var \Drupal\user\Entity\User $account The user. */
/** @var \Drupal\apigee_edge\Entity\Storage\DeveloperAppStorageInterface $developer_app_storage */
$developer_app_storage = \Drupal::entityTypeManager()->getStorage('developer_app');
$apps = $developer_app_storage->loadByDeveloper($account->getEmail());

$active_apps = [];

/** @var \Drupal\apigee_edge\Entity\DeveloperApp $app */
foreach ($apps as $app) {
  if ($app->getStatus() == \Drupal\apigee_edge\Entity\AppInterface::STATUS_APPROVED) {
    $active_apps[] = $app;
  }
}

Filter apps using custom attributes

Some developer portals might want to restrict the apps displayed on the site based on a custom app attribute. For example, an organization with two developer portals (A and B) may want to show apps with the custom attribute portal = portal_a on one portal, and those with portal = portal_b on the other.

The Apigee Edge API does not provide an endpoint to filter out apps based on attributes. Filtering must be done after the apps listing has been retrieved from the API. The steps below outline one example of how this can be done:

  1. Create a custom module (mymodule), with the following structure:
    mymodule
     ⊢ mymodule.info (contains the usual module declarations)
     ⊢ mymodule.module
     ⊢ src
         ⊢ Entity
             ⊢ Storage
                 ⊢ DeveloperAppStorage.php
  2. In mymodule.module, override the storage handler for the developer_app entity type:
    <?php
    
    use Drupal\mymodule\Entity\Storage\DeveloperAppStorage;
    
    /**
     * Implements hook_entity_type_alter().
     */
    function mymodule_entity_type_alter(array &$entity_types) {
    
      /** @var \Drupal\Core\Entity\EntityTypeInterface[] $entity_types */
      if (isset($entity_types['developer_app'])) {
        // Override the storage class.
              $entity_types['developer_app']->setStorageClass(DeveloperAppStorage::class);
      }
    }
    
  3. In DeveloperAppStorage.php, override the loadMultiple() method (used internally to load single or multiple apps) to apply the attribute filtering. In this example, apps belonging to Portal A have an attribute portal == portal_a. Make sure to modify the sample code to match your custom attribute naming:

    <?php
    
    namespace Drupal\mymodule\Entity\Storage;
    
    use Drupal\apigee_edge\Entity\DeveloperAppInterface;
    use Drupal\apigee_edge\Entity\Storage\DeveloperAppStorage as EdgeDeveloperAppStorage;
    
    /**
     * Overriden entity storage class for Developer app entities.
     */
    class DeveloperAppStorage extends EdgeDeveloperAppStorage {
    
      /**
       * {@inheritdoc}
       */
      public function loadMultiple(array $ids = NULL) {
        $entities = parent::loadMultiple($ids);
    
        return array_filter($entities, function (DeveloperAppInterface $app) {
          return ($app->hasAttribute('portal') && ($app->getAttributeValue('portal') == 'portal_a'));
        });
      }
    
    }
  4. Optionally, extend the save() method to ensure apps created in each portal have the custom attribute set to the appropriate value required for filtering.  For example, apps created in Portal A would be given the custom attribute "portal == portal_a".

Help improve this page

Page status: No known problems

You can: