How to programmically render and publish all methods associated with an API Model, during the API Model creation process

Not applicable

Referring to this previous question.

Instead of redirecting the user to the Method Admin page, it is preferred all the methods are rendered and published automatically.
What code would be needed to complete this process?

I have been looking through the code for "DeveloperPortal\profiles\apigee\modules\custom\devconnect\smartdocs\smartdocs.admin.inc" and

"DeveloperPortal\profiles\apigee\modules\custom\devconnect\smartdocs\batch\smartdocs.render.inc".

It looks like the forms for rendering methods call the smartdocs_render_node (not smartdocs_render_nodes ) function, but I can not find the definition for this function.

Any help would be appreciated.

Thank you for your time,

Mitchel

Solved Solved
2 15 432
1 ACCEPTED SOLUTION

@Mitchel Skees ,

Great Question,

Take a look at the custom module smartdocs services developed by @Yuriy that does exactly same, "publish & render" through an API.

I believe you can get the code that you are looking for from above module / install the module in your portal & call the method from your custom module to integrate same.

Hope it helps.

View solution in original post

15 REPLIES 15

@Mitchel Skees ,

Great Question,

Take a look at the custom module smartdocs services developed by @Yuriy that does exactly same, "publish & render" through an API.

I believe you can get the code that you are looking for from above module / install the module in your portal & call the method from your custom module to integrate same.

Hope it helps.

Hey @Anil Sagar,

I've been trying to use a mix of the code you provided and the code already in place for the site I'm working on:

<?php
$model->load();
$revision_id = $model->getLatestRevisionNumber();
// Load our active revision.
$rev = new Revision($model->getConfig(), $model->getUuid());
$rev->load($revision_id);      

$form_state['values']['model'] = $model->toArray();
$form_state['values']['operation'] = 'render_publish_nodes';
$form_state['values']['methods'] = array();
$form_state['values']['revision'] = $rev;          
$revision_array = smartdocs_prepare_revision($rev);

foreach ($revision_array['resources'] as $resource) {
    foreach ($resource['methods'] as $method) {
        $form_state['values']['methods'][$method['id']] = $method['id'];
    }
}

//Render and Publish the API's Method Nodes
smartdocs_revision_detail_submit(array(), $form_state);

?>

However, when executing this code doing the model creation process, the model's methods are not rendered.

Is there another step needed to create the Methods and then publish them?

Do I need to do something with the batch operations?

Thank you for your time,
Mitchel

Hey @Anil Sagar or @Yuriy,

Do you have any suggested fixes for this?

Thank you for your time,

Mitchel

@Mitchel Skees , Unfortunately, Did not get a chance to dig deep, I will keep you posted if i find any. No ETA as of now.

Hey @Anil Sagar and @Yuriy,

Turns out the batch opertations were necessary to render and publish the resources of an API.

Here is the revised code:

<?php
//Load the Model - usually you would pass the UUID of the model into the load function
$model->load();
$revision_id = $model->getLatestRevisionNumber();
// Load our active revision.
$rev = new Revision($model->getConfig(), $model->getUuid());
$rev->load($revision_id);      

$form_state['values']['model'] = $model->toArray();
$form_state['values']['operation'] = 'render_publish_nodes';
$form_state['values']['methods'] = array();
$form_state['values']['revision'] = $rev;          
$revision_array = smartdocs_prepare_revision($rev);

foreach ($revision_array['resources'] as $resource) {
    foreach ($resource['methods'] as $method) {
        $form_state['values']['methods'][$method['id']] = $method['id'];
    }
}

//Render and Publish the API's Method Nodes
try {
    smartdocs_revision_detail_submit($form, $form_state);

    //Queued by the UI not drush or cron
    //If this code is run by drush or cron please see the project Anil mentioned
    $batch = &batch_get();
    $batch['progressive'] = FALSE;
    batch_set($batch);
    $dest = drupal_get_destination();
    batch_process($dest['destination']);
} catch (Exception $e){
    throw $e;
}


/* Please note the closing php code symbol below is not recommended on drupal sites, however, for formatting purposes it is displayed here. */ 
?><br>

I would like to mention, the code we are using for our site was designed primarily by @Tamás Nagy. He did a great job pulling that off for us. Perhaps later on he could write up how he automated the creation of API Models for the community's benefit.

Thanks again, Tamas!

