Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to upload multiple smartDocs json swagger files in a one-go on dev portal

Not applicable

Hi,

Say if we have multiple swagger.json files to upload through smartdocs module on apigee portal, it becomes quite tedious to upload, render and publish them one by one manually.

Need an approach to automate this process so that we can upload all swagger files in one-go on an Edge organization and can render and publish corresponding nodes on apigee portal.

Also observed, we have two samples models viz. 'Pet Store Example API' and 'Weather Example API', which are uploaded by default automatically when an new apigee protal is provisioned. Codebase path for these sample models found at

{org-name}\profiles\apigee\modules\custom\devconnect\smartdocs\samples\petstore.swagger.json

Just curious to know if we put all our json swagger files at above path likewise, what next it takes to configure smartDoc module, so as to save these files on edge account and render and publish them all together on portal. Or if you have some other approach please suggest, will be highly thankful for that.

br,

Vishal

Solved Solved
3 5 484
1 ACCEPTED SOLUTION

Not applicable

HI @Vishal Bhatnagar

For each Swagger model from your , you need to create an API model and import the swagger to render the methods. Please refer to the functions used in apigee profile ( profiles/apigee/apigee.install_callbacks.inc )

To create model - apigee_batch_smartdocs_create_model($model_machine_name, $model_display_name, $model_description)

To import Swagger/Wadl -

apigee_batch_smartdocs_import_model($model_machine_name, $model_import_file, $document_format, $content_type)


/**
 * Batch operation callback.
 */
function apigee_batch_smartdocs_create_model($model_machine_name, $model_display_name, $model_description) {
  // Enable SmartDocs module.
  if (!module_exists('smartdocs')) {
    module_enable(array('smartdocs'), TRUE);
  }


  // Create sample SmartDocs  model.
  $model = new Apigee\SmartDocs\Model(devconnect_default_org_config());
  try {
    $model->load($model_machine_name);
    $update = TRUE;
  }
  catch (Apigee\Exceptions\ResponseException $e) {
    $update = FALSE;
  }


  // If the model loaded, then no reason to modify it, just return.
  if ($update) {
    return;
  }


  $model->setName($model_machine_name);
  $model->setDisplayName($model_display_name);
  $model->setDescription($model_description);
  try {
    $model->save($update);
  }
  catch (Apigee\Exceptions\ResponseException $e) {
    $message = $e->getResponse();
    $message_obj = @json_decode($message, TRUE);
    if (is_array($message_obj) && array_key_exists('message', $message_obj)) {
      $err_msg = $message_obj['message'];
    }
    else {
      $err_msg = $e->getMessage();
    }
    $msg_args = array('%model' => $model_display_name, '!error_message' => $err_msg);


    drupal_set_message(t('Error creating %model: !error_message', $msg_args), 'error');
    watchdog('apigee', 'Error creating %model: !error_message', $msg_args, WATCHDOG_ERROR);


  }
  catch (Exception $e) {
    $msg_args = array('%model' => $model_display_name, '!error_message' => $e->getMessage());


    drupal_set_message(t('Error creating %model: !error_message', $msg_args), 'error');
    watchdog('apigee', 'Error creating %model: !error_message', $msg_args, WATCHDOG_ERROR);
  }
}


/**
 * Batch Operation Callback.
 */
function apigee_batch_smartdocs_import_model($model_machine_name, $model_import_file, $document_format, $content_type) {
  // Create sample SmartDocs  model.
  $model = new Apigee\SmartDocs\Model(devconnect_default_org_config());
  try {
    $model->load($model_machine_name);
  }
  catch (Apigee\Exceptions\ResponseException $e) {
    $message = $e->getResponse();
    $message_obj = @json_decode($message, TRUE);
    if (is_array($message_obj) && array_key_exists('message', $message_obj)) {
      $err_msg = $message_obj['message'];
    }
    else {
      $err_msg = $e->getMessage();
    }
    $msg_args = array('%model' => $model_machine_name, '!error_message' => $err_msg);
    drupal_set_message(t('Error importing %model: !error_message', $msg_args), 'error');
    watchdog('apigee', 'Error importing %model: !error_message', $msg_args, WATCHDOG_ERROR);
  }
  catch (Exception $e) {
    $msg_args = array('%model' => $model_machine_name, '!error_message' => $e->getMessage());
    drupal_set_message(t('Error importing %model: !error_message', $msg_args), 'error');
    watchdog('apigee', 'Error importing %model: !error_message', $msg_args, WATCHDOG_ERROR);
  }
  if ($model->getLatestRevisionNumber() <= 0) {
    try {
      // Import the model using the Swagger file.
      $file_contents = file_get_contents($model_import_file);
      $model->importFile($file_contents, $document_format, $content_type);
      drupal_set_message('Sample model %model imported into SmartDocs.', array('%model' => $model->getDisplayName()), 'status');
    }
    catch (Apigee\Exceptions\ResponseException $e) {
      $message = $e->getResponse();
      $message_obj = @json_decode($message, TRUE);
      if (is_array($message_obj) && array_key_exists('message', $message_obj)) {
        $err_msg = $message_obj['message'];
      }
      else {
        $err_msg = $e->getMessage();
      }
      drupal_set_message(t('Error importing %model: !error_message.', array('%model' => $model_machine_name, '!error_message' => $err_msg)), 'error');
      watchdog('apigee', 'Error importing %model: !error_message.', array('%model' => $model_machine_name, '!error_message' => $err_msg), WATCHDOG_ERROR);
    }
    catch (Exception $e) {
      drupal_set_message(t('Error importing %model: !error_message.', array('%model' => $model_machine_name, '!error_message' => $e->getMessage())), 'error');
      watchdog('apigee', 'Error importing %model: !error_message.', array('%model' => $model_machine_name, '!error_message' => $e->getMessage()), WATCHDOG_ERROR);
    }
  }
}

You can re use the code in your custom module in which you are going to loop all of your swagger files.

View solution in original post

5 REPLIES 5