04
Mon, Nov
1 New Articles

The ABCs of APIs

APIs
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

When IBM wants to provide more functionality, it either builds it into the operating system or provides another API. Each release of OS/400 has more APIs than the last. If you don’t know how to take advantage of APIs, you may not be getting everything out of the system that you could be. A great deal of really useful information is in your system, but you must know how to use APIs to get to it.

The AS/400 has a great command interface. If you want to perform a function, find the command that will take you there. Commands that return data will usually do so by sending data back to the screen, to a spool file, or to an outfile. An outfile is a simple database file where you can store the output that a command produces. Although outfile processing works well, it does not work fast. If you want speed, you need to use an API.

The more you know about APIs, the more likely you are to find ways to use them. Using APIs is not very difficult and is very efficient. The key to using APIs is to call the API and pass it the correct parameters. Most of the work associated with APIs lies in handling parameters. Some parameters, such as file name and library name, are simple. Other parameter passing is more complex. More complex parameters contain items such as variable length data structures, binary fields, or pointers. These can be difficult to get just right.

Application Programming Interface 101

APIs have two distinct methods of returning data. Some APIs return data as a data structure (or series of data structures) within a parameter, and other APIs return the data via an object called a user space. Let’s talk about how the data structure method works first.

The Retrieve Object Description (QUSROBJD) API is a prime example of how to return data using data structures. After calling the API (Figure 1), the 100-byte variable ReceiveVar will contain information about the object named in the fourth parameter, ObjectLib. The layout of the data within this variable is dependent upon the file format name we enter in the third parameter, FileFmt. Because I have entered OBJD0100 as our file format, this API will return the least information possible. Format OBJD0200 will return all of OBJD0100 and a little more, OBJD0300 will return all of OBJD0200 and a little more, and so on. Each format requires that you define a larger ReceiveVar parameter. Most of the APIs use this technique of passing a predefined file format name to describe the data you want the API to return.

In the second parameter, ReceiveLen, we define the length of the ReceiveVar parameter. ReceiveLen is a 4-byte binary field. Here’s a tip: When you define a binary field as a standalone field, you must define it with a length of 9. If you define it in a data structure with the from and to positions, you define a binary field with a length of 4. Failure to define parameters may not always result in hard errors, but your results will probably not be what you are looking for. Consequently, defining the correct length of parameters is very important when dealing with APIs.

To Get Good Grades, Proof Your Own Work

As shown in Figure 1, the last parameter in the Retrieve Object Description API is called ErrorDs, and it is optional. Most APIs use this parameter to handle errors. If you code this parameter and an error occurs with the API, the system will not issue the CPF message. Instead, the API will return the message ID (MSGID) and error description within the subfields of the ErrorDs data structure.

The error-handling data structure is a typical example of how APIs use data structures as parameters. The first field in the data structure, bytes provided (BytesPrv), is a 4-byte binary field that you use to tell the API how large the data structure is. The bytes available (BytesAvl) field is a 4-byte binary field the API uses to tell you how much data it returned. This technique of using a fixed-length field (usually binary) to tell the length of the following data is used in a lot of API parameters.

Undergraduate Work

The second example of how APIs return data is by using an object called a user space. A user space is a chunk of AS/400 “real estate” that you can use any way you please. APIs can manage the creation, deletion, and retrieval of data from these user spaces.

APIs that create lists of information usually employ a user space to hold the information. Because the length of the list may be larger than the maximum size of a single field, a return field parameter cannot be used. User spaces do not have the same field size restrictions and are, therefore, a more logical choice.

Pay Attention to the Examples Given in Class!

Figure 2 shows examples of how to use three different APIs. In our sample code, we create a user space, retrieve a list of data from the system and store it in a user space, and retrieve the list.

We’ve chosen to show this example in RPG III rather than RPG IV because we realize that not all of you have moved to RPG IV. You can still use most of the APIs in RPG III, as this example shows; however, we strongly advocate that you move to RPG IV as soon as your business concerns will allow. We should mention here that it is far easier to retrieve data from a user space with RPG IV because it supports pointers.

In Figure 2, we use the Create User Space (QUSCRTUS) API to create a user space. This API is easy to use. The parameters are self-explanatory, so you shouldn’t have much trouble using this one.

