SOAP API call for CloneAsset via Javascript


(Martin) #1

Hi,


There seems to be an inconstancy between the Javascript SOAP request libraries and the WSDL documentation. When I call the SOAP API request via the Javascript libraries, it is being sent in this format:



[xml]

<soap-env:envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="/_web_services/soap-server">

<soap-env:body>

<ns1:cloneasset>

<assetid>string</assetid>

<currentparentid>string</currentparentid>

<newparentid>string</newparentid>

<numberofclone>int</numberofclone>

</ns1:cloneasset>

</soap-env:body>

</soap-env:envelope>[/xml]



But according to the documentation Matrix generates (/_web_services/soap-server?desc=CloneAsset&group=asset_service), it should be in the format:



[xml]

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns1="/_web_services/soap-server" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

<soap:Body>

<ns1:CloneAsset>

<AssetID>string</AssetID>

<NewParentID>string</NewParentID>

<NumberOfClone>int</NumberOfClone>

<PositionUnderNewParent>int</PositionUnderNewParent>

<LinkType>LinkType</LinkType>

</ns1:CloneAsset>

</soap:Body>

</soap:Envelope>[/xml]


(Nic Hubbard) #2

Are you sure that you have that SOAP Service function enabled, and the API you are using enabled?


(Duncan Robertson) #3

I've yet to make this work myself. Has anyone used the Javascript SOAP request successfully and would mind posting the code?


(Martin) #4

Hi Nic. Yes and yes. We have Python scripts successfully doing the same calls. I am pretty sure it is just the .js files provided the aren't correctly coded.


I am with Duncan. If anyone has working code examples, they would be greatly appreciated.


(Nic Hubbard) #5

I use the SOAP server all the time, but I have not used the .js files provided for this. I think that you should submit a bug report for these issues.


(Benjamin Pearson) #6

Hopefully, this help you and other people with the js calls.


