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,48 +110,49 @@ 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 {
139138 assert config != null : "config was null" ;
140139 assert objectMapper != null : "objectMapper was null" ;
141140
142- if (config .getApiKey () == null ) {
141+ if (config .getApiKey () == null ) {
143142 throw new AirtableException ("Missing Airtable API-Key" );
144143 }
145- if (config .getEndpointUrl () == null ) {
144+ if (config .getEndpointUrl () == null ) {
146145 throw new AirtableException ("Missing endpointUrl" );
147146 }
148147
149148 this .config = config ;
150149
151- if (config .getTimeout () != null ) {
150+ if (config .getTimeout () != null ) {
152151 LOG .info ("Set connection timeout to: " + config .getTimeout () + "ms." );
153152 Unirest .setTimeouts (config .getTimeout (), config .getTimeout ());
154153 }
155154
156- setProxy (config .getEndpointUrl ());
155+ configureProxy (config .getEndpointUrl ());
157156
158157 // Only one time
159158 Unirest .setObjectMapper (objectMapper );
@@ -176,58 +175,63 @@ public Airtable configure(Configuration config, ObjectMapper objectMapper) throw
176175
177176 /**
178177 * Set Proxy Manually.
179- *
180- * @param proxy
178+ *
179+ * @param proxy
181180 */
182- public void setProxyManual (String proxy ){
183- if (proxy != null && !proxy .isEmpty () && !proxy .equals (" " )){
184- Unirest . setProxy (HttpHost . create ( proxy ) );
181+ public void setProxy (String proxy ) {
182+ if (proxy != null && !proxy .isEmpty () && !proxy .equals (" " )) {
183+ this . config . setProxy (proxy );
185184 }
186185 }
187-
186+
188187 /**
189- * Set Proxy environment for Unirest.
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>
190192 *
191- * Proxy will be ignored for endpointUrls containing <code>localhost</code> or <code>127.0.0.1,/code>
192193 * @param endpointUrl
193194 */
194- private void setProxy (String endpointUrl ) {
195- final String httpProxy = System .getenv ("http_proxy" );
196- final String httpsProxy = System .getenv ("https_proxy" );
197- if (httpProxy != null
198- && (endpointUrl .contains ("127.0.0.1" )
199- || endpointUrl .contains ("localhost" ))) {
200- LOG .info ("Use Proxy: ignored for 'localhost' ann '127.0.0.1'" );
201- Unirest .setProxy (null );
202- } else if (httpsProxy != 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
203205 && (endpointUrl .contains ("https" ))) {
204- LOG .info ("Use Proxy: Environment variable 'https_proxy' found and used: " + httpsProxy );
205- Unirest .setProxy (HttpHost .create (httpsProxy ));
206- } else if (httpProxy != null
207- && (endpointUrl .contains ("http" ))){
208- LOG .info ("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy );
209- Unirest .setProxy (HttpHost .create (httpProxy ));
210- } else {
211- Unirest .setProxy (null );
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+ }
212215 }
213216 }
214217
215218 /**
216219 * Getting the base by given property <code>AIRTABLE_BASE</code>.
217220 *
218221 * @return the base object.
219- * @throws com.sybit.airtable.exception.AirtableException Missing Airtable_BASE
222+ * @throws com.sybit.airtable.exception.AirtableException Missing
223+ * Airtable_BASE
220224 */
221225 public Base base () throws AirtableException {
222226
223227 LOG .info ("Using Java property '-D" + AIRTABLE_BASE + "' to get key." );
224228 String val = System .getProperty (AIRTABLE_BASE );
225229
226- if (val == null ) {
230+ if (val == null ) {
227231 LOG .info ("Environment-Variable: Using OS environment '" + AIRTABLE_BASE + "' to get base name." );
228232 val = System .getenv (AIRTABLE_BASE );
229233 }
230- if (val == null ) {
234+ if (val == null ) {
231235 val = getCredentialProperty (AIRTABLE_BASE );
232236 }
233237
@@ -236,12 +240,14 @@ public Base base() throws AirtableException {
236240
237241 /**
238242 * Builder method to create base of given base id.
243+ *
239244 * @param base the base id.
240245 * @return
241- * @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was Null
246+ * @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was
247+ * Null
242248 */
243249 public Base base (String base ) throws AirtableException {
244- if (base == null ) {
250+ if (base == null ) {
245251 throw new AirtableException ("base was null" );
246252 }
247253 final Base b = new Base (base , this );
@@ -257,7 +263,7 @@ public void setConfig(Configuration config) {
257263 assert config != null : "config was null" ;
258264
259265 this .config = config ;
260- setProxy (config .getEndpointUrl ());
266+ configureProxy (config .getEndpointUrl ());
261267 }
262268
263269 /**
@@ -306,6 +312,6 @@ private String getCredentialProperty(String key) {
306312
307313 public void setEndpointUrl (String endpointUrl ) {
308314 this .config .setEndpointUrl (endpointUrl );
309- setProxy (endpointUrl );
315+ configureProxy (endpointUrl );
310316 }
311317}
0 commit comments