Page 1 of 1

APIs - How to?

Posted: Fri May 07, 2021 7:56 am
by Fromm603
So, I am a green screen guy, still kind of new to LANSA. Is there a good source to learn (or find open source) for doing APIs ? Specifically, I want to do a put to UPS (need to send return labels), then I need puts and gets for Shopify.
Are there functions in LANSA to do this?

I will not be publishing, only consuming. Code examples would be awesome.

Re: APIs - How to?

Posted: Fri May 07, 2021 12:25 pm
by Dino
Hi, The documentation for web services is a good place to start here:
https://docs.lansa.com/15/en/lansa018/index.htm#

most of those sites today are use restful apis, basically, they are sending plain text in json format that you can receive in your program.

either way, you could use the LANSA INTEGRATOR HTTPSERVICE to receive that text file and then process it (bind it or even manually)
or using the primitives for restful available today (preferred method, easier) from web pages/server modules.

are you in v14, v15?
are you planning to access this from server modules? web page? or functions?

Re: APIs - How to?

Posted: Fri May 07, 2021 12:26 pm
by Dino
Also there is a nice course in https://learn.lansa.com/courses/lansa-v ... -with-apis , you will need to email to request the access to the course.

Re: APIs - How to?

Posted: Fri May 07, 2021 10:53 pm
by Fromm603
Thanks !! I am actually hoping to call this from an RPGLE program on the IBMi.

Re: APIs - How to?

Posted: Fri May 07, 2021 11:26 pm
by Fromm603
And I am on V15 EPC 150020.

Re: APIs - How to?

Posted: Sat May 08, 2021 12:38 pm
by Dino
Usually to consume restful apis, you will have a web page, or a server module doing the job.

But if you want to have an RPG program doing the job, maybe calling a LANSA program to do that for you, that program will have to be a function and you will need LANSA INTEGRATOR license, service HTTPSERVICE.

A LANSA function like this can read a page with json information like this http://jsonplaceholder.typicode.com/todos/1, which can have json information and return that information to you in a file in the IFS. You can access that file directly or use other services in LANSA INTEGRATOR to bind the information in that text file with json content to result to fields. If you need to add security and stuff, more steps are needed, but in the uri you could include some parameters as well. It is simpler to have a web page/server module to consume restful than this, but both are options available.

Code: Select all

Function Options(*DIRECT)

* Open service
Use Builtin(JSMX_OPEN) To_Get(#JSMXSTS #JSMXMSG #JSMXHDLE1)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)
* Load service
Change Field(#JSMXCMD) To('SERVICE_LOAD SERVICE(HTTPSERVICE) TRACE(*YES)')
Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)

* Access the web page http://jsonplaceholder.typicode.com/todos/1
Change Field(#JSMXCMD) To('SEND HOST(jsonplaceholder.typicode.com) CONTENT(*HTML) URI(/todos/1) HANDLER(OutboundFile) METHOD(GET) WAIT(*YES)')
Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)

Change Field(#JSMXCMD) To('GET PROPERTY(STATUS-MESSAGE)')
Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)

* Receive the content and put it in a text file in the ifs in the LANSA Integrator instance folder
Change Field(#JSMXCMD) To('RECEIVE HANDLER(IFILE) TO(typicode.json)')
Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 #JSMXCMD) To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)

* Unload service
Use Builtin(JSMX_COMMAND) With_Args(#JSMXHDLE1 'SERVICE_UNLOAD') To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)
* Close service
Use Builtin(JSMX_CLOSE) With_Args(#JSMXHDLE1) To_Get(#JSMXSTS #JSMXMSG)
Execute Subroutine(CHECK) With_Parms(#JSMXSTS #JSMXMSG)
return

* Check routine
Subroutine Name(CHECK) Parms((#JSMXSTS *RECEIVED) (#JSMXMSG *RECEIVED))
If Cond('#JSMXSTS *NE OK')
Use Builtin(JSMX_CLOSE) With_Args(#JSMXHDLE1) To_Get(#JSMXSTS #JSMXMSG)
Message Msgtxt('LANSA INTEGRATOR found a problem')
Endif
endroutine