Ceptor Docs

Page tree
Skip to end of metadata
Go to start of metadata

If you wish to provide a list of published APIs available in a given environment, an easy method of doing this is available using "Location Scripts".

More information about scripts here: Scripts and Macros 

You have complete control over what information you want to provide, and how to present it - this example shows how to provide the information as a JSON response:

First, in the Ceptor Console find Gateway Configuration - here, go to the location that serves your API.

Here, Edit the script, select "Groovy" as the script type and paste in this example script:

Sample script listing available APIs
import io.ceptor.config.gateway.Config.APIManagement.APIVersion;
import io.ceptor.apimanager.Config.APIType;
import portalprotect.org.json.*;

if (context.httpExchange.getRequestPath().equals("/availableapis")) {
    List<APIVersion> deployedAPIs = context.getDeployedAPIsForLocation(context.currentLocation);
    JSONArray response = new JSONArray();
    
    for(APIVersion version : deployedAPIs) {
        JSONObject jo = new JSONObject();
        
        jo.put("api_name", version.apiName);
        jo.put("version_name", version.name);
        jo.put("version_description", version.description);
        jo.put("type", version.type.toString());
        switch(version.type) {
            case APIType.openapi:
                jo.put("openapi.spec.url", version.basePath+"?openapi.json");
                break;
            case APIType.soap:
                jo.put("wsdl.spec.url", version.basePath+"?wsdl");
                break;
            case APIType.plainhttp:
                break;
        }
        response.put(jo);
    }
    context.respond(200, 'OK', 'application/json; charset=UTF-8', response.toString(2));
}


The script should look like this:

It starts by checking if the request path equals /availableapis - if not, it does nothing - but if it does, it obtains a list of all currently published/deployed APIs from the gateway for the current Location and then it creates a response containing the information about all the published APIs.

It does this by creating a new JSONArray, and then a JSONObject for each, with the essential information.

If you navigate your browser to e.g. https://localhost:8443/availableapis you should then see a response similar to this:

[
  {
    "api_name": "Plain",
    "version_name": "1",
    "version_description": "Plain API Example",
    "type": "plainhttp"
  },
  {
    "api_name": "World Bank",
    "version_name": "1",
    "version_description": "",
    "type": "openapi",
    "openapi.spec.url": "/worldbank?openapi.json"
  },
  {
    "api_name": "Petstore Remote",
    "version_name": "Remote Petstore 1",
    "version_description": "Remote petstore",
    "type": "openapi",
    "openapi.spec.url": "/petstore/remote/v1?openapi.json"
  },
  {
    "api_name": "Hello World",
    "version_name": "1",
    "version_description": "The first version",
    "type": "openapi",
    "openapi.spec.url": "/hello/v1?openapi.json"
  },
  {
    "api_name": "Petstore Local",
    "version_name": "Petstore local v1",
    "version_description": "Local petstore",
    "type": "openapi",
    "openapi.spec.url": "/petstore/local/v1?openapi.json"
  }
]


That's all there is to it - you can customized the script to return more (or less) information for each avalable API - it could return e.g. the openapi spec or wsdl itself embedded in the response, or it might want to filter on which APIs to return depending on input criteria.

If you e.g. want to provide the full URL including scheme and hostname instead of just the path, you could change the line:

jo.put("openapi.spec.url", version.basePath+"?openapi.json");

to this:

jo.put("openapi.spec.url", context.macro("%{HTTP_HOST}")+version.basePath+"?openapi.json");
  • No labels