[list=1]

  • Create a standard page with raw html content type
    [*]Added the js code below to the raw html content type
    [*]Previewed the page and clicked the button


    And to me everything is functioning the way it should, if the wrong response is returned can you please check that your javascript is not making the content lower case. Maybe your javascript is having a namespace issue with the soap js?

        
        
        
        var current_system_root = 'http://www.YOURDOMAIN.com';
        include_service_lib('asset', current_system_root);
    
    function clonePageAsset()
    {
      // This is the location of the SOAP Server which support the CloneAsset method
      var location = 'http://www.YOURDOMAIN.com/_web_services/soap';
      var wsdl = 'http://www.YOURDOMAIN.com/_web_services/soap?WSDL';
    
      // Clone Asset has the following args (AssetID of Asset to Clone, Current Parent ID, Destination Parent ID and Number of Clones)
      // Some this example would clone Asset #99 under Site #74 to Site #74 with 3 clones.
      var soapBody = CloneAsset('99', '74', '74', 3);
      var soapRequest = constructSOAPRequest(soapBody, location);
      send(wsdl, soapRequest, 'handleClonePage');
    
    }//end clonePageAsset()
    
    function handleClonePage(response)
    {
      // Your custom callback
    
    }//end handleClonePage()
    
    </script>
    
    <button onClick="clonePageAsset();">Clone Page</button>
    




    There is further information about the soap javascript files here: http://manuals.matrix.squizsuite.net/web-services/chapters/basic-soap-implementation



    Hope that helps

  • (Anton Babushkin) #7

    Matrix needs a RESTful API :slight_smile:


    (Nic Hubbard) #8

    [quote]
    Matrix needs a RESTful API :slight_smile:

    [/quote]



    Is that different from the REST Resource assets? Curious about those…


    (Benjamin Pearson) #9

    [quote]
    Is that different from the REST Resource assets? Curious about those…

    [/quote]



    The REST Resource Asset is a REST client used to access a REST server.



    A RESTful API is a API to perform actions (similar to what you are doing with SOAP) but the actions are performed by HTTP request. A simple example would be



    REST Url: http://www.example.com/rest/pages/110



    Performing a action would achieve the following:

    GET - would list all attributes of page #110.

    PUT - would replace the page with another page or create if it doesn't exist.

    POST - would create a new page

    DELETE - would delete the page.



    Theoretically, REST is the same method a browser uses to retrieve a web page, but this is not deemed an "application" because web pages are generally static and non interactive. Many companies, like Amazon and Ebay do offer developer interfaces using REST. Matrix could offer REST too, although we do currently offer 2 different APIs (SOAP and JS), a third API would be overkill at this point.





    Hope that helps


    (Martin) #10

    [quote]
    Hopefully, this help you and other people with the js calls.



    [list=1]

  • Create a standard page with raw html content type
    [*]Added the js code below to the raw html content type
    [*]Previewed the page and clicked the button


    And to me everything is functioning the way it should, if the wrong response is returned can you please check that your javascript is not making the content lower case. Maybe your javascript is having a namespace issue with the soap js?

        
        
        
        var current_system_root = 'http://www.YOURDOMAIN.com';
        include_service_lib('asset', current_system_root);
    
    function clonePageAsset()
    {
      // This is the location of the SOAP Server which support the CloneAsset method
      var location = 'http://www.YOURDOMAIN.com/_web_services/soap';
      var wsdl = 'http://www.YOURDOMAIN.com/_web_services/soap?WSDL';
    
      // Clone Asset has the following args (AssetID of Asset to Clone, Current Parent ID, Destination Parent ID and Number of Clones)
      // Some this example would clone Asset #99 under Site #74 to Site #74 with 3 clones.
      var soapBody = CloneAsset('99', '74', '74', 3);
      var soapRequest = constructSOAPRequest(soapBody, location);
      send(wsdl, soapRequest, 'handleClonePage');
    
    }//end clonePageAsset()
    
    function handleClonePage(response)
    {
      // Your custom callback
    
    }//end handleClonePage()
    
    </script>
    
    <button onClick="clonePageAsset();">Clone Page</button>
    




    There is further information about the soap javascript files here: http://manuals.matrix.squizsuite.net/web-services/chapters/basic-soap-implementation



    Hope that helps

    [/quote]



    Hi Benjamin,



    Thanks for the reply. Your above code is essentially what I have done. The fault string returned is: “Unable To Clone Asset”.



    If your call to CloneAsset is correct, then why does the documentation Matrix generates (http://www.YOURDOMAIN.com/_web_services/soap-server?desc=CloneAsset&group=asset_service) say that the XML sent should have different variables and be in a different format to the request sent?



    PositionUnderNewParent and LinkType are missing. Why does it need the CurrentParentID, when it has the AssetID?

  • (Duncan Robertson) #11

    Hey Benjamin, thanks for the code.


    I implemented this on a fresh install of 4.0.2, did the code, and I'm getting an error:


        Failed to load resource: the server responded with a status of 500 (Internal Service Error)


    This is the request payload:

        <?xml version="1.0" encoding="UTF-8"?>7170753

    (Benjamin Pearson) #12

    [quote]
    Hi Benjamin,



    Thanks for the reply. Your above code is essentially what I have done. The fault string returned is: “Unable To Clone Asset”.



    If your call to CloneAsset is correct, then why does the documentation Matrix generates (http://www.YOURDOMAIN.com/_web_services/soap-server?desc=CloneAsset&group=asset_service) say that the XML sent should have different variables and be in a different format to the request sent?



    PositionUnderNewParent and LinkType are missing. Why does it need the CurrentParentID, when it has the AssetID?

    [/quote]



    Ahh, I see the problem, yes you are correct PositionUnderNewParent and LinkType are missing from the JS file, but in the code, the position defaults to -1 (at the end of the tree) and link type defaults to link type 1, so when the js file is called it has everything to complete the request. As for the current parent id, it is set, but never used. The new bug report can be found here: http://bugs.matrix.squiz.net/view_bug.php?bug_id=4933



    EDIT Forgot about your problem, “Unable to Clone Asset” happens when the HIPO job is unable to clone asset (of course!). Can you check you error log (data/private/logs/error.log) or try it in the backend with the same assets? It might reveal why you are having trouble. At a guess, I would assume it might be because the user you are trying to execute the clone asset doesn’t have write permission to the new parent asset.


    (Martin) #13

    [quote]
    Ahh, I see the problem, yes you are correct PositionUnderNewParent and LinkType are missing from the JS file, but in the code, the position defaults to -1 (at the end of the tree) and link type defaults to link type 1, so when the js file is called it has everything to complete the request. As for the current parent id, it is set, but never used. The new bug report can be found here: http://bugs.matrix.squiz.net/view_bug.php?bug_id=4933



    EDIT Forgot about your problem, “Unable to Clone Asset” happens when the HIPO job is unable to clone asset (of course!). Can you check you error log (data/private/logs/error.log) or try it in the backend with the same assets? It might reveal why you are having trouble. At a guess, I would assume it might be because the user you are trying to execute the clone asset doesn’t have write permission to the new parent asset.

    [/quote]





    Thanks!


    (Benjamin Pearson) #14

    [quote]
    Hey Benjamin, thanks for the code.



    I implemented this on a fresh install of 4.0.2, did the code, and I'm getting an error:


        Failed to load resource: the server responded with a status of 500 (Internal Service Error)


    This is the request payload:

        <?xml version="1.0" encoding="UTF-8"?>7170753

    [/quote]

    Hmm not exactly sure, but I don't think it is relating to the Soap Server as core/include/mysource.inc seems to be the only location to throw a 500 error, and this is usually a Database Error. Does data/private/logs/error.log show any errors?

    (Martin) #15

    [quote]
    Ahh, I see the problem, yes you are correct PositionUnderNewParent and LinkType are missing from the JS file, but in the code, the position defaults to -1 (at the end of the tree) and link type defaults to link type 1, so when the js file is called it has everything to complete the request. As for the current parent id, it is set, but never used. The new bug report can be found here: http://bugs.matrix.squiz.net/view_bug.php?bug_id=4933

    [/quote]



    The bug report says it has been fixed in build 4.3. Where can I download this version of Matrix with the fix?


    (Benjamin Pearson) #16

    [quote]
    The bug report says it has been fixed in build 4.3. Where can I download this version of Matrix with the fix?

    [/quote]



    Version 4.3 is only a pre-release version and can only be downloaded from the public cvs here


    (Martin) #17

    [quote]
    Version 4.3 is only a pre-release version and can only be downloaded from the public cvs here

    [/quote]



    Thanks for your help. This is working now, but is causing a further error.



    To find the updated .js files, I had to go here: http://public-cvs.squiz.net/cgi-bin/viewcvs.cgi/?root=matrix_web_services



    I copied the files over from the CVS branch to the relevant places under: path_to_matrix/packages/web_services/api



    I found I had to also copy these files to: path_to_matrix/public/asset_types (which is the place they are actually served from)



    Now the SOAP request is being sent in the format that matches the documentation:


        
        <?xml version="1.0" encoding="UTF-8"?>
        
            
                
                    149776
                    134736
                    1
                    3
                    1
                
            
        
    


    The asset is successfully being cloned. However, this is returning a 500 error, with this response:

        
        <?xml version="1.0" encoding="UTF-8"?>
        
        
            
                SOAP-ENV:Server
                Unable To Clone Asset
            
        
        
    




    The corresponding line in the log file is:
        
        [2011-02-18 12:17:33][133963:Martin Butt][8:php notice][R] (/core/hipo/jobs/hipo_job_clone_assets.inc:610) - Undefined index:  linkid
    


    Can you shed any light on this?

    (Benjamin Pearson) #18

    [quote]


    The corresponding line in the log file is:

        
        [2011-02-18 12:17:33][133963:Martin Butt][8:php notice][R] (/core/hipo/jobs/hipo_job_clone_assets.inc:610) - Undefined index:  linkid
    


    Can you shed any light on this?
    [/quote]

    It looks like it failed before that, is there any log entry before that entry? Maybe it might be a permission problem (do you have write access to the parent asset etc?)

    (Martin) #19

    [quote]
    It looks like it failed before that, is there any log entry before that entry? Maybe it might be a permission problem (do you have write access to the parent asset etc?)

    [/quote]



    That is the only relevant line in the log file. The thing is, that the clone WAS successful. But the SOAP call returns with a 500 status and thinks it failed. I am happy to put some debug code in to get some better output, if you tell me where.


    (Benjamin Pearson) #20

    [quote]
    That is the only relevant line in the log file. The thing is, that the clone WAS successful. But the SOAP call returns with a 500 status and thinks it failed. I am happy to put some debug code in to get some better output, if you tell me where.

    [/quote]



    In packages/web_services/api/soap_api_asset_service/soap_api_asset_service.inc, put:


        log_dump($all_errors);


    just above the line:

        throw new SoapFault('Server', 'Unable To Clone Asset');


    then clone the asset again and look at the error log.