Results 1 to 10 of 10

Thread: Extending the REST API

  1. #1
    TheLetterM's Avatar
    TheLetterM is offline Senior Member
    Join Date
    Mar 2009
    Posts
    26

    Question Extending the REST API

    Hi Sugar Team and Devs,

    I am doing a lot of work with the REST API in Sugar 5.5, and as such have been relying on the documentation to provide some explanation of its features, especially as it relates to upgrade-safe customization.

    As such, I'd like to ask some questions to see if we can clear up what's written in the doc, as it's very confusing.

    Extensibility in Upgrade Safe Manner
    In Tokyo release, we have provided upgrade safe extensibility by using the concept of versioning. (Please look at the versioning section in detail).
    Following are the steps needed to create your own version which is upgrade safe.
    1)In services directory, there is a v2 directory. Create a v2_1 directory in the custom directory. This v2_1 can be created anywhere in the directory structure of source code but its best to put it in the custom directory so that it will be upgrade safe.
    This is a little misleading. Do we create the "v2_1" folder in <root>/custom/v2_1 (doesnt seem quite right), <root>/services/custom/v2_1 (not sure about this either), <root>/custom/services/v2_1 (which is my best guess). The documentation says it can be "anywhere in the source code, after telling you to put it in a specific place.
    Also, (jumping ahead a bit) do ALL the files for the implementation need to go here?
    (The versioning section is not helpful).

    2)You need to provide you own registry.php for ex. customregistry.php and the source code looks like:
    <?php
    require_once('service/v2/registry.php');
    ...
    Where does this file go?

    3)Look at the registerFunction. We call parent:: registerFunction() to include all the out of box functions and the next line is $this->serviceClass->registerFunction(name, input, output). So this way you can provide your own functions.
    4)You need to provide you own implementation class which extends from the base implementation class. For ex.
    <?php
    require_once('service/core/SugarWebServiceImpl.php');
    class SugarWebServiceImpl_v2_1 extends SugarWebServiceImpl {
    function get_entry($session, $module_name, $id){
    return $id;
    } // fn
    } // class
    This all seems pretty straightforward, except where the files go.

    5)You need to provide you own soap.php or rest.php and initialize all the variables.
    So now your new SOAP URL will be http://localhost/service/v2_1/soap.php.
    Following is an example of your own rest.php
    <?php
    Chdir(../) (based on how much deep you have defines v2_1 directory)
    $webservice_class = 'SugarRestService2';
    $webservice_path = 'service/v2/SugarRestService2.php';
    $registry_class = ' customregistry ';
    $registry_path = 'service/v2_1/ customregistry.php';
    $webservice_impl_class = 'SugarRestServiceImpl_v2_1';
    $location = '/service/v2_1/rest.php';
    require_once('../core/webservice.php');
    ?>
    Where should the chdir ultimately command be going? To the root directory?

    So now your new REST URL will be http://localhost/service/v2_1/rest.php.
    So this way, your v2_1 directory is upgrade safe.
    This bit pretty much sums up all the confusion. If the v2_1 goes in the <root>/custom directory (as suggested in step 1), how is the url in <root>/service? There is no sort of handler that will redirect requests that i can see.

    Any help is appreciated. It seems like this is a great new concept in Sugar 5.5, and knowing how to use it would allow us devs to do some great new things in terms of external interaction with Sugar.


    Thanks.

  2. #2
    skymeyer is offline Member
    Join Date
    Dec 2006
    Posts
    5

    Default Re: Extending the REST API

    Can some SugarCRM developers comment on this one please ? As stated it is very confusing where to put the files. Need help on this one !

  3. #3
    jamos is offline Junior Member
    Join Date
    Mar 2009
    Posts
    2

    Default Re: Extending the REST API

    After some trial and error, I've got a working custom impementation.

    As with all the documentation I've seen for Sugar, there are some big gaps, and other items that are outright incorrect. I'm specifically extending the REST service, so my notes are specific to that, and I'm working with 5.5.x (CE) - so I have no idea if this applies to the newly released v 6.

    I've made the assumption that to be upgrade safe, the custom service has to live in /custom - so in my case I made it /custom/services/v2_1/

    And all the custom files do seem to go here. e.g.

    Code:
    /custom/services/v2_1/
    /custom/services/v2_1/customregistry.php
    /custom/services/v2_1/rest.php
    /custom/services/v2_1/SugarRestServiceImpl_v2_1.php
    /custom/services/v2_1/SugarRestService_v2_1.php
    Then my implementation class extends not the WebService abstract, but the Rest Service Impl class - so SugarRestServiceImpl_v2_1.php looks like...

    Code:
    require_once('service/core/SugarRestServiceImpl.php');
    
    class SugarRestServiceImpl_v2_1 extends SugarRestServiceImpl {
    //do stuff here
    }
    and rest.php looks like

    Code:
    Chdir('../../..');
    $webservice_class = 'SugarRestService_v2_1';
    $webservice_path = 'custom/services/v2_1/SugarRestService_v2_1.php';
    $registry_class = 'customregistry';
    $registry_path = 'custom/services/v2_1/customregistry.php';
    $webservice_impl_class = 'SugarRestServiceImpl_v2_1';
    $location = '/custom/services/v2_1/rest.php';
    require_once('service/core/webservice.php');
    Note that because we're three levels deep in custom/services/v2_1 - the Chdir command goes up three levels, not the two as provided in the examples.

    As far as I can see, there's not a way to override the implementation that's called by the default SugarRestService class, so I extended it as simply as possible:

    Code:
    require_once('service/core/SugarRestService.php');
    require_once('custom/services/v2_1/SugarRestServiceImpl_v2_1.php');
    
    class SugarRestService_v2_1 extends SugarRestService{
      protected $implementationClass = 'SugarRestServiceImpl_v2_1';
    }
    There may be a better approach than this - but it's late, and frankly I've burned way too many hours already on what should have been a much simpler project thanks in no small part to the idiosyncrasies of the Sugar documentation (and that unfortunately includes the Definitive Guide from Apress...)

    One last thing - the customregistry.php example, once you know where to put it, is correct.

    Hope this helps some other poor sap who gets stuck following the docs.

  4. #4
    gbelin is offline Member
    Join Date
    Sep 2010
    Posts
    10

    Default Re: Extending the REST API

    Quote Originally Posted by jamos View Post
    After some trial and error, I've got a working custom impementation.

    As with all the documentation I've seen for Sugar, there are some big gaps, and other items that are outright incorrect. I'm specifically extending the REST service, so my notes are specific to that, and I'm working with 5.5.x (CE) - so I have no idea if this applies to the newly released v 6.

    I've made the assumption that to be upgrade safe, the custom service has to live in /custom - so in my case I made it /custom/services/v2_1/

    And all the custom files do seem to go here. e.g.

    Code:
    /custom/services/v2_1/
    /custom/services/v2_1/customregistry.php
    /custom/services/v2_1/rest.php
    /custom/services/v2_1/SugarRestServiceImpl_v2_1.php
    /custom/services/v2_1/SugarRestService_v2_1.php
    Then my implementation class extends not the WebService abstract, but the Rest Service Impl class - so SugarRestServiceImpl_v2_1.php looks like...

    Code:
    require_once('service/core/SugarRestServiceImpl.php');
    
    class SugarRestServiceImpl_v2_1 extends SugarRestServiceImpl {
    //do stuff here
    }
    and rest.php looks like

    Code:
    Chdir('../../..');
    $webservice_class = 'SugarRestService_v2_1';
    $webservice_path = 'custom/services/v2_1/SugarRestService_v2_1.php';
    $registry_class = 'customregistry';
    $registry_path = 'custom/services/v2_1/customregistry.php';
    $webservice_impl_class = 'SugarRestServiceImpl_v2_1';
    $location = '/custom/services/v2_1/rest.php';
    require_once('service/core/webservice.php');
    Note that because we're three levels deep in custom/services/v2_1 - the Chdir command goes up three levels, not the two as provided in the examples.

    As far as I can see, there's not a way to override the implementation that's called by the default SugarRestService class, so I extended it as simply as possible:

    Code:
    require_once('service/core/SugarRestService.php');
    require_once('custom/services/v2_1/SugarRestServiceImpl_v2_1.php');
    
    class SugarRestService_v2_1 extends SugarRestService{
      protected $implementationClass = 'SugarRestServiceImpl_v2_1';
    }
    There may be a better approach than this - but it's late, and frankly I've burned way too many hours already on what should have been a much simpler project thanks in no small part to the idiosyncrasies of the Sugar documentation (and that unfortunately includes the Definitive Guide from Apress...)

    One last thing - the customregistry.php example, once you know where to put it, is correct.

    Hope this helps some other poor sap who gets stuck following the docs.
    I am trying to develop a webservice similar than the REST yo have develop, do you have any documentation that explains how to create a custom webservice? Is there any webservice sample that could be downloaded to see what files are needed and where they should be?

    thank in advance.

  5. #5
    cmould is offline Senior Member
    Join Date
    Oct 2009
    Posts
    45

    Default Re: Extending the REST API

    Thanks for this, I had posted similar frustration with the documentation seeking help to do this; in my issue I was able to see the function in the wsdl by pointing a browser at the soap server. But on execution I received a "function..... does not exist in the service" errorr. I however was extending the soap implementation and not REST and my files were located in custom/v2_1. Dont see why this would matter though. I will try your approach.

  6. #6
    geraldclark is offline Sugar Community Member
    Join Date
    Nov 2008
    Location
    Pittsburgh PA
    Posts
    35

    Default Re: Extending the REST API

    did anyone ever officially figure this out?

  7. #7
    cmould is offline Senior Member
    Join Date
    Oct 2009
    Posts
    45

    Default Re: Extending the REST API

    Quote Originally Posted by geraldclark View Post
    did anyone ever officially figure this out?
    I have spent many hours on this I cannot get this to work with SugarSoap. I have had to revert to modifying the v2 files to get my function seen. Will someone properly document what is required to extend the soap api.

  8. #8
    geraldclark is offline Sugar Community Member
    Join Date
    Nov 2008
    Location
    Pittsburgh PA
    Posts
    35

    Default Re: Extending the REST API

    cmould,

    I have attached a code example that extends V2. Extending any other version should work the same way.

    The files in this zip belong in custom/cust_service/v2 - this is the directory I chose to keep things making sense however you can opt to change it.

    The URL for the extended api will be http://your-url/sugarcrm/custom/cust...ce/v2/rest.php

    let me know how it goes!
    Attached Files Attached Files
    Last edited by geraldclark; 2010-11-23 at 09:41 PM. Reason: typo

  9. #9
    vladimir.pekez is offline Junior Member
    Join Date
    Apr 2010
    Posts
    5

    Default Re: Extending the REST API

    Hi Gerald

    I have used your starter code and created my own SOAP extension but I always get:

    method 'login' not defined in service

    when for example trying to login via WS (using SoapUI)

    and the following PHP warning in PHP error log:

    PHP Warning: in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in /var/www-ext/html/include/nusoap/nusoap.php on line 3564

    I guess I should be able to use all the inherited V2 operations such as login in my extended web service?

    Vladimir

    Quote Originally Posted by geraldclark View Post
    cmould,

    I have attached a code example that extends V2. Extending any other version should work the same way.

    The files in this zip belong in custom/cust_service/v2 - this is the directory I chose to keep things making sense however you can opt to change it.

    The URL for the extended api will be http://your-url/sugarcrm/custom/cust...ce/v2/rest.php

    let me know how it goes!

  10. #10
    geraldclark is offline Sugar Community Member
    Join Date
    Nov 2008
    Location
    Pittsburgh PA
    Posts
    35

    Default Re: Extending the REST API

    Hi Vladimir,

    This link should help point you in the right direction:
    Extending Web Services - SugarCRM Support Site

    I recommend you update your code to use the latest v4_1 REST API as the SOAP interface will most likely be deprecated in the near future.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Rest in C#
    By geraldclark in forum Developer Help
    Replies: 8
    Last Post: 2010-10-26, 02:33 AM
  2. Rest Api Of SugarCRM
    By karan5037 in forum General Discussion
    Replies: 0
    Last Post: 2009-10-21, 05:27 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •