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 419
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

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.

Hi Seshi,

Thank for your reply.

Followed below steps :-

Step 1: - As 'apigee.install_callbacks.inc' get invoked from 'apigee.profile', so added entries for my swagger files in 'apigee.profile', same way as it is done for 'pet' and 'weather' samples examples.

Step 2:- Placed my swagger file at path ...\profiles\apigee\modules\custom\devconnect\smartdocs\samples\

Step 3:- Started the profile installation using url http://dev-{org-name}.devportal.apigee.com/install.php?profile=apigee&locale=en

[had already wiped off the database and file folder for new installation]

All went good but one below error occurred for rendering my swagger file- (file got imported on edge but issue is only with rendering)

"Error rendering SmartDocs methods for vishalstore_example: API Revision 1 is not found for Organization vishal-psl and API 611f3526-56cf-4c2b-b97f-1126d67c8c9a."

Swagger file got listed on UI (Admin bar->Content->smartdocs->{list}) along with other swagger APIs(pet and weather) but clicking on my API throws below error -

"Error loading revision 1 of model vishalstore_example . Edge Management server response: { "code" : "apimodel.ApiRevisionNotFound", "message" : "API Revision 1 is not found for Organization vishal-psl and API 611f3526-56cf-4c2b-b97f-1126d67c8c9a.", "contexts" : [ ] }"

It can be inferred from the error msg that it is somehow related to "Revision #" which is not incrementing from Revision#0 to Revision#1.

Any help on this is highly appreciable.

br,

Vishal Bhatnagar

Hi @Vishal Bhatnagar

Let me clarify that any changes or customization to devportal should only be done in sites/all/* folder path.

Step 1: Add your custom module to sites/all/modules/custom/ to implement the batch creation of API models and render them.

Step 2: Place your swagger files in your custom module folder e.g sites/all/modules/custom/smartdcos_batch/swagger/*.json

Step 3: make a copy of the above mentioned functions in your module include file and call them on each swagger file in a loop as a batch process.

Assumption : Good module development skills in Drupal.

Not applicable

Hi @seshi

Could you share your suggestions on above comment.

Hi @Seshi,

Thanks for your guidance!

Created a custom module, looped in swagger files using batch api and invoked above mentioned callback functions. Now models are getting created, imported, rendered and even getting published 🙂

However, when we traverse to any APIs resource URL, it shows a page without smartDocs template implemented.

Please see image below :

1846-smartdocsnotemplate.jpg

We do not see request, response, header, parameter etc constructs on this page.

Also checked smartDocs template(Admin menu->SmartDocs-> select any model settings->Template) and it is also set to default template.

Could you please share more insight on this, how to implement smartdocs templates for published pages within our custom module.