After we have created the user space, we use the List Database Relations (QDBLDBR) API to get a list of all logical files over a physical file. The first parameter that this API wants is the name of the user space. The second parameter, OUTFMT, tells the API what kind of information you want to list by designating which file format to use to describe the returned data. We used DBLR0100 in our example, which tells the system to retrieve file information. The third parameter tells the API which file and library to use. The fourth parameter contains the member name of the physical file. And the last parameter, which we’ve aptly named IGNORE, is used only for OUTFMT DBLR0300, so we can ignore it.

After the QDBLDBR API executes, the user space contains a list of all logical files that exist over the specified physical file. Now that we have gathered our information in the

form of a list, we have to retrieve that list from our user space. To accomplish this, we use the Retrieve User Space (QUSRTVUS) API. In general, you give this API just the name of the user space, a starting position (relative to the beginning of the space), the length of data to retrieve, and a data structure variable into which the information will be returned.

It’s generally a little more complicated when dealing with the list APIs because we don’t know the position of the data in the user space. The list API deposited variable data into the user space, and we don’t know its exact position. But all list APIs follow the same convention for storing data. The beginning of the user space contains a “general” data structure that contains the offset position of the data. If you look at the GENDS data structure in Figure 2, you’ll see the OFFLST, NUMLST, and SIZENT fields, which define the number of bytes from the beginning of the user space to the beginning of the list, the number of entries in the list, and the size of each entry in the list, respectively. We can use these fields to set up a loop to retrieve each list entry using the QUSRTVUS API.

Cliff Notes

Many of the formats used as parameters for APIs are already coded on your machine. Library QSYSINC stores various source files that contain the members that correspond to various APIs. Source file QRPGLESRC in library QSYSINC contains members coded in RPG IV, and source file QRPGSRC contains members coded in RPG III. If you scan through QRPGLESRC, you will find a member named QUSROBJD, which contains the definitions for the return data for this API. Figure 3 shows the layout for the OBJD0100 file format.

Although QSYSINC relieves you from the task of coding the parameters and ensures that the parameters are defined correctly, it does not do it all for you. You still have to know what values are placed in those parameters. For that, you need to refer to the manuals on the IBM AS/400 Online Library (publib.boulder.ibm.com/pubs/ html/as400/online/homeeng1.htm). Get familiar with them, but get used to change. IBM seems bent on making you jump through hoops to retrieve information. Every release seems to change the Web site where information is stored.

Homework can be easy if you have the right reference materials. Many times, the most difficult part of working with APIs is knowing where to look for the information. See the references at the end of this article for some Internet sites that can help you in your quest for more information about using APIs.

REFERENCES AND RELATED MATERIALS

• IBM AS/400 Online Library: publib.boulder.ibm.com/pubs/html/as400/online/homeeng1.htm. (From this Web page, select your version and then enter APIs in the find field.)

• IBM Information Center: publib.boulder.ibm.com/pubs/html/as400/infocenter.html. (IBM is in the process of moving all APIs from the manuals to this Web site. Select programming and then select OS/400 APIs.)

• IBM Redbooks: www.redbooks.ibm.com. (Enter APIs in the search field. Of particular interest is “A Modern Approach to Programming RPG IV with Style.”)

D FileFmt S 8 inz(‘OBJD0100’)

D ReceiveVar S 100

D ObjectLib S 20 inz(‘MYOBJECT MYLIB ‘)

D ObjectType S 10

D Binaries DS

D ReceiveLen 1 4B 0 inz(100)

D ErrorDs DS 116 inz

D BytesPrv 1 4B 0 inz(116)

D BytesAvl 5 8B 0

D MessageId 9 15

D ERR### 16 16

D MessageDta 17 116

* Attempt to retrieve object description

C call ‘QUSROBJD’

C parm ReceiveVar

C parm ReceiveLen

C parm FileFormat

C parm ObjectLib

C parm ‘*FILE’ ObjectType

C parm ErrorDs

* If file doesn’t exist, send message and get out

C if MessageId *BLANKS

C exsr SNDMSG

C endif

*

Figure 1: This sample code calls the QUSROBJD API.

* BINARY VARIABLES

I DS

I B 1 40 STRPOS

I B 5 80 STRLEN

I B 9 120 LENSPC

* GENERAL DESCRIPTION

IGENDS DS 140

I B 113 1160 SIZINP

I B 125 1280 OFFLST

I B 133 1360 NUMLST

I B 137 1400 SIZENT

* GENERAL FEEDBACK

IINPUT DS 58

