66 */
77package com .sybit .airtable ;
88
9-
109import com .mashape .unirest .http .ObjectMapper ;
1110import com .mashape .unirest .http .Unirest ;
1211import com .sybit .airtable .converter .ListConverter ;
2928import java .util .Map ;
3029import java .util .Properties ;
3130
32-
3331/**
34- * Representation Class of Airtable.
35- * It is the entry class to access Airtable data.
32+ * Representation Class of Airtable. It is the entry class to access Airtable
33+ * data.
3634 *
37- * The API key could be passed to the app by
38- * + defining Java property <code>AIRTABLE_API_KEY</code> (e.g. <code>-DAIRTABLE_API_KEY=foo</code>).
39- * + defining OS environment variable <code>AIRTABLE_API_KEY</code> (e.g. <code>export AIRTABLE_API_KEY=foo</code>).
40- * + defining property file `credentials.properties` in root classpath containing key/value <code>AIRTABLE_API_KEY=foo</code>.
41- * + On the other hand the API-key could also be added by using the method <code>Airtable.configure(String apiKey)</code>.
35+ * The API key could be passed to the app by + defining Java property
36+ * <code>AIRTABLE_API_KEY</code> (e.g. <code>-DAIRTABLE_API_KEY=foo</code>). +
37+ * defining OS environment variable <code>AIRTABLE_API_KEY</code> (e.g.
38+ * <code>export AIRTABLE_API_KEY=foo</code>). + defining property file
39+ * `credentials.properties` in root classpath containing key/value
40+ * <code>AIRTABLE_API_KEY=foo</code>. + On the other hand the API-key could also
41+ * be added by using the method <code>Airtable.configure(String apiKey)</code>.
4242 *
4343 * @since 0.1
4444 */
4545public class Airtable {
4646
47- private static final Logger LOG = LoggerFactory .getLogger ( Airtable .class );
47+ private static final Logger LOG = LoggerFactory .getLogger (Airtable .class );
4848
4949 private static final String AIRTABLE_API_KEY = "AIRTABLE_API_KEY" ;
5050 private static final String AIRTABLE_BASE = "AIRTABLE_BASE" ;
5151
5252 private Configuration config ;
5353
5454 /**
55- * Configure, <code>AIRTABLE_API_KEY</code> passed by Java property, enviroment variable
56- * or within credentials.properties.
55+ * Configure, <code>AIRTABLE_API_KEY</code> passed by Java property,
56+ * enviroment variable or within credentials.properties.
5757 *
5858 * @return An Airtable instance configured with GsonObjectMapper
5959 * @throws com.sybit.airtable.exception.AirtableException Missing API-Key
@@ -64,8 +64,8 @@ public Airtable configure() throws AirtableException {
6464 }
6565
6666 /**
67- * Configure, <code>AIRTABLE_API_KEY</code> passed by Java property, enviroment variable
68- * or within credentials.properties.
67+ * Configure, <code>AIRTABLE_API_KEY</code> passed by Java property,
68+ * enviroment variable or within credentials.properties.
6969 *
7070 * @param objectMapper A custom ObjectMapper implementation
7171 * @return An Airtable instance configured with supplied ObjectMapper
@@ -74,22 +74,20 @@ public Airtable configure() throws AirtableException {
7474 @ SuppressWarnings ("UnusedReturnValue" )
7575 public Airtable configure (ObjectMapper objectMapper ) throws AirtableException {
7676
77- LOG .info ( "System-Property: Using Java property '-D" + AIRTABLE_API_KEY + "' to get apikey." );
77+ LOG .info ("System-Property: Using Java property '-D" + AIRTABLE_API_KEY + "' to get apikey." );
7878 String airtableApi = System .getProperty (AIRTABLE_API_KEY );
7979
80- if (airtableApi == null ) {
81- LOG .info ( "Environment-Variable: Using OS environment '" + AIRTABLE_API_KEY + "' to get apikey." );
80+ if (airtableApi == null ) {
81+ LOG .info ("Environment-Variable: Using OS environment '" + AIRTABLE_API_KEY + "' to get apikey." );
8282 airtableApi = System .getenv (AIRTABLE_API_KEY );
8383 }
84- if (airtableApi == null ) {
84+ if (airtableApi == null ) {
8585 airtableApi = getCredentialProperty (AIRTABLE_API_KEY );
8686 }
8787
8888 return this .configure (airtableApi , objectMapper );
8989 }
9090
91-
92-
9391 /**
9492 * Configure Airtable.
9593 *
@@ -112,103 +110,128 @@ public Airtable configure(String apiKey) throws AirtableException {
112110 */
113111 @ SuppressWarnings ("WeakerAccess" )
114112 public Airtable configure (String apiKey , ObjectMapper objectMapper ) throws AirtableException {
115- return configure (new Configuration (apiKey , Configuration .ENDPOINT_URL ), objectMapper );
113+ return configure (new Configuration (apiKey , Configuration .ENDPOINT_URL , null ), objectMapper );
116114 }
117115
118116 /**
119117 *
120118 * @param config
121119 * @return
122- * @throws com.sybit.airtable.exception.AirtableException Missing API-Key or Endpoint
120+ * @throws com.sybit.airtable.exception.AirtableException Missing API-Key or
121+ * Endpoint
123122 */
124123 @ SuppressWarnings ("WeakerAccess" )
125124 public Airtable configure (Configuration config ) throws AirtableException {
126125 return configure (config , new GsonObjectMapper ());
127126 }
128127
129-
130128 /**
131129 *
132130 * @param config
133131 * @param objectMapper A custom ObjectMapper implementation
134132 * @return
135- * @throws com.sybit.airtable.exception.AirtableException Missing API-Key or Endpoint
133+ * @throws com.sybit.airtable.exception.AirtableException Missing API-Key or
134+ * Endpoint
136135 */
137136 @ SuppressWarnings ("WeakerAccess" )
138137 public Airtable configure (Configuration config , ObjectMapper objectMapper ) throws AirtableException {
139- if (config .getApiKey () == null ) {
138+ assert config != null : "config was null" ;
139+ assert objectMapper != null : "objectMapper was null" ;
140+
141+ if (config .getApiKey () == null ) {
140142 throw new AirtableException ("Missing Airtable API-Key" );
141143 }
142- if (config .getEndpointUrl () == null ) {
144+ if (config .getEndpointUrl () == null ) {
143145 throw new AirtableException ("Missing endpointUrl" );
144146 }
145147
146148 this .config = config ;
147149
148- if (config .getTimeout () != null ) {
150+ if (config .getTimeout () != null ) {
149151 LOG .info ("Set connection timeout to: " + config .getTimeout () + "ms." );
150152 Unirest .setTimeouts (config .getTimeout (), config .getTimeout ());
151153 }
152154
153- setProxy (config .getEndpointUrl ());
155+ configureProxy (config .getEndpointUrl ());
154156
155157 // Only one time
156158 Unirest .setObjectMapper (objectMapper );
157159
158160 // Add specific Converter for Date
159161 DateTimeConverter dtConverter = new DateConverter ();
160- ListConverter lConverter = new ListConverter ();
161- MapConverter thConverter = new MapConverter ();
162-
163- lConverter .setListClass (Attachment .class );
164- thConverter .setMapClass (Thumbnail .class );
165162 dtConverter .setPattern ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
166-
167163 ConvertUtils .register (dtConverter , Date .class );
164+
165+ ListConverter lConverter = new ListConverter ();
166+ lConverter .setListClass (Attachment .class );
168167 ConvertUtils .register (lConverter , List .class );
169- ConvertUtils .register (thConverter , Map .class );
170168
169+ MapConverter thConverter = new MapConverter ();
170+ thConverter .setMapClass (Thumbnail .class );
171+ ConvertUtils .register (thConverter , Map .class );
171172
172173 return this ;
173174 }
174175
175176 /**
176- * Set Proxy environment for Unirest.
177+ * Set Proxy Manually.
178+ *
179+ * @param proxy
180+ */
181+ public void setProxy (String proxy ) {
182+ if (proxy != null && !proxy .isEmpty () && !proxy .equals (" " )) {
183+ this .config .setProxy (proxy );
184+ }
185+ }
186+
187+ /**
188+ * Set Proxy environment in Configuration.
189+ *
190+ * Proxy will be ignored for endpointUrls containing <code>localhost</code>
191+ * or <code>127.0.0.1,/code>
177192 *
178- * Proxy will be ignored for endpointUrls containing <code>localhost</code> or <code>127.0.0.1,/code>
179193 * @param endpointUrl
180194 */
181- private void setProxy (String endpointUrl ) {
182- final String httpProxy = System .getenv ("http_proxy" );
183- if (httpProxy != null
184- && (endpointUrl .contains ("127.0.0.1" )
185- || endpointUrl .contains ("localhost" ))) {
186- LOG .info ("Use Proxy: ignored for 'localhost' ann '127.0.0.1'" );
187- Unirest .setProxy (null );
188- } else if (httpProxy != null ) {
189- LOG .info ("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy );
190- Unirest .setProxy (HttpHost .create (httpProxy ));
191- } else {
192- Unirest .setProxy (null );
195+ private void configureProxy (String endpointUrl ) {
196+ if (this .config .getProxy () == null ) {
197+ final String httpProxy = System .getenv ("http_proxy" );
198+ final String httpsProxy = System .getenv ("https_proxy" );
199+ if (httpProxy != null
200+ && (endpointUrl .contains ("127.0.0.1" )
201+ || endpointUrl .contains ("localhost" ))) {
202+ LOG .info ("Use Proxy: ignored for 'localhost' ann '127.0.0.1'" );
203+ this .config .setProxy (null );
204+ } else if (httpsProxy != null
205+ && (endpointUrl .contains ("https" ))) {
206+ LOG .info ("Use Proxy: Environment variable 'https_proxy' found and used: " + httpsProxy );
207+ this .config .setProxy (httpProxy );
208+ } else if (httpProxy != null
209+ && (endpointUrl .contains ("http" ))) {
210+ LOG .info ("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy );
211+ this .config .setProxy (httpsProxy );
212+ } else {
213+ this .config .setProxy (null );
214+ }
193215 }
194216 }
195217
196218 /**
197219 * Getting the base by given property <code>AIRTABLE_BASE</code>.
198220 *
199221 * @return the base object.
200- * @throws com.sybit.airtable.exception.AirtableException Missing Airtable_BASE
222+ * @throws com.sybit.airtable.exception.AirtableException Missing
223+ * Airtable_BASE
201224 */
202225 public Base base () throws AirtableException {
203226
204227 LOG .info ("Using Java property '-D" + AIRTABLE_BASE + "' to get key." );
205228 String val = System .getProperty (AIRTABLE_BASE );
206229
207- if (val == null ) {
230+ if (val == null ) {
208231 LOG .info ("Environment-Variable: Using OS environment '" + AIRTABLE_BASE + "' to get base name." );
209232 val = System .getenv (AIRTABLE_BASE );
210233 }
211- if (val == null ) {
234+ if (val == null ) {
212235 val = getCredentialProperty (AIRTABLE_BASE );
213236 }
214237
@@ -217,12 +240,14 @@ public Base base() throws AirtableException {
217240
218241 /**
219242 * Builder method to create base of given base id.
243+ *
220244 * @param base the base id.
221245 * @return
222- * @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was Null
246+ * @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was
247+ * Null
223248 */
224249 public Base base (String base ) throws AirtableException {
225- if (base == null ) {
250+ if (base == null ) {
226251 throw new AirtableException ("base was null" );
227252 }
228253 final Base b = new Base (base , this );
@@ -235,8 +260,10 @@ public Configuration getConfig() {
235260 }
236261
237262 public void setConfig (Configuration config ) {
263+ assert config != null : "config was null" ;
264+
238265 this .config = config ;
239- setProxy (config .getEndpointUrl ());
266+ configureProxy (config .getEndpointUrl ());
240267 }
241268
242269 /**
@@ -285,6 +312,6 @@ private String getCredentialProperty(String key) {
285312
286313 public void setEndpointUrl (String endpointUrl ) {
287314 this .config .setEndpointUrl (endpointUrl );
288- setProxy (endpointUrl );
315+ configureProxy (endpointUrl );
289316 }
290317}
0 commit comments