Velocity Services question

Questions and answers on developing, deploying and using plugins and JavaBeans

Velocity Services question

Postby jdbruijn » Wed Mar 04, 2015 5:06 pm

I'm trying to use the velocity plugin to access a webservice of JasperServer.
I've tested the call using the chrome Postman plugin and there I'm getting an response so restful is working on my JasperServer
This is the config.json that I'm using:
Code: Select all
test_invoke:{ 
   services: {
     jasper_info:{
        url:"http://localhost:8181/jasperserver-pro/rest_v2/serverInfo",
       method:"get",
       returns:{
          data:"String"
       }
     }
}

And I'm calling it like this:
Code: Select all
function onAction(event) {
   plugins.Velocity.invokeService('jasper_info',{}, callBack);
}

function callBack(result){
   application.output('callback');
   if (result) {
       if (result.exception) {
           application.output(result.exception, LOGGINGLEVEL.ERROR);
       } else {
           application.output(JSON.stringify(result));
       }
   }
}

But I'm getting an empty object back from the call. maybe my config.json is incorrect, but according to the documentation it should return a string and does not require athentication.
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby jdbruijn » Wed Mar 04, 2015 5:20 pm

It definitely is related to my config.json. When I remove the returns section:
Code: Select all
test_invoke:{ 
   services: {
     jasper_info:{
        url:"http://localhost:8181/jasperserver-pro/rest_v2/serverInfo",
       method:"get"
     }

Then I'm getting the data back in my callback function (as a json object in result.data).
How do I format my config.json so it knows it is getting a json object back?
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby ptalbot » Wed Mar 04, 2015 9:58 pm

You don't. Actually not declaring the return in config.json will return the body of the response in result.data.
All you have to do is then convert that String, using plugins.VelocityReport.fromJSON(result.data) to get an object back.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Velocity Services question

Postby jdbruijn » Thu Mar 05, 2015 9:55 am

Thanks Patrick that explains it.

Next question is how do I sent a JSON object back to the webservice in the request body?
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby jdbruijn » Fri Mar 06, 2015 4:08 pm

Patrick,
This is what I came up with for the PUT request:
Code: Select all
     jasper_set_user:{
        url:"http://localhost:8181/jasperserver-pro/rest_v2/organizations/$org/users/$usr",
       method:"put",
       authentication:"basic",
       defaultUser:"superuser",
       params:{
         usr:"String",
         org:"String",
         body:"String"
       },
       entityFormat:"json"
     },


And this is how I am calling it:
Code: Select all
   var myUser = {
      fullName:"Jos de Bruijn",
      emailAddress:"my@mail.address",
      enabled:true,
      password:'1234',
      roles:[
      {name:"ROLE_USER"}]
      };
   
   plugins.Velocity.invokeService('jasper_set_user',{usr: 'jos',org: 'myOrg', body: plugins.VelocityReport.toJSON(myUser)}, callBack);


And although I'm getting no error response back from the server, it has not updated my user record correctly. It has done something because I cannot login anymore with this user.
It probably is in my config.json, but I cannot find any examples that look like my situation.
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby ptalbot » Fri Mar 06, 2015 10:49 pm

If your service is expecting a JSON body you are right that you need to use 'entityFormat: "json"'
But you should probably leave the myUser object as an Object. so
body: "Object" in config.json
and then
plugins.Velocity.invokeService('jasper_set_user',{usr: 'jos',org: 'myOrg', body: myUser}, callBack);
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Velocity Services question

Postby jdbruijn » Mon Mar 09, 2015 10:01 am

I tried that, but then I get the following reply:
net.stuff.servoy.plugin.velocityreport.exception.UnknownParameterTypeException: Unknow type: body
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby ptalbot » Wed Mar 11, 2015 1:03 am

Hum, I need to check that out. What happens if you don't declare it at all (but pass it in the params)?
It also really depends on the type of structure the service is expecting.
Receiving { body: { prop: "value" } } is different from receiving {prop: "value"} so that part needs to be check in the service documentation.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC

Re: Velocity Services question

Postby jdbruijn » Wed Mar 11, 2015 10:06 am

Not declaring it does not work.

i've tested the webservice with the postman plugin in chrome.
In there I've added 2 headers: Authentication and content-type: application/json
And in the raw field i've added the JSON string. That is accepted by the webservice, so I know that is working.
The webservice returns the changed record as xml string.
When I run from velocity I do get an xml string back, but it has no changed data. So the webservice looks like it is accepting my data (I dont get an error), but not really doing anything with it.
Jos de Bruijn
Focus Feedback BV
Servoy Certified Developer
Image
jdbruijn
 
Posts: 492
Joined: Sun Apr 11, 2010 6:34 pm

Re: Velocity Services question

Postby ptalbot » Thu Mar 19, 2015 7:16 pm

Sorry for the delay on this, had been quite busy lately...

There was an issue when the processType was set to "server", the result from the service invocation was an empty object if you didn't specify the returns property.
I've fixed that in v3.5.5 that I just released. I also added the option to specify the type of return value as "xml" or "json",
so you can declare:
returns: {
data: "json"
}

or
return: {
data: "xml"
}

And you won't need to deserialize the result. It will be received as a Servoy JavaScript object with the name you've specified.

As to why your service is not updating the data, I would have to know the service and what it expects to be able to help.
What you can do is change the url of your service to point to a request bin url (you can create one here: http://requestb.in/). This will allow you to inspect what is sent to invoke your service and see if the data (and headers) matches your service expectations...

Hope this helps.
Patrick Talbot
Freelance - Open Source - Servoy Valued Professional
https://www.servoyforge.net
Velocity rules! If you don't use it, you don't know what you're missing!
User avatar
ptalbot
 
Posts: 1654
Joined: Wed Mar 11, 2009 5:13 am
Location: Montreal, QC


Return to Plugins and Beans

Who is online

Users browsing this forum: No registered users and 3 guests