Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagegroovy
titleSample script listing available APIs
linenumberstrue
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));
}

...

Code Block
[
  {
    "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.

...

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

to this:

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