Skip to content
This repository was archived by the owner on Mar 31, 2025. It is now read-only.

Commit f2edf9b

Browse files
authored
Merge pull request #18 from Sybit-Education/develop
configurable Proxy
2 parents 86de3f4 + 2c864c3 commit f2edf9b

13 files changed

Lines changed: 180 additions & 128 deletions

File tree

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ before_install:
55
- chmod +x ./gradlew
66
script:
77
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then bash ./gradlew check; fi'
8-
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./gradlew test integrationTest; fi'
9-
- "./gradlew sendCoverageToCodacy"
8+
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./gradlew test integrationTest sendCoverageToCodacy; fi'
109
deploy:
1110
- provider: releases
1211
api_key:

src/itest/java/com/sybit/airtable/AirtableTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void airtableConfigTest() throws AirtableException{
4141

4242
Airtable airtable = new Airtable();
4343
assertNull(airtable.getConfig());
44-
airtable.configure(new Configuration("KEY","URL"));
44+
airtable.configure(new Configuration("KEY","URL","PROXY"));
4545
assertEquals(airtable.apiKey(),"KEY");
4646
assertEquals(airtable.endpointUrl(),"URL");
4747

src/main/java/com/sybit/airtable/Airtable.java

Lines changed: 83 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package com.sybit.airtable;
88

9-
109
import com.mashape.unirest.http.ObjectMapper;
1110
import com.mashape.unirest.http.Unirest;
1211
import com.sybit.airtable.converter.ListConverter;
@@ -29,31 +28,32 @@
2928
import java.util.Map;
3029
import 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
*/
4545
public 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
}

src/main/java/com/sybit/airtable/Base.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ public class Base {
2424

2525
private final Map<String, Table> tableMap = new HashMap<>();
2626

27-
private final String base;
27+
private final String baseName;
2828

2929
private final Airtable parent;
3030

3131

3232
/**
33-
* Create Airtable Base with given base ID.
33+
* Create Airtable Base with given baseName ID.
3434
*
35-
* @param base base ID could be found at https://airtable.com if you select your current base.
35+
* @param name base ID could be found at https://airtable.com if you select your current baseName.
3636
* @param airtable parent airtable object
3737
*/
38-
public Base(String base, Airtable airtable) {
39-
this.base = base;
38+
public Base(String name, Airtable airtable) {
39+
assert name != null : "baseName was null";
40+
assert airtable != null : "airtable was null";
41+
42+
this.baseName = name;
4043
this.parent = airtable;
4144
}
4245

@@ -54,7 +57,6 @@ public Airtable airtable() {
5457
* @return Object to access table.
5558
*/
5659
public Table table(String name) {
57-
5860
return table(name, Records.class);
5961
}
6062

@@ -65,6 +67,8 @@ public Table table(String name) {
6567
* @return Object to access table.
6668
*/
6769
public Table table(String name, Class clazz) {
70+
assert name != null : "name was null";
71+
assert clazz != null : "clazz was null";
6872

6973
if(!tableMap.containsKey(name)) {
7074
LOG.debug("Create new instance for table [" + name + "]");
@@ -77,10 +81,10 @@ public Table table(String name, Class clazz) {
7781
}
7882

7983
/**
80-
* Get base id of base.
81-
* @return base id
84+
* Get baseName id of baseName.
85+
* @return baseName id
8286
*/
8387
public String name() {
84-
return base;
88+
return baseName;
8589
}
8690
}

0 commit comments

Comments
 (0)