BackendService - REST API example

Implement REST API for your backend application

The following example illustrates how to respond with a server-side code on a REST API call.

import { ApplicationBackendServiceBase, BackendServiceEventTypes } from '@dms';
import { MyEvent } from '@dms-bo';
export * from '@dms';
export * from '@dms-bo';

export default class BackendService extends ApplicationBackendServiceBase {

    public async processEvent(
         event: { eventName: string;
         eventArguments: {
                appArea: string;
                appCode: string;
                userName: string;
                request: {
                    httpMethod: string;
                    restCommand: string;
                    headers: any;
                    body: any;
                    queryStringParameters: any;
                };
          };
        }): Promise<any> {

        switch (event.eventName) {
            case "system:api:inbound": {
              switch (event.eventArguments.request.restCommand) {
                    case 'event':{ 
                      if(event.eventArguments.request.httpMethod=='GET'){
                          const myEvents = await MyEvent.query().executeSelect();
                          return { statusCode: 200, events: myEvents };
                      }
                      else
                      if(event.eventArguments.request.httpMethod=='POST'){
                          const myNewEvent = event.eventArguments.request.body;
                          await MyEvent.query().executeCreate(myNewEvent);
                          return { statusCode: 200 };
                      }
                      else {
                          return { statusCode: 400, message: "unsupported http method" };
                      }
                    }
                    case 'random':{ 
                      if(event.eventArguments.request.httpMethod=='GET'){
                          const randomValue = Math.random(1000);
                          return { statusCode: 200, value: randomValue };
                      }
                      else {
                          return { statusCode: 400, message: "unsupported http method" };
                      }
                    }
                    default: return { statusCode: 400 }
                }
                break;     
            }
            default: return;
        }
    }
}

The snippet allows now an external client to do the following remote calls:

GET /livelink/app/MOBFL/event HTTP/1.1
Host: api.dynamicsmobile.com
Authorization: apparea:RA202051203845;eyJraWQiOiIzTkRoNlh........
Content-Type: application/json


Response
--------------------  
Http Staus Code: 200

Body:
{
 statusCode: 200,
 events: [
  {DMS_ROWID: "xyz-1232-123-sada-a2", onDateAndTime: "2020-01-01T23:21:21"},
  {DMS_ROWID: "ad3-234-133-sada-v2", onDateAndTime: "2021-01-03T13:12:34"},
  {DMS_ROWID: "kal-293-253-sad3-r2", onDateAndTime: "2020-02-01T15:13:45"}
  ] 
}

POST /livelink/app/MOBFL/event HTTP/1.1
Host: api.dynamicsmobile.com
Authorization: apparea:RA202051203845;eyJraWQiOiIzTkRoNlh........
Content-Type: application/json
Content-Length: 321

[
    {
        "onDateAndTime": "2012-01-01T23:59:39"        
    }
]

Response
--------------------  
Http Staus Code: 200

Body:
{
 statusCode: 200 
}

GET /livelink/app/MOBFL/random HTTP/1.1
Host: api.dynamicsmobile.com
Authorization: apparea:RA202051203845;eyJraWQiOiIzTkRoNlh........
Content-Type: application/json

Response
--------------------  
Http Staus Code: 200

Body:
{
 "statusCode": 200,
 "random": 1000 
}

PUT /livelink/app/MOBFL/random HTTP/1.1
Host: api.dynamicsmobile.com
Authorization: apparea:RA202051203845;eyJraWQiOiIzTkRoNlh........
Content-Type: application/json

Response
--------------------  
Http Staus Code: 400

Body:
{
 "statusCode": 400,
 "message": "unsupported http method"
}

Last updated

Dynamics Mobile provided by Mobile Affairs Ltd. | 1712 Sofia, Bulgaria, Alexander Malinov 51 | sales@dynamicsmobile.com