Page 1 of 1

Generic Service - how to use?

Posted: Mon May 07, 2012 10:03 pm
by Tommy Jorgensen

My problem is I have a REST service that only takes input as a request body.

That means no query parameters and no form parameters. So I cannot use the input mapping that is provided by the wizard.

I have tested with the same REST service with zero input and the output comes out just fine as per the mappings.

I have now created a Generic Service and added it to the App, then I did the output mappings based on a sample response.

So with my intentions hopefully clear, my question is about the custom implementation.

I have been unable to find any documentation on this topic, other than "settings" is the same as passed in to jQuery.ajax

My assumption is that all I have to do is configure the "settings" so when I click a button that has "Invoke Service" as an action, it will call "execute", which triggers my my custom settings to run just before it.

I set the url and dataType like this:
settings.url = '[url=http://..../rest/myservice']http://..../rest/myservice'[/url];
settings.dataType = 'json';

I can see in the Browser that the Console.log the implementation is invoked, but it never executes the service.

I tried calling
myService.execute( settings);

but that caused something that looked like a stackoverflow (as if execute got caught in a recursive loop).

I also tried overriding the success method, but no noticeable result

settings.success(
function ( data) {
updateComponents( data);
alert( 'custom data: '+data);
}
);


Generic Service - how to use?

Posted: Mon May 07, 2012 11:03 pm
by maxkatz

Good timing.. I was just about to post a reply on how to use the Generic Service for different question.

The service is invoked just like any other service and you can of course define input and output parameters. Here is a very simple example:

code
$t.myimpl = $t.createClass(null, {

Code: Select all

 init: function(requestOptions) { 
     this.__requestOptions = $.extend({}, requestOptions); 
 }, 

 process: function(settings) { 
     if (this.__requestOptions.echo) { 
         settings.success(this.__requestOptions.echo); 
     } else { 
         console.log('Default implementation is used. Please define your own.'); 

        // save JSON data to local storage 
        // could be saved from a another (previous) service invocation.  
         localStorage.setItem("cacheddata", JSON.stringify({ 
             'name': 'joe' 
         })); 

       // load JSON data from local storage 
        var cdata = localStorage.getItem("cacheddata"); 

        // pass the JSON to the service, for output mapping 
         settings.success(JSON.parse(cdata)); 
     } 
     settings.complete('success'); 
 } 

});
/code

The above assumes you service returns "name". You can map name to any component, just the standard mapping.

Hope this helps.


Generic Service - how to use?

Posted: Mon May 07, 2012 11:26 pm
by Tommy Jorgensen

Thanks Max, I did need to call a REST service, and perhaps that was where I got things mixed up, in that I tried modifying the service I was executing on "click" and re-executing it naturally caused trouble, where as it seems what I really would need is to actually have two services.

One "Generic Service" that is called on action
One "REST Service" that I can invoke from the Generic Service.

For now I just ended up doing an async call using the $.ajax method and that worked in case it helps others

code
...
console.log('Default implementation is used. Please define your own.')

Code: Select all

 var jsonData = JSON.stringify({}); // empty result 
 $.ajax({ 
  url: restServiceUrl, 
  async: false, 
  dataType: 'json', 
  success: function ( data){ 
   jsonData = data; // JSON.parse( data); // formatted data already in JSON format 
      } 
 }); 

 settings.success( jsonData); 

...
/code


Generic Service - how to use?

Posted: Mon May 07, 2012 11:38 pm
by maxkatz

You can invoke a service defined in Tiggzi like this:

serviceInstanceName.execute({});


Generic Service - how to use?

Posted: Fri Nov 23, 2012 1:44 pm
by Ricki Ricardo

Can I send my own paramentrs? like serviceName.execute({'param1': 'param1value', 'param2': 'param2Value'}); Or how can I do it?


Generic Service - how to use?

Posted: Fri Nov 23, 2012 4:44 pm
by maxkatz

When you create a Generic Service, you have an option to request parameters like in a regular service. Request parameters you define will be available in settings.data


Generic Service - how to use?

Posted: Tue Jan 06, 2015 7:52 pm
by Bikram Ray

• What I need is to POST to following Services: https://api.test.sabre.com/v1/auth/token?=

• You need to use the POST method and send as a header your authorization Token: Authorization: Basic VmpFNk16VmxNamcyWkdWdGRXTSVTVVUlZJNlJWaFU6ZHpOTWQzZE5Telk9

• You will need to send the following information as a payload: grant_type=client_credentials

• And set the Content-Type header as: application/x-www-form-urlencoded

I have created the REST Service in Appery.io to call this but I am having problem with setting the payload. It is coming as grant_type:client_credentials. But the specification need it as grant_type=client_credentials.


Generic Service - how to use?

Posted: Wed Jan 11, 2017 9:03 am
by EJLD

even 5yrs later, still helpful. Thks