I 1 20 USRSPC

I 1 10 SPCNAM

I 11 20 SPCLIB

I 21 28 OUTFMT

I 29 48 FILLII

I 29 38 FILNAI

I 39 48 FILLBI

I 49 58 RCDFMI

* GENERIC ERROR DATA STRUCTURE

IERROR IDS

I B 1 40 BYTPRV

I B 5 80 BYTAVA

I 9 15 MSGID

I 16 16 ERR###

I 17 116 MSGDTA

* DETAIL LIST FROM QDBLDBR--LIST DATABASE RELATIONS API

ILIST DS 48

I 1 20 MAINFL

I 1 10 MNFILE

I 11 20 MNLIB

I 21 30 DEPFIL

I 31 40 DEPLIB

I 41 41 DEPTYP

I 42 44 DEPRSR

I B 45 480 BINREF

*

C *ENTRY PLIST

C PARM FILLIB 20

C MOVELFILLIB OUTFIL 10

C MOVE FILLIB OUTLIB 10

C MOVEL'USRSPC' SPCNAM

C MOVEL'QTEMP' SPCLIB

C Z-ADD116 BYTPRV

* CREATE USER SPACE

C CALL 'QUSCRTUS'

C PARM USRSPC

C PARM *BLANKS ATRSPC 10

C PARM 1024 LENSPC

C PARM *BLANKS VALSPC 1

C PARM '*CHANGE' AUTSPC 10

C PARM *BLANKS TXTSPC 50

C PARM '*YES' RPLSPC 10

C PARM ERROR

* LIST DATABASE RELATIONS TO USER SPACE

C CALL 'QDBLDBR'

C PARM USRSPC

C PARM 'DBRL0100'OUTFMT 8

C PARM FILLIB

C PARM '*FIRST' RCDFMI

C PARM *BLANKS IGNORE 10

C PARM ERROR

* IF FILE FOUND

C MSGID IFNE 'CPF5715'

C Z-ADD1 STRPOS

C Z-ADD140 STRLEN

* RETRIEVE USER SPACE GENERAL INFORMATION DATA STRUCTURE

C CALL 'QUSRTVUS'

C PARM USRSPC

C PARM STRPOS

C PARM STRLEN

C PARM GENDS

C Z-ADD1 STRPOS

C Z-ADDSIZINP STRLEN

* RETRIEVE DETAIL LIST INFORMATION

C CALL 'QUSRTVUS'

C PARM USRSPC

C PARM STRPOS

C PARM STRLEN

C PARM INPUT

C MOVEL'USRSPC' SPCNAM

C MOVEL'QTEMP' SPCLIB

C OFFLST ADD 1 STRPOS

C Z-ADDSIZENT STRLEN

* WALK THROUGH USER SPACE FOR NUMBER OF LOGICAL FILES

C DO NUMLST

C CALL 'QUSRTVUS'

C PARM USRSPC

C PARM STRPOS

C PARM STRLEN

C PARM LIST

C DEPFIL IFEQ '*NONE'

C LEAVE

C ENDIF

* DO SOMETHING WITH NAMES OF DEPENDENT FILES (DEPLIB/DEPFIL)

C* MOVELDEPFIL DSPFIL

C* MOVE DEPLIB DSPLIB

* ADD SIZE OF ENTRY TO START POSITION FOR NEXT LIST ENTRY

C ADD SIZENT STRPOS

C ENDDO

C ENDIF

C SETON LR

D*****************************************************************

D*Type Definition for the OBJD0100 format

D*****************************************************************

D QUSD0100 DS

D* Qus OBJD0100

D QUSBRTN06 1 4B 0

D* Bytes Returned

D QUSBAVL07 5 8B 0

D* Bytes Available

D QUSOBJN00 9 18

D* Object Name

D QUSOBJLN 19 28

D* Object Lib Name

D QUSOBJT00 29 38

D* Object Type

D QUSRL01 39 48

D* Return Lib

D QUSASP04 49 52B 0

D* Aux Storage Pool

D QUSOBJO04 53 62

D* Object Owner

D QUSOBJD04 63 64

D* Object Domain

D QUSCDT10 65 77

D* Create Date Time

D QUSCDT11 78 90

D* Change Date Time

Figure 2: This sample program shows how to use list APIs with a user space.

Figure 3: This sample code is from QSYSINC/QRPGLESRC, member QUSROBJD.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: