github.com/phrase/openapi@v0.0.0-20240514140800-49e8a106740e/openapi-generator/templates/java/libraries/google-api-client/ApiClient.mustache (about) 1 package {{invokerPackage}}; 2 3 import {{apiPackage}}.*; 4 import com.fasterxml.jackson.databind.DeserializationFeature; 5 import com.fasterxml.jackson.databind.ObjectMapper; 6 import com.fasterxml.jackson.databind.SerializationFeature; 7 import org.openapitools.jackson.nullable.JsonNullableModule; 8 {{#joda}} 9 import com.fasterxml.jackson.datatype.joda.JodaModule; 10 {{/joda}} 11 {{#java8}} 12 import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; 13 {{/java8}} 14 {{#threetenbp}} 15 import com.fasterxml.jackson.datatype.threetenbp.ThreeTenModule; 16 {{/threetenbp}} 17 {{#threetenbp}} 18 import org.threeten.bp.*; 19 {{/threetenbp}} 20 import com.google.api.client.googleapis.util.Utils; 21 import com.google.api.client.http.AbstractHttpContent; 22 import com.google.api.client.http.HttpRequestFactory; 23 import com.google.api.client.http.HttpRequestInitializer; 24 import com.google.api.client.http.HttpTransport; 25 import com.google.api.client.json.Json; 26 27 import java.io.IOException; 28 import java.io.OutputStream; 29 30 {{>generatedAnnotation}} 31 public class ApiClient { 32 private final String basePath; 33 private final HttpRequestFactory httpRequestFactory; 34 private final ObjectMapper objectMapper; 35 36 private static final String defaultBasePath = "{{basePath}}"; 37 38 // A reasonable default object mapper. Client can pass in a chosen ObjectMapper anyway, this is just for reasonable defaults. 39 private static ObjectMapper createDefaultObjectMapper() { 40 ObjectMapper objectMapper = new ObjectMapper() 41 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) 42 .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) 43 .setDateFormat(new RFC3339DateFormat()); 44 {{#joda}} 45 objectMapper.registerModule(new JodaModule()); 46 {{/joda}} 47 {{#java8}} 48 objectMapper.registerModule(new JavaTimeModule()); 49 {{/java8}} 50 {{#threetenbp}} 51 ThreeTenModule module = new ThreeTenModule(); 52 module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT); 53 module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME); 54 module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME); 55 objectMapper.registerModule(module); 56 {{/threetenbp}} 57 JsonNullableModule jnm = new JsonNullableModule(); 58 objectMapper.registerModule(jnm); 59 return objectMapper; 60 } 61 62 public ApiClient() { 63 this(null, null, null, null); 64 } 65 66 public ApiClient( 67 String basePath, 68 HttpTransport httpTransport, 69 HttpRequestInitializer initializer, 70 ObjectMapper objectMapper 71 ) { 72 this.basePath = basePath == null ? defaultBasePath : ( 73 basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) : basePath 74 ); 75 this.httpRequestFactory = (httpTransport == null ? Utils.getDefaultTransport() : httpTransport).createRequestFactory(initializer); 76 this.objectMapper = (objectMapper == null ? createDefaultObjectMapper() : objectMapper); 77 } 78 79 public HttpRequestFactory getHttpRequestFactory() { 80 return httpRequestFactory; 81 } 82 83 public String getBasePath() { 84 return basePath; 85 } 86 87 public ObjectMapper getObjectMapper() { 88 return objectMapper; 89 } 90 91 public class JacksonJsonHttpContent extends AbstractHttpContent { 92 /* A POJO that can be serialized with a com.fasterxml Jackson ObjectMapper */ 93 private final Object data; 94 95 public JacksonJsonHttpContent(Object data) { 96 super(Json.MEDIA_TYPE); 97 this.data = data; 98 } 99 100 @Override 101 public void writeTo(OutputStream out) throws IOException { 102 objectMapper.writeValue(out, data); 103 } 104 } 105 106 // Builder pattern to get API instances for this client. 107 {{#apiInfo}}{{#apis}} 108 public {{classname}} {{classVarName}}Api() { 109 return new {{classname}}(this); 110 } 111 {{/apis}}{{/apiInfo}} 112 }