Extending API method

Hi all,

I have to extend saveFilePost in clients/base/api/FileApi.php, looking at https://support.sugarcrm.com/Documentation/Sugar_Developer/Sugar_Developer_Guide_7.7/Integration/Web_Services/v10/Extend…  seems so simple. In my case I have put in custom/clients/base/api/CustomFileApi.php this code:

<?php
require_once('clients/base/api/FileApi.php');
class CustomFileApi extends FileApi {
    public function registerApiRest() {
        return parent::registerApiRest();
    }
    public function saveFilePost($api, $args, $temporary = false) {
        $field = $args['field'];
        $bean = $this->loadBean($api, $args);
        $def = $bean->field_defs[$field];
        if($def['type'] == 'MyNewFileType') {
            $def['type'] = 'image';
        }
        $response = parent::saveFilePost($api, $args, $temporary);
        return $response;
    }
}

Looking at {my_sugar}/rest/v10/help I've seen there are two identical endpoints, the only difference is the score, one is 9.50 (original) and one is 10.00 (custom).

I was thinking that this number is the priority but placing some breakpoints the call only stop in the original method.

Can someone help me? Thanks in advance.

  • Hi Nicola,

    How that score is calculated is not well documented but basically the priority is scored based on specificity of how the API is registered.  This is how it selects which API to use when multiple APIs are potentially set up to service the same endpoint.  So, for example, an API registered for a particular module will "win" because it is more specific than one than a base API that was registered to all modules.

    However, the highest score should win so your Custom API should be called from what you've shared.  I think there is something else going wrong?  However, you can try to boost the score artificially.

    For me, it is a best practice to explicitly define which API endpoints you are overriding via the registerApiRest() function instead of calling the parent implementation.  This also gives you more control over how your API gets registered and allows you to use an extra parameter 'extraScore' to tweak the score for your API endpoint.

    For example,

    <?php
    
    
    if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    
    
    require_once("clients/base/api/PingApi.php");
    
    
    class CustomPingApi extends PingApi
    {
       public function registerApiRest() {
       return array(
       'ping' => array(
       'reqType' => 'GET',
       'path' => array('ping'),
       'pathVars' => array(''),
       'method' => 'ping',
       'shortHelp' => 'An example API only responds with pong (and include timestamp) ',
       'extraScore' => 2,  // Boost score by 2, negative values will decrease score
       ),
       );
       }
    
    
       //override to modify the ping function of the ping endpoint
       public function ping($api, $args)
      {
       $result = parent::ping($api, $args);
    
    
       //append the current timestamp
       return $result . ' ' . time();
       }
    }
    

    App Ecosystem @ SugarCRM

  • Hi Matt,

    I've tried with that extraScore parameter and finally it passed in the right method. In my example the local variable lose the context when I call the parent method. So the only way you have to override something different out of $args is to copy the same method and modify it.

    Thank you for your help, is always interesting acquire these knowledges.