IBMi APIs provide many tools that a 2E programmer might need. There are also different ways a programmer can interface with the APIs. The first method is to write a native RPG program as a user program and include that program within the 2E model. Once it is written any programmer can call the user program from their 2E function.

api1

Lets look at the advantages verses the disadvantages of using this method.

Advantages
1. API interface normally is written in fewer functions..
2. Native code bypasses any limitations of 2E.

Disadvantages
1. Someone on the team needs to have sufficient experience to write the native RPG or COBOL.

Lets look at a sample of using a user program to interface with an IBM API. In our example we will write a program that will verify the existence of a file on the IFS. We will also check the security level of the object on the IFS.

Here is the RPGLE source code needed to call the IFS API:

h option(*nodebugio) dftactgrp(*NO)
*
daccess PR 10I 0 extproc(‘access’)
dpathptr1 * value
dmode1 10I 0 value
*
*IFS API Constants
*
DF_OK S 10I 0 inz(0)
DR_OK S 10I 0 inz(4)
DW_OK S 10I 0 inz(2)
DX_OK S 10I 0 inz(1)
*
* Working variables
*
dFile_exists S 10I 0
dpathptr S *
dpathname S 101

d$filename s 100A
d$path s 50A
d$file s 50A
d$found s 1A
d$read s 1A
d$write s 1A
d$execute s 1A
*
* Main —
*
c *entry plist
c parm $path 50
c parm $file 50
c parm $found 1
c parm $read 1
c parm $write 1
c parm $execute 1

* Set a character pointer to the file name string
c eval $filename = %trim($path)+’/’+%trim($file)
c eval pathname = %trim($filename)+x’00’
c eval pathptr = %addr(pathname)
c eval $found = ‘N’
c eval $read = ‘N’
c eval $write = ‘N’
c eval $execute = ‘N’
* Call the IFS API
c eval File_Exists = access(pathptr:F_OK)
* Did we find it?
c File_exists ifeq 0
c eval $found = ‘Y’
c endif
* If file is found !
c if $found = ‘Y’
* Call the IFS API
c eval File_Exists = access(pathptr:R_OK)
* Do we have Read access ?
c File_exists ifeq 0
c eval $read = ‘Y’
c endif
*Call the IFS API
c eval File_Exists = access(pathptr:W_OK)
* Do we have Write access ?
c File_exists ifeq 0
c eval $write = ‘Y’
c endif
* Call the IFS API
c eval File_Exists = access(pathptr:X_OK)
* Do we have Execute access ?
c File_exists ifeq 0
c eval $execute = ‘Y’
c endif

c endif

c move *on *inlr
c return

In the *entry section you see the values that will need to be included for the parameters of this function. These parameters provide the interface 2E needs to the function.

Path 50 characters I
File 50 characters I
Found 1 character O
Read 1 character O
Write 1 character O
Execute 1 character O

ibmapi2

Once you have created the user program and added the correct parameters to the user program it is simple to call the function from within any 2E function. In this example a prompt record was created with the fields needed on the screen to send and receive the parameter values.

ibmapi3

In this article you have seen one example of creating a user program as an interface to an IBM API. For the next blog entry lets see if we can call this same API directly with 2E.

The RPG code was borrowed from this site: Sample RPG code