Ben Langhinrichs

Photograph of Ben Langhinrichs

E-mail address - Ben Langhinrichs






July, 2020
SMTWTFS
   01 02 03 04
05 06 07 08 09 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

Search the weblog





























Genii Weblog


Civility in critiquing the ideas of others is no vice. Rudeness in defending your own ideas is no virtue.


Thu 9 Jul 2020, 11:33 AM
This is a follow up to my post, From REST to Notes db in two seconds. In that, we use the results of a call to a public REST API to created a form, fields, view, and documents. Cool, but often you just want to create an instant little mini-report in a rich text field. Let's start at the end this time. There is a nifty REST API to retrieve foreign exchange rates. You call it at https://open.exchangerate-api.com/v6/latest. An example is today's results, which are 
 
{"result":"success","documentation":"https://www.exchangerate-api.com/docs/free","terms_of_use":"https://www.exchangerate-api.com/terms","time_last_update_unix":1594253195,"time_last_update_utc":"Thu, 09 Jul 2020 00:06:35 +0000","time_next_update_unix":1594340705,"time_next_update_utc":"Fri, 10 Jul 2020 00:25:05 +0000","time_eol_unix":0,"base_code":"USD","rates":{"USD":1,"AED":3.67,"ARS":70.88,"AUD":1.44,"BGN":1.73,"BRL":5.36,"BSD":1,"CAD":1.36,"CHF":0.94,"CLP":791.55,"CNY":7.01,"COP":3608.55,"CZK":23.69,"DKK":6.6,"DOP":58.2,"EGP":15.98,"EUR":0.885,"FJD":2.16,"GBP":0.796,"GTQ":7.7,"HKD":7.75,"HRK":6.68,"HUF":314.67,"IDR":14199.6,"ILS":3.45,"INR":74.99,"ISK":139.34,"JPY":107.48,"KRW":1196.48,"KZT":409.22,"MXN":22.72,"MYR":4.27,"NOK":9.44,"NZD":1.53,"PAB":1,"PEN":3.53,"PHP":49.53,"PKR":166.43,"PLN":3.96,"PYG":6615.67,"RON":4.29,"RUB":71.27,"SAR":3.75,"SEK":9.22,"SGD":1.39,"THB":31.23,"TRY":6.86,"TWD":29.42,"UAH":26.85,"UYU":43.29,"ZAR":17}}
 
Even if you don't deal with JSON much, you could probably pick out that the exchange rate with CAD (Canadian dollar) is 1.36. We could certainly use the new JSON classes in Notes to traverse the JSON, find the values and build a report, but our Midas LSX is all about maximizing what you can do while minimizing the effort to do it. So, my business users want a snapshot of four specific exchange rates. I decided to present them like this:
 
Inline JPEG image
But how did I get from the JSON returned to this table? As a practical matter, I first just used our AppendFieldsWithJSON method to create fields from everything. It looked like this (and goes on and on until ZAR):
 
Inline JPEG image
 
You can see that when our method hits an object inside the object (this one called rates), it presents the values like this. But I wanted the rates presented more nicely, so I switched the properties of the AppendFieldsWithJSON to start down at the rates object (by setting ChildOf='rates'), and specify it as a vertical table inside a single tabbed table with a label. That looked like this:
 
Inline JPEG image
 
This is close, but I only want my four currencies, and I think the background should be green. Oh, and I'd like the labels to reflect the commonly used names for these currencies. So, I change the properties string a bit to include a comma-delimited list of the items I want and the way I want them to appear.
 
props = "AsTable='vertical' AsObjectArray='yes' ChildOf='rates' Items='AUD=Australian Dollar,CAD=Canadian Dollar,EUR=Euro,JPY=Japanese Yen' "
 
After a bit more tweaking of the color and tab label, I get my final code:
 
Sub Initialize
   DimAs New NotesSession
   Dim db As NotesDatabase
   Dim doc As NotesDocument
   Dim http As NotesHTTPRequest
   Dim gs As New Geniisession
   Dim rtitem As New Geniirtitem
   Dim rtchunk As Geniirtchunk
   Dim props As String
   Dim col1props As String
   Dim col2props As String
   Dim json_val As String 
 
   Set db = s.CurrentDatabase
   Set doc = New NotesDocument(db)
   doc.Form = "JournalEntry"
   doc.Subject = "Exchange rate data as of "+CStr(Now)
 
   ' *** Create the rich text item, overwriting it if it already exists
   Call rtitem.CreateBackend(doc.Handle, "Body"True)
 
   ' *** Add the title, and make everything Verdana to look better
   Call rtitem.DefineFont("Verdana")
   Set rtchunk = rtitem.DefineChunk("Everything")
   rtchunk.Font = "Plain 10pt Verdana"
   Call rtchunk.AppendTable(11"RowDisplay='Tabbed' TabFont='12pt Bold Verdana' BorderEffects='Drop_Shadow' TableWidth='Fixed' ", _
   "Text='' Width='3in' CellColor='RGB127,255,127' TabLabel='FX Rates with US Dollar'")
   Call rtchunk.ZoomIn("Table 1; Row 1; Inside column 1")
 
   ' *** Retrieve the JSON to use
   Set http = s.CreateHTTPRequest()
   http.PreferStrings = True
   json_val = CStr(http.get("https://open.exchangerate-api.com/v6/latest"))
 
   props = "AsTable='vertical' AsObjectArray='yes' ChildOf='rates' Items='AUD=Australian Dollar,CAD=Canadian Dollar,EUR=Euro,JPY=Japanese Yen' "
   col1props = "Text='' TextFont='Bold #004080'"      
   col2props = "Text='' TextFont='-Bold #004080'"      
 
    ' *** Appends the fields to the form and save
   Call rtchunk.AppendFieldsWithJSON(json_val, props, col1props, col2props)
   Call rtitem.Save()    
End Sub
 
and when I run that, I am back to the beginning. This could be the entire rich text field, or you could have an agent just drop it in where you want in an existing rich text. 
Inline JPEG image
A lot of power with a small amount of code, and it is easy enough to tweak and change and run again so you can design and create your look without spending a ton of time designing and creating your code. That's the Midas LSX advantage,

Copyright 2020 Genii Software Ltd.