IRIS API Explorer Application
Tags: angular, api, practice, programming
Published: 2021-11-19 13:36:22

<h3 style="color:#aaaaaa;font-style:italic;">
  Background
</h3>

InterSystems IRIS 2019 is going to introduce new and exciting features. One of the areas with new interesting must-to-know things is the API Management. 

OpenAPI initiative (<https://www.openapis.org/>) is the organization supporting a standard specification to define APIs (<https://github.com/OAI/OpenAPI-Specification>). The OpenAPI Specification (OAS) defines a standard, programming language-agnostic interface description for REST APIs, which allows both humans and computers to discover and understand the capabilities of a service without requiring access to source code, additional documentation, or inspection of network traffic. When properly defined via OpenAPI, a consumer can understand and interact with the remote service with a minimal amount of implementation logic. Similar to what interface descriptions have done for lower-level programming, the OpenAPI Specification removes guesswork in calling a service.

InterSystems introduces in InterSystems IRIS **support for an API-design first approach**, this approach allows to design your specification first and then generate server-side from it. If we design the API first, normally we use Swagger Editor or another similar tool to create the specification and get the OAS specification in JSON format whenever we want.

Once we have the API designed and ready to implement then we can create the server side API logic using the OAS specification. In InterSystems IRIS 2019.1 we can use the new routine **<samp>^%REST</samp>** to scaffold the API and automatically generate the classes where put the code that will call the business logic. The methods of this classes will be based on naming conventions although you can define the method and class in your specification (operationId).

Example of use of the InterSystems IRIS REST Command Line Interface:

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  USER>do ^%REST<br /> <br />REST Command Line Interface (CLI) helps you CREATE or DELETE a REST application 
</div>

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  Enter an application name or (L)ist all REST applications (L): acmeapi<br />REST application not found: acmeapi<br />Do you want to create a new REST application? Y or N (Y):<br /> <br />File path or absolute URL of a swagger document.<br />If no document specified, then create an empty application.<br />OpenAPI 2.0 swagger: C:\myspec\acme.swagger.json<br /> <br />OpenAPI 2.0 swagger document: C:\myspec\notification.swagger.json<br />Confirm operation, Y or N (Y):<br />-----Creating REST application: acmeapi-----<br />CREATE acmeapi.spec<br />GENERATE acmeapi.disp<br />CREATE acmenapi.impl<br />REST application successfully created.<br /> <br />Create a web application for the REST application? Y or N (Y):<br />Specify web application name. Default is /csp/api/acme<br />Web application name: /csp/api/acme/v1<br /> <br />-----Deploying REST application: acmeapi-----<br />Application acmeapi deployed to /csp/api/acme/v1<br /> 
</div>

At this moment the creation of a REST API only can use the **OpenAPI 2.0** Swagger specification in order to build the scaffold of the API.

As you see this routine create three classes: 

  * &lt;application>**.spec**: this class is the container for the swagger spec (XData OpenAPI block). This class is read-only.
  * &lt;application>**.disp**: dispatch class ready to use in the CSP application. It extends %CSP.REST and define the XData UrlMap. This class is read-only and marked as system class (by default is hidden in Atelier).
  * &lt;application>**.impl**: class defining all the necessary signature methods. This class should be complete in order to make the API works.

<h3 style="color:#aaaaaa;font-style:italic;">
  What if I have my API developed already?
</h3>

In InterSystems IRIS 2018.1 InterSystems introduced the service discovery that enables developers to explore the API capabilities remotely. Also the Swagger integration allowing you to generate an Open API Specification (OAS) from your existing REST application. So, any API that we modify in InterSystems IRIS it can auto-generate the swagger specification.

It is possible to query all the available APIs in the system through a management API:

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  HTTP GET http://&lt;host>:&lt;port>/api/mgmnt/ 
</div>

Returns:

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  <br />[<br />...,<br />    {<br />        "name": "/csp/petstore/v2",<br />        "dispatchClass": "petstore.disp",<br />        "namespace": "USER",<br />        "resource": "",<br />        "swaggerSpec": "/api/mgmnt/v1/USER/spec/csp/petstore/v2",<br />        "enabled": true<br />    }<br />]
</div>

Moreover the Swagger specification of the API can be retrieve doing a HTTP GET to the URL show by the property swaggerSpec. Any API operation defined by the original swagger specification has a new property defining the name of the method that should implement the action:

Example:

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  "x-ISC_ServiceMethod": "getPetById",
</div>

A really funny stuff is that we can make use of this api/mgmnt not only for discovery, also for API creation/query/delete using 

<div style="background:#eeeeee;border:1px solid #cccccc;padding:5px 10px;">
  HTTP POST to /api/mgmnt/v2/&lt;namespace>/&lt;applicationName><br />HTTP GET to /api/mgmnt/v2/&lt;namespace>/&lt;applicationName><br />HTTP DELETE to /api/mgmnt/v2/&lt;namespace>/&lt;applicationName>
</div>

<h3 style="color:#aaaaaa;font-style:italic;">
  IRIS API Explorer
</h3>

IRIS Explorer is an Angular 5 application that take advantage of this API in order to provide a nice visual tool to manage IRIS APIs. Let's do a quick tour:

First we need to do a login into an InterSystems IRIS instance (by default look for a local instance in port 52773):

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wvm0jvtj2h300xwu6brs.png)
After login the app make a query to retrieve all the existing APIS:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yrulrvtwpzkc54773lo4.png)
We can delete an existing APIs or we can create a new one. To create a New Application we need to provide the Namespace, the Application Name and the Swagger specification from a .json file:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7p021fuzllbnhkap43i.png)
Once we have the API created we can view the specification. In order to make this more funny I embedded a Swager-UI (<https://github.com/swagger-api/swagger-ui>).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/df2w3ihbidqr4539zggr.png)
And of course we can retrieve the JSON OAS Spec:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bxq3whwj3x4xnvtkvrun.png)
All the code is open and is up to you to use or modify as your convenience. The App is available in Open Exchange:

<https://openexchange.intersystems.com/index.html#!/package/IRIS%20API%20EXPLORER>

And also in GitHub:

<https://github.com/drechema/iris-explorer>

I hope it will be useful