5
votes

My goal is to automatically create APIs to new web services in the WSO2 publisher, and these web services are currently documented with swagger 2. To do this as efficiently as possible, I would like to use an URL to the swagger.json, as is done in using the UI under: WSO2 Publisher, Add existing API; preferably in such a way that name and other mandatory fields are filled in using swagger (if they exist in the swagger json).


Possible path to a solution:

Under the 'What I have tried' section, we see that the response message at step 1 is 'imported', so if we can figure out how to call the imported template in step 2 that should solve the issue.


What I have tried:

All the links in the 'Found Resources' section detail how to use the explicit swagger json to help create the api - and mandatory fields like name, context etc are still required (even when found in the json). I have not been able to get a command like the ones above to work with an url, and I have tried with the field swagger_url="urlToSwaggerJson" (see end of next section).

I would guess that the answer lies in actually using the UI commands, similar to what is described in: http://yasassriratnayake.blogspot.nl/2015/06/creating-api-with-curl-commands-with.html

Under 'Creating the API with Normal Creating Flow'. When using the UI, the first step taken is to create a template description from the swagger url, so I have tried to adjust the procedure by adding step 1 below. Problem: The design step does not find the api started in step 1.

0) LOGIN:

curl -k -X POST -c cookies URLtoWSO2/publisher/site/blocks/user/login/ajax/login.jag -d 'action=login&username=admin&password=admin'

Response: {"error" : false}

1) START THE PROCESS:

curl -k -X POST -b cookies URLtoWSO2/publisher/site/blocks/item-design/ajax/add.jag -F import-definition="swagger-url" -F swagger-url="URLtoSwagger" -F swagger-file="" -F action="start"

Response: {"error" : false, "message" : "imported"}

2) DESIGN THE API:

curl -k -X POST -b cookies URLtoWSO2/publisher/site/blocks/item-design/ajax/add.jag -F action="design" -F name="NameOfAPI" -F context="/URItoAPI"

Response: {"error" : true, "message" : " Error in adding API "}

[I do not get an error if adding -F version=1.0.0, but that just creates an empty API not using swagger info. It is also information that exists in swagger, and is automatically filled in when using the UI]


Extra information:

It seems like creating the API using WSO2 api call only uses swagger for resource information, and still requires the explicit swagger json.

The command:

curl -k -X POST -b cookies URLtoWSO2/publisher/site/blocks/item-add/ajax/add.jag -d "action=addAPI&name=PhoneVerification&context=/phoneverify&version=1.0.0&tiersCollection=Gold,Bronze&resourceCount=0&resourceMethod-0=GET&resourceMethodAuthType-0=Application&resourceMethodThrottlingTier-0=Unlimited&uriTemplate-0=/*" -d'endpoint_config={"production_endpoints":{"url":"URL","config":null},"endpoint_type":"http"}';

can be changed to:

curl -k -X POST -b cookies URLtoWSO2/publisher/site/blocks/item-add/ajax/add.jag -d "action=addAPI&name=PhoneVerification&context=/phoneverify&version=1.0.0&tiersCollection=Gold,Bronze&uriTemplate-0=/*" -d'swagger=SwaggerJson' -d'endpoint_config={"production_endpoints":{"url":"URL","config":null},"endpoint_type":"http"}';

Which makes WSO2 use the swagger resource information, but fails in the sense that it does not recognize name, version, or other swagger information. Also, it fails since it requires the explicit swagger json, not an URL to it, i.e.:

curl -k -X POST -b cookies URLtoWSO2/publisher/site/blocks/item-add/ajax/add.jag -d "action=addAPI&name=PhoneVerification&context=/phoneverify&version=1.0.0&tiersCollection=Gold,Bronze&uriTemplate-0=/*" -d'swagger=URLtoSwagger' -d'endpoint_config={"production_endpoints":{"url":"URL","config":null},"endpoint_type":"http"}';

fails.


Found Resources:

1

1 Answers

0
votes

In WSO2 APIM 1.10.0 ++ there is a bunch of RESTful APIs to achieve this. I tried via the REST APIs passing in the Swagger JSON and it picks up the API name, context etc accurately: hence such a call would create your API. E.g below

curl -H "Authorization: Bearer <token>" -H "Content-Type: application/json" -X POST -d @data.json http://127.0.0.1:9763/api/am/publisher/v0.9/apis

data.json has the following among other elements:

    "provider": "admin",
    "version": "1.0.0",
    "description": "Verify a phone number",
    "name": "PhoneVerification3",
    "context": "/phoneverify3"

See here for the API:
https://docs.wso2.com/display/AM1100/apidocs/publisher/#!/operations#APIsApi#apisGet