Rest Services In MDriven
No edit summary
(Replacing message template with parser tag)
 
(57 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''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.
<message>Write the content here to display this box</message>
'''REST Services''' are services executed by connecting to a URL that defines operation and parameters and returns an answer – not seldom as JSON objects.


'''Calling existing REST services'''  
You must set the tagged value '''RestAllowed''' on the ViewModels you want to allow Rest access to.
MDriven supports a couple of EAL operators to manage REST services. All operators reside on the selfVM variable available only in the ViewModel context.
 
=== '''Calling Existing REST Services''' ===
MDriven supports a couple of EAL operators to manage REST services. All operators reside on the [[selfVM]] variable which is available only in the ViewModel context.
  ''selfVM.RestGet(targeturl,user,pwd,optionalnestingwithheaders)''
  ''selfVM.RestGet(targeturl,user,pwd,optionalnestingwithheaders)''
 
Read: [[OCLOperators RestGet]]
  ''selfVM.RestPost(targeturl,user,pwd,optionalnestingwithheadersAndUploadValues)''
  ''selfVM.RestPost(targeturl,user,pwd,optionalnestingwithheadersAndUploadValues)''
 
Read: [[OCLOperators RestPost]]
  ''selfVM.RestDownload(targeturl,user,pwd,optionalnestingwithheaders)''
  ''selfVM.RestDownload(targeturl,user,pwd,optionalnestingwithheaders)''
There is a also a new helper operator on selfVM:
Read: [[OCLOperators RestDownload]]
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.


And when it comes to '''exposing ourselves to others''' – Turnkey has two new MVC verbs:
'''Note!''' ''optionalnestingwithheaders'' is the '''name''' of the blue ViewModel class in the example below (as an OCL String).
TurnkeyRest/Get?command=vmname&id=rootobjref


TurnkeyRest/Post?command=vmname&id=rootobjref
==== Example ====
 
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, if one is found 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. If it is, additional parameters sent in the url are matched against ViewModel variables – and given corresponding values. Then any actions present in the root level of the ViewModel are executed. And then the Get verb packs the viewmodel content as json in the response.
 
The Post verb looks in the request values after names that match the ViewModel root – if match is found the corresponding value is applied. Then changes – if any – are committed to db. The last thing for post is that it packs the complete ViewModel as json in the response. If there is an error – a string “error: <message>”  is returned.
 
You must set RestAllowed on ViewModels you want to allow Rest access to.
 
=== Example ===
Here is a real life example
[[File:2018-05-29 10h31 45.png|none|thumb|689x689px]]
[[File:2018-05-29 10h31 45.png|none|thumb|689x689px]]
First Action GetExporttest retrieves data by converting another viewmodel to xml - it stores it in the variable vText
The action '''GetExporttest''' retrieves data by converting another ViewModel to XML - it stores it in the variable <code>vText</code>.
 
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.
 
=== Video ===
 
<html>
<p class="warn">
  <em>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.</em>
</p>
<br>
<div class="video">
  <div class="video__wrapper">
    <iframe src="https://www.youtube.com/embed/MKlQD8F1mz8?rel=0&autoplay=0" frameborder="0" allowfullscreen></iframe>
  </div>
  <div class="video__navigation">
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="10" tabindex="0"> What is REST? </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="82" tabindex="0"> How does it work with MDriven turnkey? </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="125" tabindex="0"> calling an existing REST service </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="191" tabindex="0"> exposing ourself as REST service </span>
 
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="244" tabindex="0"> MDriven turnkey app slot </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="333" tabindex="0"> how to expose information as the Rest service? </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="450" tabindex="0"> MDriven Rest/get strategy </span>
 
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="573" tabindex="0"> how to consume data? </span>
 
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="680" tabindex="0"> selfvm </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="716" tabindex="0"> Rest/Get operator </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="925" tabindex="0"> hard-coded objects </span>
 
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="1336" tabindex="0"> Json to objects operator </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="1784" tabindex="0"> update data with allow post </span>
 
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="1898" tabindex="0"> RestPost operator as a post command </span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="2490" tabindex="0"> operation execution</span>
    <span class="navigation-item" data-video="MKlQD8F1mz8" data-start="2558" tabindex="0"> vSomeParam adding new parameters </span>
 
 
  </div>
</div>


</html>
The next action invokes RestPost to send that data to a URL address; it also says it should look at the nesting named 'Xml'. In this nesting, we have the STRINGCONTENT (see also [[OCLOperators RestPost]]) which gets its content from the vText variable. We also add the header Authorization with a bearer token to get access from the receiving service.


'''See also''': [[Documentation:Exposing Ourselves as a REST Service|Exposing Ourselves as a REST Service]]
[[Category:Rest]]
[[Category:Rest]]
[[Category:Advanced]]
[[Category:Advanced]]
{{Edited|July|12|2024}}

Latest revision as of 07:55, 17 June 2024

REST Services are services executed by connecting to a URL that defines operation and parameters and returns an answer – not seldom as JSON objects.

You must set the tagged value RestAllowed on the 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 which is available only in the ViewModel context.

selfVM.RestGet(targeturl,user,pwd,optionalnestingwithheaders)

Read: OCLOperators RestGet

selfVM.RestPost(targeturl,user,pwd,optionalnestingwithheadersAndUploadValues)

Read: OCLOperators RestPost

selfVM.RestDownload(targeturl,user,pwd,optionalnestingwithheaders)

Read: OCLOperators RestDownload

Note! optionalnestingwithheaders is the name of the blue ViewModel class in the example below (as an OCL String).

Example

2018-05-29 10h31 45.png

The action GetExporttest retrieves data by converting another ViewModel to XML - it stores it in the variable vText.

The next action invokes RestPost to send that data to a URL address; it also says it should look at the nesting named 'Xml'. In this nesting, we have the STRINGCONTENT (see also OCLOperators RestPost) which gets its content from the vText variable. We also add the header Authorization with a bearer token to get access from the receiving service.

See also: Exposing Ourselves as a REST Service

This page was edited 9 days ago on 06/17/2024. What links here