Search in sources :

Example 1 with AaiParameter

use of io.apicurio.datamodels.asyncapi.models.AaiParameter in project carbon-apimgt by wso2.

the class SolaceAdminApis method buildAPIProductRequestBody.

/**
 * Build the request body for API product creation request
 *
 * @param aai20Document      Async definition of the Solace API
 * @param environment        name of the Environment in Solace
 * @param apiNameWithContext name of the API with context
 * @param registeredApiName  name of the API in Solace
 * @return org.json.JSON Object of request body
 */
private org.json.JSONObject buildAPIProductRequestBody(Aai20Document aai20Document, String environment, String apiNameWithContext, String registeredApiName) {
    org.json.JSONObject requestBody = new org.json.JSONObject();
    org.json.JSONArray apiName = new org.json.JSONArray();
    apiName.put(registeredApiName);
    requestBody.put("apis", apiName);
    requestBody.put("approvalType", "auto");
    if (aai20Document.info.description != null) {
        requestBody.put("description", aai20Document.info.description);
    } else {
        requestBody.put("description", apiNameWithContext);
    }
    requestBody.put("displayName", apiNameWithContext);
    requestBody.put("pubResources", new org.json.JSONArray());
    requestBody.put("subResources", new org.json.JSONArray());
    requestBody.put("name", apiNameWithContext);
    org.json.JSONArray environments = new org.json.JSONArray();
    environments.put(environment);
    requestBody.put("environments", environments);
    HashSet<String> parameters1 = new HashSet<>();
    org.json.JSONArray attributes1 = new org.json.JSONArray();
    for (AaiChannelItem channel : aai20Document.getChannels()) {
        if (channel.parameters != null) {
            Set<String> parameterKeySet = channel.parameters.keySet();
            for (String parameterName : parameterKeySet) {
                if (!parameters1.contains(parameterName)) {
                    AaiParameter parameterObject = channel.parameters.get(parameterName);
                    if (parameterObject.schema != null) {
                        org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject);
                        if (attributeObj != null) {
                            attributes1.put(attributeObj);
                            parameters1.add(parameterName);
                        }
                    } else if (parameterObject.$ref != null) {
                        if (aai20Document.components.parameters != null) {
                            AaiParameter parameterObject1 = aai20Document.components.parameters.get(parameterName);
                            if (parameterObject1.schema != null) {
                                org.json.JSONObject attributeObj = getAttributesFromParameterSchema(parameterName, parameterObject1);
                                if (attributeObj != null) {
                                    attributes1.put(attributeObj);
                                    parameters1.add(parameterName);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    requestBody.put("attributes", attributes1);
    HashSet<String> protocolsHashSet = new HashSet<>();
    for (AaiChannelItem channel : aai20Document.getChannels()) {
        protocolsHashSet.addAll(AsyncApiParser.getProtocols(channel));
    }
    org.json.JSONArray protocols = new org.json.JSONArray();
    for (String protocol : protocolsHashSet) {
        org.json.JSONObject protocolObject = new org.json.JSONObject();
        protocolObject.put("name", protocol);
        protocolObject.put("version", getProtocolVersion(protocol));
        protocols.put(protocolObject);
    }
    requestBody.put("protocols", protocols);
    return requestBody;
}
Also used : AaiParameter(io.apicurio.datamodels.asyncapi.models.AaiParameter) JSONObject(org.json.JSONObject) AaiChannelItem(io.apicurio.datamodels.asyncapi.models.AaiChannelItem) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONArray(org.json.JSONArray) HashSet(java.util.HashSet)

Aggregations

AaiChannelItem (io.apicurio.datamodels.asyncapi.models.AaiChannelItem)1 AaiParameter (io.apicurio.datamodels.asyncapi.models.AaiParameter)1 HashSet (java.util.HashSet)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1