Search in sources :

Example 6 with ApiConfigLoader

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;
}
Also used : ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) SwaggerContext(com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext) EndpointsPrettyPrinter(com.google.api.server.spi.response.EndpointsPrettyPrinter) ServiceContext(com.google.api.server.spi.ServiceContext) TypeLoader(com.google.api.server.spi.TypeLoader) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) SwaggerGenerator(com.google.api.server.spi.swagger.SwaggerGenerator) URLClassLoader(java.net.URLClassLoader) Swagger(io.swagger.models.Swagger) URLClassLoader(java.net.URLClassLoader) File(java.io.File)

Example 7 with ApiConfigLoader

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);
}
Also used : ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) ApiKey(com.google.api.server.spi.config.model.ApiKey) ApiConfigValidator(com.google.api.server.spi.config.validation.ApiConfigValidator) ApiConfig(com.google.api.server.spi.config.model.ApiConfig) ApiConfigWriter(com.google.api.server.spi.config.ApiConfigWriter) Test(org.junit.Test)

Example 8 with ApiConfigLoader

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);
}
Also used : ApiConfigAnnotationReader(com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader) ApiConfigLoader(com.google.api.server.spi.config.ApiConfigLoader) TypeLoader(com.google.api.server.spi.TypeLoader) Before(org.junit.Before)

Aggregations

ApiConfigLoader (com.google.api.server.spi.config.ApiConfigLoader)8 TypeLoader (com.google.api.server.spi.TypeLoader)6 ApiConfigAnnotationReader (com.google.api.server.spi.config.annotationreader.ApiConfigAnnotationReader)5 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)5 Before (org.junit.Before)5 ServiceContext (com.google.api.server.spi.ServiceContext)3 SchemaRepository (com.google.api.server.spi.config.model.SchemaRepository)3 ApiKey (com.google.api.server.spi.config.model.ApiKey)2 ApiConfigValidator (com.google.api.server.spi.config.validation.ApiConfigValidator)2 EndpointsPrettyPrinter (com.google.api.server.spi.response.EndpointsPrettyPrinter)2 File (java.io.File)2 URLClassLoader (java.net.URLClassLoader)2 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)1 JacksonFactory (com.google.api.client.json.jackson2.JacksonFactory)1 ApiConfigWriter (com.google.api.server.spi.config.ApiConfigWriter)1 DiscoveryGenerator (com.google.api.server.spi.discovery.DiscoveryGenerator)1 DiscoveryContext (com.google.api.server.spi.discovery.DiscoveryGenerator.DiscoveryContext)1 SwaggerGenerator (com.google.api.server.spi.swagger.SwaggerGenerator)1 SwaggerContext (com.google.api.server.spi.swagger.SwaggerGenerator.SwaggerContext)1 TestEndpoint (com.google.api.server.spi.testing.TestEndpoint)1