No edit summary |
|||
Line 22: | Line 22: | ||
=== Exposing ourselves as a REST service === | === Exposing ourselves as a REST service === | ||
And when it comes to '''exposing ourselves to others''' – Turnkey has two new MVC verbs | And when it comes to '''exposing ourselves to others''' – Turnkey has two new MVC verbs, Get and Post. | ||
TurnkeyRest/Get?command=vmname&id=rootobjref | TurnkeyRest/Get?command=vmname&id=rootobjref | ||
TurnkeyRest/Post?command=vmname&id=rootobjref | TurnkeyRest/Post?command=vmname&id=rootobjref | ||
==== NOTE! the correct controller name is '''TurnkeyRest''' - this was earlier <s>MDrivenRest</s> ==== | |||
What they do is that they look for ViewModel named as the command-parameter | What they do is that they look for ViewModel named as the command-parameter. | ||
When found the tagged value '''RestAllowed''' is checked and the [http://wiki.mdriven.net/index.php/Part_9_MDriven_Turnkey,_cloud_tools_and_access_groups accessgroups] are checked to see if access is allowed. | |||
Then additional parameters (these can be either URL Encoded or multi-part form encoded [https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST See HTTP POST]) are set | |||
* For Get, the parameter name is lookup up against ViewModel variables and given the sent value. | |||
* For Post, the parameter name is lookup up against ViewModel variables '''and''' '''attributes''' and given the sent value. | |||
Finally any actions present at the root level of the ViewModel are executed. | |||
Post saves any changed values to the database. | |||
Both Get and Post returns the viewmodel content as json in the HTTP response. | |||
If there is an error – a string “error: <message>” is returned. | |||
=== Video === | === Video === |
Revision as of 11:55, 17 July 2019
REST services are services that are executed by connecting to an URL that defines operation and parameters then it returns an answer – not seldom as JSon objects.
You must set the tagged value RestAllowed on ViewModels you want to allow Rest access to.
Calling existing REST services
MDriven supports a couple of EAL operators to manage REST services. All operators reside on the selfVM variable – available only in the ViewModel context.
selfVM.RestGet(targeturl,user,pwd,optionalnestingwithheaders)
selfVM.RestPost(targeturl,user,pwd,optionalnestingwithheadersAndUploadValues)
selfVM.RestDownload(targeturl,user,pwd,optionalnestingwithheaders)
There is a also a new helper operator on selfVM:
selfVM.JSonToObjects( «<Type>» , JSonDataInStringFormat)
The selfVM.JSonToObjects creates objects of class Type and matches attributes and association from the json data – and it can create object trees (unclosed graphs) by following names on associations. These few additions enables us to consume Rest services that others expose.
Example
Here is a real life example
First Action GetExporttest retrieves data by converting another viewmodel to xml - it stores it in the variable vText
Next action invokes RestPost to send that data to an url-address, it also says that it should look at the nesting named 'Xml' - in this nesting we have the STRINGCONTENT (see also OCLOperators RestPost) data and we assign it to vText - we also add the header Authorization with a bearer token in order to get access from the receiving service.
Exposing ourselves as a REST service
And when it comes to exposing ourselves to others – Turnkey has two new MVC verbs, Get and Post.
TurnkeyRest/Get?command=vmname&id=rootobjref
TurnkeyRest/Post?command=vmname&id=rootobjref
NOTE! the correct controller name is TurnkeyRest - this was earlier MDrivenRest
What they do is that they look for ViewModel named as the command-parameter.
When found the tagged value RestAllowed is checked and the accessgroups are checked to see if access is allowed.
Then additional parameters (these can be either URL Encoded or multi-part form encoded See HTTP POST) are set
- For Get, the parameter name is lookup up against ViewModel variables and given the sent value.
- For Post, the parameter name is lookup up against ViewModel variables and attributes and given the sent value.
Finally any actions present at the root level of the ViewModel are executed.
Post saves any changed values to the database.
Both Get and Post returns the viewmodel content as json in the HTTP response.
If there is an error – a string “error: <message>” is returned.
Video
To make your experience more comfortable, we set the main tags mentioned in the video to the right bar menu of this mini player. Choose the interesting subtitle on the list and immediately get to the exact theme navigation-itemplace in the video. Now you can pick any topic to be instructed without watching the whole video.