|
17 | 17 | import java.io.IOException; |
18 | 18 | import java.io.OutputStream; |
19 | 19 |
|
| 20 | + |
20 | 21 | public class ApiClient { |
21 | | - private final String basePath; |
22 | | - private final HttpRequestFactory httpRequestFactory; |
23 | | - private final ObjectMapper objectMapper; |
24 | | - private HttpTransport httpTransport = new NetHttpTransport(); |
25 | | - private int connectionTimeout = 20000; |
26 | | - private int readTimeout = 20000; |
27 | | - |
28 | | - private static final String defaultBasePath = "https://api.xero.com/api.xro/2.0"; |
29 | | - |
30 | | - // A reasonable default object mapper. Client can pass in a chosen ObjectMapper anyway, this is |
31 | | - // just for reasonable defaults. |
32 | | - private static ObjectMapper createDefaultObjectMapper() { |
33 | | - ObjectMapper objectMapper = |
34 | | - new ObjectMapper() |
| 22 | + private final String basePath; |
| 23 | + private final HttpRequestFactory httpRequestFactory; |
| 24 | + private final ObjectMapper objectMapper; |
| 25 | + private HttpTransport httpTransport = new NetHttpTransport(); |
| 26 | + private int connectionTimeout = 20000; |
| 27 | + private int readTimeout = 20000; |
| 28 | + |
| 29 | + private static final String defaultBasePath = "https://api.xero.com/api.xro/2.0"; |
| 30 | + |
| 31 | + // A reasonable default object mapper. Client can pass in a chosen ObjectMapper anyway, this is just for reasonable defaults. |
| 32 | + private static ObjectMapper createDefaultObjectMapper() { |
| 33 | + ObjectMapper objectMapper = new ObjectMapper() |
35 | 34 | .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) |
36 | 35 | .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) |
37 | 36 | .setDateFormat(new RFC3339DateFormat()) |
38 | 37 | .setSerializationInclusion(Include.NON_EMPTY); |
39 | | - objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); |
40 | | - |
41 | | - ThreeTenModule module = new ThreeTenModule(); |
42 | | - objectMapper.registerModule(module); |
43 | | - return objectMapper; |
44 | | - } |
45 | | - |
46 | | - public ApiClient() { |
47 | | - this(null, null, null, null, null); |
48 | | - } |
49 | | - |
50 | | - public ApiClient( |
51 | | - String basePath, |
52 | | - HttpTransport transport, |
53 | | - HttpRequestInitializer initializer, |
54 | | - ObjectMapper objectMapper, |
55 | | - HttpRequestFactory reqFactory) { |
56 | | - this.basePath = |
57 | | - basePath == null |
58 | | - ? defaultBasePath |
59 | | - : (basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) : basePath); |
60 | | - if (transport != null) { |
61 | | - this.httpTransport = transport; |
| 38 | + objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); |
| 39 | + |
| 40 | + ThreeTenModule module = new ThreeTenModule(); |
| 41 | + objectMapper.registerModule(module); |
| 42 | + return objectMapper; |
| 43 | + } |
| 44 | + |
| 45 | + public ApiClient() { |
| 46 | + this(null, null, null, null,null); |
| 47 | + } |
| 48 | + |
| 49 | + public ApiClient( |
| 50 | + String basePath, |
| 51 | + HttpTransport transport, |
| 52 | + HttpRequestInitializer initializer, |
| 53 | + ObjectMapper objectMapper, |
| 54 | + HttpRequestFactory reqFactory |
| 55 | + ) { |
| 56 | + this.basePath = basePath == null ? defaultBasePath : ( |
| 57 | + basePath.endsWith("/") ? basePath.substring(0, basePath.length() - 1) : basePath |
| 58 | + ); |
| 59 | + if (transport != null) { |
| 60 | + this.httpTransport = transport; |
| 61 | + } |
| 62 | + this.httpRequestFactory = (reqFactory != null ? reqFactory : (transport == null ? Utils.getDefaultTransport() : transport).createRequestFactory(initializer) ); |
| 63 | + this.objectMapper = (objectMapper == null ? createDefaultObjectMapper() : objectMapper); |
| 64 | + } |
| 65 | + |
| 66 | + public HttpRequestFactory getHttpRequestFactory() { |
| 67 | + return httpRequestFactory; |
| 68 | + } |
| 69 | + |
| 70 | + public int getConnectionTimeout() { |
| 71 | + return connectionTimeout; |
| 72 | + } |
| 73 | + |
| 74 | + public void setConnectionTimeout(int connectionTimeout) { |
| 75 | + this.connectionTimeout = connectionTimeout; |
| 76 | + } |
| 77 | + |
| 78 | + public int getReadTimeout() { |
| 79 | + return readTimeout; |
| 80 | + } |
| 81 | + |
| 82 | + public void setReadTimeout(int readTimeout) { |
| 83 | + this.readTimeout = readTimeout; |
| 84 | + } |
| 85 | + |
| 86 | + public HttpTransport getHttpTransport() { |
| 87 | + return httpTransport; |
| 88 | + } |
| 89 | + |
| 90 | + public void setHttpTransport(HttpTransport transport) { |
| 91 | + this.httpTransport = transport; |
62 | 92 | } |
63 | | - this.httpRequestFactory = |
64 | | - (reqFactory != null |
65 | | - ? reqFactory |
66 | | - : (transport == null ? Utils.getDefaultTransport() : transport) |
67 | | - .createRequestFactory(initializer)); |
68 | | - this.objectMapper = (objectMapper == null ? createDefaultObjectMapper() : objectMapper); |
69 | | - } |
70 | | - |
71 | | - public HttpRequestFactory getHttpRequestFactory() { |
72 | | - return httpRequestFactory; |
73 | | - } |
74 | | - |
75 | | - public int getConnectionTimeout() { |
76 | | - return connectionTimeout; |
77 | | - } |
78 | | - |
79 | | - public void setConnectionTimeout(int connectionTimeout) { |
80 | | - this.connectionTimeout = connectionTimeout; |
81 | | - } |
82 | | - |
83 | | - public int getReadTimeout() { |
84 | | - return readTimeout; |
85 | | - } |
86 | | - |
87 | | - public void setReadTimeout(int readTimeout) { |
88 | | - this.readTimeout = readTimeout; |
89 | | - } |
90 | | - |
91 | | - public HttpTransport getHttpTransport() { |
92 | | - return httpTransport; |
93 | | - } |
94 | | - |
95 | | - public void setHttpTransport(HttpTransport transport) { |
96 | | - this.httpTransport = transport; |
97 | | - } |
98 | | - |
99 | | - public String getBasePath() { |
100 | | - return basePath; |
101 | | - } |
102 | | - |
103 | | - public ObjectMapper getObjectMapper() { |
104 | | - return objectMapper; |
105 | | - } |
106 | | - |
107 | | - public class JacksonJsonHttpContent extends AbstractHttpContent { |
108 | | - /* A POJO that can be serialized with a com.fasterxml Jackson ObjectMapper */ |
109 | | - private final Object data; |
110 | | - |
111 | | - public JacksonJsonHttpContent(Object data) { |
112 | | - super(Json.MEDIA_TYPE); |
113 | | - this.data = data; |
| 93 | + |
| 94 | + public String getBasePath() { |
| 95 | + return basePath; |
114 | 96 | } |
115 | 97 |
|
116 | | - @Override |
117 | | - public void writeTo(OutputStream out) throws IOException { |
118 | | - objectMapper.writeValue(out, data); |
| 98 | + public ObjectMapper getObjectMapper() { |
| 99 | + return objectMapper; |
119 | 100 | } |
120 | | - } |
121 | 101 |
|
122 | | - // Builder pattern to get API instances for this client. |
| 102 | + public class JacksonJsonHttpContent extends AbstractHttpContent { |
| 103 | + /* A POJO that can be serialized with a com.fasterxml Jackson ObjectMapper */ |
| 104 | + private final Object data; |
| 105 | + |
| 106 | + public JacksonJsonHttpContent(Object data) { |
| 107 | + super(Json.MEDIA_TYPE); |
| 108 | + this.data = data; |
| 109 | + } |
123 | 110 |
|
124 | | - public AccountingApi accountingApi() { |
125 | | - return new AccountingApi(this); |
126 | | - } |
| 111 | + @Override |
| 112 | + public void writeTo(OutputStream out) throws IOException { |
| 113 | + objectMapper.writeValue(out, data); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Builder pattern to get API instances for this client. |
| 118 | + |
| 119 | + public AccountingApi accountingApi() { |
| 120 | + return new AccountingApi(this); |
| 121 | + } |
| 122 | + |
127 | 123 | } |
0 commit comments