Sunday 1 November 2015

DXL and Cloudant documents

It was a very busy month and I didn't have much time to write something interesting. Today I decided to do a quick tutorial how to use HTTP perms.

You can read more on HTTP Perms in DOORS DXL Reference

Reading Cloudant documents

We know DOORS integration is not only outgoing to outside World, sometimes we need to get data into DOORS. Today I will show you a very short DXL script which will let you get data from external database. Because of ease of use and simple connectivity I decided to connect to Cloudant.

If you are not familiar with Cloudant, have a quick look at its Learning Center. In short Cloudant is a NoSQL database-as-a-service (DaaS) widely used with Bluemix Mobile and Cloud applications. But let's not jump into my future post and let me show you how can you read Cloudant documents from DOORS DXL.

As you know Cloudant uses HTTP requests and my script will focus on DXL HTTP requests.

Buffer getData(string username, password, account, db, doc)
{
  Buffer buf = create
  buf = "https://" username ":" password "@" account ".cloudant.com/" db "/" doc
 
  HttpHeader h = create
  HttpResponse resp = HttpRequest(HttpGet, tempStringOf buf, null, h)
  delete h
  delete buf

  if (!null resp && resp.isOk)
  {
    HttpBody b = resp.body
    Buffer respBuf = create
    respBuf += b.value
   
    delete resp
    return respBuf 
  }
  else {
    if (!null resp) {
      print "error getting response " resp.code""
      delete resp
    }
    else {
      print "connection error"
    }
  }

  return null
}

Buffer b = getData("", "", "ae4582eb-c21f-4c1a-92b3-8da8589eea0c-bluemix", "doors9test", "aee5f8c1be606328ddedef1a546cb7ca")

if (!null b)  {
  print tempStringOf b
  delete b
}


I set read permissions on this Database so everyone should be able to get following JSON:

{"_id":"aee5f8c1be606328ddedef1a546cb7ca","_rev":"1-f44612cc036915a9f12f1afb7ba2fc37","title":"DOORS 9 connection test","description":"You shall read this ;)"}

Writing documents back to Cloudant


If you would like to PUT something to Cloudnant database you can use the same HttpRequest but this time you need to set HttpHeader Content-Type. This is required buy HttpRequest with HttpPut verb, otherwise an error will be reported. Setting header values is done with add perm:

HttpHeader h = create
add(h, "Content-Type", "application/json")  // minimum required header
add(h, "Accept", "application/json")

setValue perm is used to set HTTP body:
HttpBody body = create
Buffer buf = create
buf = {"_id":"aee5f8c1be606328ddedef1a546cb7ca","_rev":"1-f44612cc036915a9f12f1afb7ba2fc37"}
setValue(body, buf)
HttpResponse resp = HttpRequest(HttpPut, url, body, h)
That's how you can simply put and get data from the Cloudant!

Soon I will do some more fun stuff with DXL, Bluemix and IoT but for now...

Happy Halloween!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.