Thursday, 14 April 2016



Call an RMP REST Web Service from a Xamarin mobile app

This entry shows how to call a RESTful Fujitsu RunMyProcess (RMP) Web Service from a mobile Xamarin App.

Intro

First we will create a datastore within RMP and then create a web service to retrieve data from it. The Second part shows how to create the Xamarin app and how to access the web service.   The datastore (called a Collection within RMP) will contain contact information such as name and email address.   The Xamarin app will call the Web Service using a name and retrieve the email address from RMP and display it . 

RMP Web Service and Collection

Create a project

From the RMP ide select new project.   I have called the project 'Demo Rest Call' and selected the entity that will have rights to edit this project.   I have selected the 'Fujitsu' group that was automatically created to match the name of the company I registered during the RMP registration process. 


Create Collection

Create a new collection by selecting new and then Collection within the project just created.


Call the new collection 'Contacts', make the collection public by ticking box and then save it by clicking the icon of the disk. 


Create Test Data 

Normally you would create a web interface to allow entry of data into the collection, but for the purposes of this example we will create a Composite API that will directly add a row into the collection.  So to start create a new Composite API from the project new menu.  Call it 'capi - Create Test Data' and select the same entity as before, in my case its called 'Fujitsu'. Create the process flow as follows.


The test data will be created by inserting a new document into the test collection via the process flow.  Open the output variables tab of the process and then select the script icon to allow entry of freemarker code. 


Next enter the following freemarker code into the editor.  This will insert one document into the test collection that we will retrieve via the mobile app. select the test button and get confirmation from the output window the document has been added.


Create the RMP Web Service

Create another Composite API from with the new project and call it 'capi - Get Contact', selecting your company as the entity again.  The web service takes a name as an input, searches for a contact with the same name in the 'Contacts' collection and then returns the email address associated with contact.




Create process as follows in the process designer view of the new composite API.





Next we need to add an output variable to the start node.  This will retrieve the name of the contact we need to search for from the input parameters. add an name variable and value ${name} to retrieve the name from the incoming JSON request.




We now need to add code to search the collection for the name and then return the email address associated to the contact.   Open the properties tab of the 'Get Contact' process and create a new attribute called email.




Then with in the script of the new attribute add the following freemarker to retrieve the email

<#assign query >
  {"name": "${name}"}
</#assign >
<#assign contact = load_object(query,"Contacts")>
${contact.email}

The created web service address can be retrieved from configuration tab of the Composite API.
for example the address of mine is:

https://live.runmyprocess.com/live/xxxxxxxxxxx/host/141905/service/225679?P_version=${P_version}&P_mode=TEST

I have replaced part of it with x's so you cant break mine and set the mode to test mode by setting the P_mode to TEST.

The web service is secured by Basic Auth and so your username and password for RMP needs to be added to the header of the request calling the web service also the Content-Type will be 'application/json' in our case.  The request body for our example will look like the following JSON.

{ "name" : "Ben" }


Xamarin Android App


Here we create a Xamarin Android app that retrieves an email address using a name that is entered.  This app issues a request to the RMP REST service.  



In our example app we send the name in a HTTP request, the resulting email address information is returned in JSON format. 
First we create a Xamarin Android app, the example screenshots in this article were taken using the Android Developer Preview. 



The view can be created using the Designer view, first replace the contents of the Resources/layout/Main.axml with the xml example below.   The view contains an EditText field to allow entry of a name, a button used to initiate the web service call and get the email and then a finally a TextView to display the retrieved email address. 




Next we need to wire up the Activity to the view.  Within the OnCreate method we connect the name and email fields from the view and add a click event to call the web service. In the MainActivity subclass add the following code.



The FetchEmailAsync method creates the request JSON, adds the basic auth header, posts the request and then parses the response.   The username and password will need to match the one you have setup within RMP.  



The source for the Xamarin project can be found in github