API Connect

API Connect

Join this online group to communicate across IBM product users and experts by sharing advice and best practices with peers and staying up to date regarding product enhancements.

 View Only
  • 1.  Fetching Product or Apis lists

    Posted Thu December 01, 2022 08:45 PM

    I am trying to fetch the Product list or Apis list to show in some custom page.

    Do anyone know if IBM APIC drupal code provides any services or any way that I can just call and retrieve these lists. 

    I see these hooks are available but not for fetching the lists.
    hook_product_create
    hook_product_update
    hook_product_delete
     
    Any suggestions, I am trying to avoid directly calling the DB where these are stored.

    ------------------------------
    Syed Imtiyaz Alam
    ------------------------------


  • 2.  RE: Fetching Product or Apis lists

    Posted Fri December 02, 2022 03:16 AM
    The hook functions you mention are so that you can have your code triggered when events happen. Retrieving a list is obviously a completely different pattern - there is no event there. There is also no need to provide custom functions for that. Products and APIs are just nodes in the Drupal database and so you retrieve them like you would any other 'node'.

    e.g. this would retrieve a list of products:
    $query = \Drupal::entityQuery('node');
    $query->condition('type', 'product');
    $query->condition('status', 1);
    $nids = $query->execute();
    foreach (array_chunk($nids, 50) as $chunk) {
      $productNodes = Node::loadMultiple($chunk);
      foreach ($productNodes as $productNode) {
        // do something
      }
    }​
    The chunking in the above code is intentional, since you could have 100s of products, and you wouldn't want to try and load them all at once as PHP will blow up with out of memory errors.

    For convenience in APIC v10 there is a helper function if you want:
    use Drupal\product\Product;
    
    $nids = Product::listProducts();
    foreach (array_chunk($nids, 50) as $chunk) {
      $productNodes = Node::loadMultiple($chunk);
        foreach ($productNodes as $productNode) {
          // do something
      }
    }​
    That listProducts function will enforce the ACL for you - so it will only return products that your current user has permission to access for example.

    There's an equivalent for APIs too:
    use Drupal\apic_api\Api;
    
    $nids = Api::listApis();
    foreach (array_chunk($nids, 50) as $chunk) {
      $apiNodes = Node::loadMultiple($chunk);
        foreach ($apiNodes as $apiNode) {
          // do something
      }
    }​


    Hope that helps,

    Chris

    ------------------------------
    Chris Dudley
    ------------------------------



  • 3.  RE: Fetching Product or Apis lists

    Posted Fri December 02, 2022 11:20 AM
    Thank you so much Chris Dudley. Yes this is good info.

    I did find the Rest APIs those are also available.
    https://apic-api.apiconnect.ibmcloud.com/v10/?_ga=2.208814481.268178704.1669944678-1700688527.1578501197#/IBMAPIConnectPlatformProviderAPI_200/operation/%2Fcatalogs%2F{org}%2F{catalog}%2Fproducts/get

    Could you please suggest if rest API vs direct code , which is good to use.

    And I wanted to see if I can have more condition to filter specific products or apis.




    ------------------------------
    Syed Imtiyaz Alam
    ------------------------------



  • 4.  RE: Fetching Product or Apis lists

    Posted Fri December 02, 2022 11:40 AM
    Edited by Chris Dudley Fri December 02, 2022 11:41 AM

    The API you link to is the Provider API - code running in the portal runs as the consumer, not the provider, and you do not want to hardcode provider credentials into portal code, that means you don't want to use that API.
    There still is an equivalent API for the use of consumers: https://apic-api.apiconnect.ibmcloud.com/v10/?&_ga=2.44427564.960955133.1669998957-1886865969.1669998957#/IBMAPIConnectPlatformConsumerAPI_200/operation/%2Fproducts/get

    However if your code is running in the portal then you have all of the products and APIs for that catalog already sitting in the portal database next to your code. It would be a very odd architecture to ignore that data and make a hugely slower REST call back to API Manager to get data that is already cached locally.

    You can definitely filter what products and APIs are retrieved from the portal database - just look on google on how to write drupal entity queries. You can do all sorts of conditions to only get back the items that you want.

    Put simply - if you are writing a custom portal module then use the data already in the portal database, thats what its there for, if you are writing custom application code that will run somewhere else (such as a separate microservice), then by all means use the API.



    ------------------------------
    Chris Dudley
    ------------------------------



  • 5.  RE: Fetching Product or Apis lists

    Posted Fri December 02, 2022 01:01 PM
    Thank you. This does makes sense and helpful to make the decision.

    ------------------------------
    Syed Imtiyaz Alam
    ------------------------------