Sincerely,

Mitchel

@Mitchel Skees , Anytime, Glad your query is resolved.

Feel free to contribute a module / write an article in Apigee community. I am sure many will use your module and give you feedback. Thank you again for great questions & solutions.

Hello Anil Sagar, I am using smartdocs 7.x-5.1 on prim version and I am getting error @ smartdocs_prepare_revision($rev); Fatal error: Call to undefined function smartdocs_prepare_revision()

Is this fuction removed from smartdocs 7.x-5.1 ??

Hey Atul,

You may need to add "include" statements at the top of the script.

use Apigee\Exceptions\ResponseException;
use Apigee\SmartDocs\Model;
use Apigee\SmartDocs\Revision;
use Apigee\ManagementAPI\APIProxy;
require_once drupal_get_path('module', 'smartdocs') . "/smartdocs.admin.inc";

Thank you for your time,

Mitchel

is it possible to get smartdocs_prepare_revision function() details ?? smartdocs 7.x-5.1 does not have smartdocs_prepare_revision function() details and your code is using smartdocs_prepare_revision function(). I am still getting same error.

Hello Mitchel, thank you for quick response.

I have included following lines

use Apigee\Exceptions\ResponseException; 

use Apigee\SmartDocs\Model; 

use Apigee\SmartDocs\Revision; 

use Apigee\ManagementAPI\APIProxy; 

require_once drupal_get_path('module', 'smartdocs') . "/smartdocs.admin.inc";

But I am getting same result

Fatal error: Call to undefined function smartdocs_prepare_revision()

I have downloaded smartdocs service drupal module and setup as per readme.

I am using swagger json Instead of yaml file. when I am calling service from postman, php code is looking for smartdocs_prepare_revision() function. I am using apigee smartdocs 7.x-5.1

I am getting Fatal error: Call to undefined function smartdocs_prepare_revision() in local_path\sites\all\modules\smartdocs_service\smartdocs_service.inc

my question is: do we have smartdocs_prepare_revision() function available in apigee smartdocs 7.x-5.1 ??

Not applicable

is it possible to get smartdocs_prepare_revision function() details ?? smartdocs 7.x-5.1 does not have smartdocs_prepare_revision function() details and your code is using smartdocs_prepare_revision function(). I am still getting same error.

Hey @atul.parmar, could you open a whole new question to bring attention to this? And I would suggest referencing this page and include your code. That would give everyone a chance to see what was causing the error.

Thank you for your time,

Mitchel

Hey @atul.parmar,

I was using version 7x-5.9.

Thank you for your time,

Mitchel

/* Function copied directly from \profiles\apigee\modules\custom\devconnect\smartdocs\smartdocs.module */

/**
 * Strips out extraneous info from a revision to streamline batching.
 *
 * @param array|Revision $revision
 *   Incoming revision (or its array representation) from which extra cruft
 *   is to be stripped.
 *
 * @return array
 *   A minimized representation of the revision.
 */
function smartdocs_prepare_revision($revision) {
  if ($revision instanceof Revision) {
    $revision = $revision->toArray();
  }
  $revision_array = array(
    'id' => $revision['id'],
    'revisionNumber' => $revision['revisionNumber'],
    'resources' => array(),
  );
  foreach ($revision['resources'] as $resource) {
    $resource_array = array(
      'id' => $resource['id'],
      'name' => $resource['name'],
      'displayName' => empty($resource['displayName']) ? $resource['name'] : $resource['displayName'],
      'baseUrl' => $resource['baseUrl'],
      'path' => $resource['path'],
      'methods' => array(),
    );
    foreach ($resource['methods'] as $method) {
      $method_array = array(
        'description' => $method['description'],
        'id' => $method['id'],
        'displayName' => empty($method['displayName']) ? $method['name'] : $method['displayName'],
        'name' => $method['name'],
        'parameters' => $method['parameters'],
        'path' => $method['path'],
        'response' => $method['response'],
        'security' => $method['security'],
        'tags' => $method['tags'],
        'verb' => $method['verb'],
        'body' => $method['body'],
      );
      $resource_array['methods'][] = $method_array;
    }
    $revision_array['resources'][] = $resource_array;
  }
  return $revision_array;
}<br>

Not applicable

I am able to fix the issue by upgrading smart-doc version. Now create model/import/render/publish all publish thr' restful services.