OpenAPI import tool

Official Content
This documentation is valid for:

The main purpose of the OpenAPI import tool is to inspect the Open Api RESTful API Documentation (fka Swagger RESTful API Documentation Specification) of any RESTful web service (it can be generated by GeneXus or not).

With the information obtained using the OpenAPI Import tool, implementing a consumer is much plain sailing because it consolidates in the KB some objects (procedures, SDTs, domains) which are necessary to consume the RESTful service. After that, it's easy to follow the tutorial HowTo: Inserting data using a BC exposed as Rest service.

The advantage of using the OpenAPI import tool is to avoid the effort of Getting information from the Swagger UI docs, such as the operations, the HTTP verb needed to consume the web service, the input and output parameters (the HTTP body structure for invoking the service and the responses).

Note that the OpenAPI documentation of  Rest web services in GeneXus is obtained through the Generate OpenAPI interface property.

The tool is accessible through the Tools > Application Integration > OpenAPI import menu option.

image_20171230143555_1_png

OpenAPI import tool dialog

File Path /URL:  Enter the URL or file path of the RESTful service documentation in Swagger format.

Swagger Format:

The files describing the RESTful API in accordance with the Swagger specification are represented as JSON objects and conform to the JSON standards. YAML, being a superset of JSON, can be used as well to represent a Swagger specification file.

For Rest web services in GeneXus you can obtain the Swagger file if you activate the Generate OpenAPI interface property.

Module / Folder: Where the automatically generated objects will be located.

image_20171230143319_1_png

Results of using the OpenAPI import tool

The tool consolidates in the KB some objects (procedures, SDTs, domains) which are necessary to consume the RESTful service.

The following is a summary of the objects which are consolidated in the KB:

Object Folder / Module Purpose
ApiResponse SDT OpenAPICommon Defines the HTTP response structure
CallApi procedure OpenAPICommon It's a parameterized procedure that given the service URL and the call parameters, executes the HTTP verb and returns the HTTP result.
VarCharToJsonFormat, 
DateTimeToJsonFormat, 
DateToJsonFormat, 
NumericToJsonFormat
OpenAPICommon Auxiliary procedures
ApiBaseUrl <MyModule>\Client Returns the BaseURL which will be used for calling the RESTful services. It should be changed in most cases.
client procedure of the RESTful service <MyModule>\Api

A procedure to invoke the service. The name of this procedure is selected upon the operationId* in the documentation file (Swagger) of the service.

*operationId: Unique string used to identify the operation. The id is unique among all operations described in the API. 

SDTs needed for defining the input and output parameters of the service. <MyModule>\Model Input and output parameters of the service.

 

Example

In this example, in a KB we have a Rest procedure called "UpdateDocservice".

Folder view structure example

The following is the folder view structure after importing the YAML file associated with the procedure of the example.

image_20171230142911_1_png

After inspecting the Swagger document of UpdateDocservice using the OpenAPI Import tool in the consumer KB, it automatically generates a procedure called UpdateDocservice under the RestAPI\API path. "RestAPI" is a module in this example, where we have chosen to import the objects generated by the tool.

Below is the part of the Swagger document which corresponds to the service we are inspecting. Note the operationId value that determines the name of the procedure to consume the RESTful service.

 /UpdateDocService:
    post:
      tags:
        - UpdateDocService
      operationId: "UpdateDocService"
      summary: "Update Doc Service"
      parameters:
        - name: "UpdateDocServiceInput"
          in: "body"
          required: false
          schema:
            $ref: "#/definitions/UpdateDocServiceInput"


      responses:
        200:
          description: "Successful operation"
          schema:
            $ref: "#/definitions/UpdateDocServiceOutput"
        404:
          description: "Not found"

The UpdateDocservice_Input and the UpdateDocservice_Output are SDTs automatically generated by the tool, to be able to consume the service.

The parameter information is also obtained from the Swagger file.

 definitions:
  UpdateDocServiceOutput:
    properties:
      Status:
        type: "string"


  UpdateDocServiceInput:
    properties:
      PropertyId:
        type: "integer"
        format: "int32"
      LanguageCode:
        type: "string"

The following parm rule is generated in the UpdateDocservice consumer object which is a stub for calling the service.

parm(in:&UpdateDocServiceInput, out:&UpdateDocServiceOutput, out:&message, out:&success);

So, in order to call the service, you just need to call the UpdateDocservice consumer object by passing it the corresponding parameters and then processing the results.

Sample consumer code

RestAPI.UpdateDocservice(&UpdateDocServiceInput,&UpdateDocServiceOutput,&Message,&success)
    if &success
        msg("Success!!")
        msg("API return " + &UpdateDocServiceOutput.ErrorMessage.Id + " " + &UpdateDocServiceOutput.ErrorMessage.Description)
        msg("HTTP Message " + &Message.Id + " " + &Message.Description)
    else
        msg("There was an error! ")
        msg("API return " + &UpdateDocServiceOutput.ErrorMessage.Id + " " + &UpdateDocServiceOutput.ErrorMessage.Description)
        msg("HTTP Message " +  &Message.Id + " " + &Message.Description)
    endif

This is rather easier than defining the input and output parameters manually and executing the HTTP verbs using HTTP client data type. This task is done by the OpenAPI Import tool, which generates the code for you.



Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.