Exposing MDriven Turnkey as REST Services
MDriven Turnkey allows you to expose your applications as REST services, enabling external systems to interact with your MDriven data. This tutorial guides you through setting up a ViewModel to act as a data source for REST requests, using MDrivenRest/get and MDrivenRest/post to expose and update data, and executing operations via REST.
Setting Up a ViewModel for REST
To expose a ViewModel as a REST service, configure it to act as a data source:
- Open your ViewModel in MDriven Designer.
- Select the head of the ViewModel and set the property
RestAllowedtotrue.- #: This setting allows the ViewModel to be accessed via REST requests.
- Upload the updated model to your MDriven Turnkey site.
Once configured, your ViewModel is ready to be accessed using REST.
Using MDrivenRest/get
The MDrivenRest/get endpoint allows you to retrieve data from your ViewModel. To access data:
- Construct the URL using the format:
/MDrivenRest/get?ViewModelName=YourViewModelName&RootObjectId=YourRootObjectId.- #: Replace
YourViewModelNamewith the name of your ViewModel andYourRootObjectIdwith the ID of the root object.
- #: Replace
- Access the URL to retrieve data in a REST format.
This method returns the data as a REST document, which can be consumed by external systems.
Using MDrivenRest/post
To update data via REST, use the MDrivenRest/post endpoint. Here's how:
- Create a new ViewModel action to handle the POST request.
- Define a nesting in your ViewModel to hold the data you want to update.
- Use the
selfVM.RestPostoperator in an action to send data.- The
RestPostoperator requires the target URL, user, password, and nesting name as parameters.
- The
For example, to update a registration number, you can:
selfVM.RestPost('http://yourdomain.com/MDrivenRest/post?ViewModelName=EditOneCar&RootObjectId=' + self.ExternalId, '', '', 'DataToPost')
This command posts the data in the DataToPost nesting to the specified ViewModel.
Executing Operations via REST
REST services in MDriven can also execute operations defined in your ViewModel. Actions available at the root level of a ViewModel will be executed during both GET and POST requests.
- Define actions in your ViewModel that modify data or perform operations.
- Use parameters in your REST URL to pass additional data to these actions.
For instance, you can append parameters to your URL like &SomeParam=Value to influence the behavior of the action.
Example: Updating Data via REST Post
Consider a scenario where you want to update a car's registration number:
- Set up a ViewModel with a variable
vNewRegNumto hold the new registration number. - Create a generic column in your ViewModel to display this variable.
- Define an action using
selfVM.RestPostto send the updated registration number back to your system.
This approach allows you to update data in your MDriven Turnkey application using REST.
By following these steps, you can effectively expose your MDriven Turnkey applications as REST services, enabling external systems to interact with your data efficiently. For more on consuming REST services, see Documentation:Rest Services In MDriven.
Source
Based on the MDriven video MDriven Turnkey | REST services in MDriven.
