Page 1 of 1

Example of using a Dynamic URL with REST service please?

Posted: Thu Aug 15, 2013 9:18 am
by steveharman

Hello!

I've defined a REST service which connects to a remote back-end that accepts parameters and "commands". For example:

http://www.mybackend.com/api.php?USER...

I'd like to keep it simple so that the Service definition in Appery simply has the connection URL and USERNAME / PASSWORD it needs to reach the backend - with everything else specified when I invoke the service.

The USERNAME and PASSWORD are setup as part of the service and it seems pretty straightforward to map a request parameter from a component (so that handles the "PARAMETER" side of my request), what I'd like is to pass a different COMMAND value to the service request each time depending on what I'd like the service to do. Sometimes it might be DELETE, sometimes ADD, sometimes LIST etc.

This sounds like I need to setup a variable of some kind in the request parameters of the service I've defined and then set that variable's value when I invoke the service (eg; DELETE, ADD, etc.). It seems like I need to use a Dynamic URl as discussed here: http://docs.appery.io/documentation/r... but for the life of me I can't figure out how I set this up and call it when I invoke the service on my Appery page.

Presumably I create, say {command} in the Request Parameter of the service I define and then somehow* specify the contents of {command} when invoking the service itself?

  • This is the part I don't get! ;-)

    If someone could provide an example I'd be grateful. According to another post, Max provided an example at the bottom of https://getsatisfaction.com/apperyio/... but the link seems to have died.

    Thanks,

    Steve


Example of using a Dynamic URL with REST service please?

Posted: Thu Aug 15, 2013 11:05 am
by Maryna Brodina

Hello! Save {command} into localStorage variable and map it to request parameter. Here is the correct link https://getsatisfaction.com/apperyio/...


Example of using a Dynamic URL with REST service please?

Posted: Thu Aug 15, 2013 2:48 pm
by steveharman

Hi Marina,

Thanks for the reply, I now understand how it should work, but am having trouble getting it TO work! :-)

To clarify; can I user the {command} tag concept in my REST service's parameter name, its value or in both?

What I'm trying to achieve is dynamically pass a parameter & value to my REST service ("list=1") but can't get it to work from a button on my page. The service definition only has a username & password defined and I need to pass different parameters AND values to it from my app.

It would be easy if the REST service simply had a "command=" parameter and I only needed to pass values to it from a local storage variable, but each command is actually named differently ("list=yes", "detele=yes" etc.) so I need to pass different commands (parameters) and values dynamically from my app.

Currently as I say I have the service username & password "hard coded" in the service as they won't change. Also if I specify "list" as a parameter and "1" as a value and hard code it within the service definition and simply invoke the service frim my button with an on-click event, it works fine and I get results returned to a grid.

It's when I come to create a local storage variable, specify its contents to be "list=yes" and map the local storage variable to the {command} parameter in the service data mapping of the button that I get nothing back from the remote service.

So:

1) Does the local storage variable name have to match the {command} tag name used in the service? (And if so does the variable need curly braces in its name?) Currently it doesn't match or have curly braces.

2) Is there a problem using "list=yes" as the contents of a storage variable? I'm worried that the equals sign might be causing issues.

Feels like I'm close to having this working but not quite there yet.

Thanks again.

Steve


Example of using a Dynamic URL with REST service please?

Posted: Thu Aug 15, 2013 4:33 pm
by Kateryna Grynko

Hi Steve,

In your case, it will be easier to call the service with own parameters in the JavaScript code.

The format of the call for service is:
codeservice_name.execute({

success: function( PlainObject data, String textStatus, jqXHR jqXHR ) {
//Success handler here
},

error: function( jqXHR jqXHR, String textStatus, String errorThrown ) {
//Error handler here
},

complete: function( jqXHR jqXHR, String textStatus ) {
//Complete handler here
},

data: { object with request params },
headers: { object with request header params }
});/code
any of the object parameters can be not used.

It is important. If you override some request parameter then it will be ignored from the list of configured in the IDE.


Example of using a Dynamic URL with REST service please?

Posted: Mon Aug 19, 2013 11:09 am
by steveharman

Hi Katya,

Thanks for the reply.

As I'm just getting started with understanding JQuery I may on this occasion take the lazy way out and define four separate predefined Services rather than passing dynamic commands to a generic Service; one service URL predefined with the REST service's "list" function, one as "delete", one as "add" and one as "update".

Then just map an Appery component's value to the "delete", "add" "list" or update services as dictated by the button concerned (delete, list, add etc.)

This isn't as elegant as your suggestion but presumably still OK to do?

Regards,

Steve


Example of using a Dynamic URL with REST service please?

Posted: Mon Aug 19, 2013 1:24 pm
by Kateryna Grynko

Hi Steve,

It's probably even better solution for your app - you divided all the activity into the atomic parts, and it is more reliable than the complex logic of one service request modification.