use of com.google.api.server.spi.config.ApiConfigLoader in project endpoints-java by cloudendpoints.
the class GetOpenApiDocAction method genOpenApiDoc.
/**
* Generates an OpenAPI document for an array of service classes.
*
* @param classPath Class path to load service classes and their dependencies
* @param outputFilePath File to store the OpenAPI document in
* @param hostname The hostname to use for the OpenAPI document
* @param basePath The base path to use for the OpenAPI document, e.g. /_ah/api
* @param serviceClassNames Array of service class names of the API
* @param outputToDisk Iff {@code true}, outputs a openapi.json to disk.
* @return a single OpenAPI document representing all service classes.
*/
public String genOpenApiDoc(URL[] classPath, String outputFilePath, String hostname, String basePath, List<String> serviceClassNames, boolean outputToDisk) throws ClassNotFoundException, IOException, ApiConfigException {
File outputFile = new File(outputFilePath);
File outputDir = outputFile.getParentFile();
if (!outputDir.isDirectory() || outputFile.isDirectory()) {
throw new IllegalArgumentException(outputFilePath + " is not a file");
}
ClassLoader classLoader = new URLClassLoader(classPath, getClass().getClassLoader());
ApiConfig.Factory configFactory = new ApiConfig.Factory();
Class<?>[] serviceClasses = loadClasses(classLoader, serviceClassNames);
List<ApiConfig> apiConfigs = Lists.newArrayListWithCapacity(serviceClasses.length);
TypeLoader typeLoader = new TypeLoader(classLoader);
ApiConfigLoader configLoader = new ApiConfigLoader(configFactory, typeLoader, new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes()));
ServiceContext serviceContext = ServiceContext.create();
for (Class<?> serviceClass : serviceClasses) {
apiConfigs.add(configLoader.loadConfiguration(serviceContext, serviceClass));
}
SwaggerGenerator generator = new SwaggerGenerator();
SwaggerContext swaggerContext = new SwaggerContext().setHostname(hostname).setBasePath(basePath);
Swagger swagger = generator.writeSwagger(apiConfigs, true, swaggerContext);
String swaggerStr = Json.mapper().writer(new EndpointsPrettyPrinter()).writeValueAsString(swagger);
if (outputToDisk) {
Files.write(swaggerStr, outputFile, UTF_8);
System.out.println("OpenAPI document written to " + outputFilePath);
}
return swaggerStr;
}
use of com.google.api.server.spi.config.ApiConfigLoader in project endpoints-java by cloudendpoints.
the class BaseSystemServiceTest method testGetApiConfigs.
@Test
public void testGetApiConfigs() throws Exception {
final String apiConfig1 = "api1 configuration";
final String apiConfig2 = "api2 configuration";
ApiConfigLoader configLoader = Mockito.spy(new ApiConfigLoader());
ApiConfigValidator configValidator = Mockito.mock(ApiConfigValidator.class);
ApiConfigWriter configWriter = Mockito.mock(ApiConfigWriter.class);
ApiConfig config1 = createGenericApiConfig();
config1.setName("api1");
config1.setVersion("v1");
ApiConfig config2 = createGenericApiConfig();
config2.setName("api2");
config2.setVersion("v10");
Mockito.doReturn(config1).doReturn(config2).when(configLoader).loadConfiguration(Mockito.<ServiceContext>any(), Mockito.<Class<?>>any());
ImmutableMap<ApiKey, String> expected = ImmutableMap.of(new ApiKey("api1", "v1"), apiConfig1, new ApiKey("api2", "v2"), apiConfig2);
Mockito.doReturn(expected).when(configWriter).writeConfig(setOf(config1, config2));
SystemService systemService = new SystemService(configLoader, "app", configWriter, new Object[] { service, getTestService2() }, false);
Map<ApiKey, String> configs = systemService.getApiConfigs();
Mockito.verify(configWriter, times(1)).writeConfig(setOf(config1, config2));
assertEquals(expected, configs);
}
use of com.google.api.server.spi.config.ApiConfigLoader in project endpoints-java by cloudendpoints.
the class SchemaRepositoryTest method setUp.
@Before
public void setUp() throws Exception {
TypeLoader typeLoader = new TypeLoader(getClass().getClassLoader());
ApiConfigAnnotationReader annotationReader = new ApiConfigAnnotationReader(typeLoader.getAnnotationTypes());
this.repo = new SchemaRepository(typeLoader);
this.configLoader = new ApiConfigLoader(new ApiConfig.Factory(), typeLoader, annotationReader);
this.config = configLoader.loadConfiguration(ServiceContext.create(), FooEndpoint.class);
}
Aggregations