Accessing the Swagger Endpoint

◷ Reading Time: 5 minutes

This process will show you how to use the Swagger endpoint as part of accessing services deployed as Azure, AWS, Google Cloud, or in FlexRule Server.

Deployed as Azure, AWS, or Google Cloud

In the previous article, we have learned that we can retrieve all the available services by using the Services URL: /Services/?code=xxxx.

Result from using the Services URL: /Services/?code=randomcode. The returned URL shows  /Services/age-service/1/person_growing_state/1

And from the result, we can then get the service details and use it for executing the service.

To request for information on Input in the response body as below, follow these steps in our previous article.

Resulting JSON from using /Services/age-service/1/person_growing_state/1 to get the Service details for executing the service

To invoke the swagger endpoint, you will just simply add /swagger.json at the end of your request path while keeping the original code of the Service URL.
E.g., Services/age-service/1/person_growing_state/1/swagger.json?code=xxxx.

The Swagger specification result will include important details such as:

  • Execution URL
  • Request and Response example formats
  • Schemas
Resulting JSON from using swagger request to service URL

If you are not familiar with swagger, you may copy-paste the result to https://editor.swagger.io/ to see it on Swagger UI.

View of swagger editor

The request body contains the example input format for the specified service. As explained in Retrieving Information on Input, the original input should be re-formatted which is already done in the swagger specification.

Deployed in FlexRule Server

Once you have successfully deployed a service on FlexRule Server, you can access the Swagger endpoint.

Navigate to Services in FlexRule Workbench and select Test Run for the service you want to access the Swagger endpoint

Workbench Services view showing Test Run selected from the Actions menu of a Service

You will see the service URL at the top of the page.

NOTE: The URL displayed here will be the URL used for executing the service. Therefore will display the location of the Master Execution server, in this case localhost:9001.

To use Swagger the URL will need to be edited to point to the location of the Master Management machine as the Management node is where Swagger will gather the details from. In this example localhost:9000.

Test Run view in Workbench showing the service execution URL highlighted

You can use Postman or a similar tool to invoke the swagger endpoint using the following request.

  • Method: GET
  • Service URL: Service Url/swagger.json

Note: Add /swagger.json at the end of your service URL
e.g. http://localhost:9000/api/services/execute/Delivery Calculator/1/Calculator/swagger.json

Also note the URL begins with the Master Management node location as mentioned above, in this case localhost:9000.

Postman view showing the Authorization token for the Swagger request on the Service
Authorization: Bearer Token
Postman view showing the response to the Swagger request on the Service
Response from Swagger endpoint

You will get a similar response with the details of your service.

{
    "openapi": "3.0.1",
    "info": {
        "title": "DC1 API",
        "description": "An API to execute FlexRule package",
        "version": "1"
    },
    "servers": [
        {
            "url": "http://localhost:9000/api/services/execute/DeliveryCalculator/1/Calculator"
        }
    ],
    "paths": {
        "http://localhost:9000/api/services/execute/DeliveryCalculator/1/Calculator": {
            "post": {
                "Tag": [
                    "Delivery Calculator"
                ],
                "parameters": [
                    {
                        "Name": "parcel weight",
                        "Type": "decimal",
                        "Assembly": null,
                        "Direction": "In"
                    },
                    {
                        "Name": "delivery cost",
                        "Type": "decimal",
                        "Assembly": null,
                        "Direction": "Out"
                    }
                ],
                "RequestBody": [
                    {
                        "application/json": {
                            "schema": {
                                "ref": "#/components/schemas/Inputs"
                            },
                            "example": {
                                "input": [
                                    [
                                        {
                                            "name": "parcel weight",
                                            "direction": 0,
                                            "type": "decimal",
                                            "assembly": null
                                        }
                                    ]
                                ],
                                "output": null
                            }
                        }
                    }
                ],
                "Response": [
                    {
                        "200": {
                            "description": "Success",
                            "content": {
                                "application/json": [
                                    {
                                        "ref": "#/components/schemas/Outputs"
                                    },
                                    {
                                        "input": null,
                                        "output": [
                                            [
                                                {
                                                    "name": "delivery cost",
                                                    "direction": 1,
                                                    "type": "decimal",
                                                    "assembly": null
                                                }
                                            ]
                                        ]
                                    }
                                ]
                            }
                        }
                    }
                ]
            }
        }
    }
}
Updated on May 7, 2024

Was this article helpful?

Related Articles