Micro Service

On the fly micro services

  • Boodskap gives you an interface where you can write micro-services and expose them internet on the fly without needing any 3rd party plugins / sdks / frameworks

  • The exposed micro-services can be secured or unsecured of your choice

  • Following access mechanisms are supported

    • Open to All (Unsecured)
    • Access Token (TOKEN in HTTP Header)
      • Use the platform's user login tokens
      • Further role based security can be given to individual methods of the exposed APIs
    • API Key (KEY in HTTP Header)
      • Provide your own API key (or)
      • Use platform's builtin API key
    • Parameter variables (Custom parameter variable)
      • Your API can handle custom parameter(s) internally
  • Context Variable: micro

Insight

Platform offers a great deal of functionality to build end-to-end solutions. Micro Services module gives you the freedom to combine such functionalities and expose them as REST services for your UI / UX or for the remote devices to communicate using the services directly skipping the standard messaging protocols.

Example

Let's take a simple solution where you need to collect some water parameters of a aquaculture farm, you can expose a simple micro service to push the data into platform records and then perform a variety of analytics to build a dashboard or notification system, etc.

{
  "ph": "INTEGER",
  "nh3": "INTEGER",
  "nh4": "INTEGER",
  "o2": "INTEGER",
  "turbidity": "INTEGER"
}
import io.boodskap.iot.MicroApi;
        
@MicroApi(
          desc = "Water sensor parameters",
          params = ["o2", "ph", "nh3", "nh4", "turbidity", "stamp"],
          types = ["int", "int", "int", "int", "int", "long"], // if declared, make sure it matches the params[]
          optional = ["stamp"],
          slug = "collect" // optional short name for REST API access
)
def collectSample(def args) {

    if(null == args['stamp']){
        args['stamp'] = util.millis();
    }
    
    record.insert(2000, args);
    
    return ["status": "OK"];
}

Content-Type Example

By default, the micro service framework will accept a JSON input and JSON output, you can change this behavior easily by overriding the defaults

@MicroApi(
          desc = "XML Example",
          params = [],
          types = [], // if declared, make sure it matches the params[]
          required = [],
          roles = [], // domain roles, empty roles for open access
          slug = "" // optional short name for REST API access
)
def asXML(def args) {

    def res = [status:200, type:'application/xml', headers:['MyHeader: xmltype'], body:'<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<sum>100</sum>'];
}

Your response map can contain, status, type, headers and body parameters

File Upload Example

@MicroApi(
          desc = "Upload Example",
          params = [],
          types = [], // if declared, make sure it matches the params[]
          required = [],
          roles = [], // domain roles, empty roles for open access
          slug = "" // optional short name for REST API access
)
def uploadFile(def args) {

    def res = [:];
    
    res['size'] = args.stream.available();
    res['file'] = args.file.fileName;
    res['file_type'] = args.file.contentType;
    
    return res;
}

You can Invoke the API by a simple post call

curl -X POST "https://your-host/api/micro/service/call/upload/Your-Slug/YourApiClass/uploadFile" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "[email protected];type=application/pdf"
{"size":160432,"file_type":"application/pdf","file":"YourFile.pdf"}

File Download Example

@MicroApi(
          desc = "Download Example",
          params = [],
          types = [], // if declared, make sure it matches the params[]
          required = [],
          roles = [], // domain roles, empty roles for open access
          slug = "download" // optional short name for REST API access
)
def downloadFile(def args) {
    http.setContentType("text/plain");
    return new java.io.ByteArrayInputStream("My file content".getBytes());
}

You can invoke the API by this simple call

curl -X GET "https://your-host/api/micro/service/call/upload/Your-Slug/YourApiClass/downloadFile" -H "accept: text/plain"
My file content

Related REST APIs

Invoke a dynamic micro-api
Create / Update Micro Api
Retrieve Micro Api
Count All Micro Apis
Delete Micro Api
Delete all Micro Apis
Search Micro Apis
Create or Update Micro Api Slug
Delete Micro Api Slug ID
Retrieve Micro Api Slug ID