Java tutorial
/* * Copyright 2014 Oliver Kotte <kotte@atb-bremen.de>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.koddistortion.swagger; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectWriter; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; import net.koddistortion.swagger.model.Resource; import net.koddistortion.swagger.model.ResourceListing; import net.koddistortion.swagger.model.api.ApiDeclaration; /** * @author Oliver Kotte (dev@koddistortion.net) */ public class SwaggerApiClient { private String baseUrl; private boolean autoRetrieveApis = false; private ObjectMapper mapper; private ResourceListing resourceListing; private Map<String, ApiDeclaration> apiDeclarations = new HashMap<>(); public SwaggerApiClient() { mapper = new ObjectMapper(); } public String getBaseUrl() { return baseUrl; } public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; } public boolean getAutoRetrieveApis() { return autoRetrieveApis; } public void setAutoRetrieveApis(boolean autoRetrieveApis) { this.autoRetrieveApis = autoRetrieveApis; } public ApiDeclaration getApiDeclaration(Resource resource) throws MalformedURLException, IOException { ApiDeclaration declaration = this.apiDeclarations.get(resource.getPath()); if (declaration == null) { declaration = retrieveApiDeclaration(resource); this.apiDeclarations.put(resource.getPath(), declaration); } return declaration; } protected ApiDeclaration retrieveApiDeclaration(Resource resource) throws MalformedURLException, IOException { final URL url = new URL(this.getBaseUrl() + resource.getPath()); return mapper.readValue(url.openStream(), ApiDeclaration.class); } public ResourceListing getResourceListing() throws IOException { URL url = new URL(this.baseUrl); this.resourceListing = mapper.readValue(url.openStream(), ResourceListing.class); if (autoRetrieveApis) { // TODO fill apis! } return this.resourceListing; } public static void main(String[] args) throws IOException { SwaggerApiClient c = new SwaggerApiClient(); c.setBaseUrl("http://petstore.swagger.wordnik.com/api/api-docs"); ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); ResourceListing listing = c.getResourceListing(); ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter(); for (Resource resource : listing.getApis()) { ApiDeclaration declaration = c.getApiDeclaration(resource); System.out.println(writer.writeValueAsString(declaration)); } } }