Skip to content

Commit

Permalink
Merge pull request #18 from Sybit-Education/develop
Browse files Browse the repository at this point in the history
configurable Proxy
  • Loading branch information
stritti authored May 18, 2017
2 parents 86de3f4 + 2c864c3 commit f2edf9b
Show file tree
Hide file tree
Showing 13 changed files with 180 additions and 128 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ before_install:
- chmod +x ./gradlew
script:
- 'if [ "$TRAVIS_PULL_REQUEST" != "false" ]; then bash ./gradlew check; fi'
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./gradlew test integrationTest; fi'
- "./gradlew sendCoverageToCodacy"
- 'if [ "$TRAVIS_PULL_REQUEST" = "false" ]; then bash ./gradlew test integrationTest sendCoverageToCodacy; fi'
deploy:
- provider: releases
api_key:
Expand Down
2 changes: 1 addition & 1 deletion src/itest/java/com/sybit/airtable/AirtableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void airtableConfigTest() throws AirtableException{

Airtable airtable = new Airtable();
assertNull(airtable.getConfig());
airtable.configure(new Configuration("KEY","URL"));
airtable.configure(new Configuration("KEY","URL","PROXY"));
assertEquals(airtable.apiKey(),"KEY");
assertEquals(airtable.endpointUrl(),"URL");

Expand Down
139 changes: 83 additions & 56 deletions src/main/java/com/sybit/airtable/Airtable.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package com.sybit.airtable;


import com.mashape.unirest.http.ObjectMapper;
import com.mashape.unirest.http.Unirest;
import com.sybit.airtable.converter.ListConverter;
Expand All @@ -29,31 +28,32 @@
import java.util.Map;
import java.util.Properties;


/**
* Representation Class of Airtable.
* It is the entry class to access Airtable data.
* Representation Class of Airtable. It is the entry class to access Airtable
* data.
*
* The API key could be passed to the app by
* + defining Java property <code>AIRTABLE_API_KEY</code> (e.g. <code>-DAIRTABLE_API_KEY=foo</code>).
* + defining OS environment variable <code>AIRTABLE_API_KEY</code> (e.g. <code>export AIRTABLE_API_KEY=foo</code>).
* + defining property file `credentials.properties` in root classpath containing key/value <code>AIRTABLE_API_KEY=foo</code>.
* + On the other hand the API-key could also be added by using the method <code>Airtable.configure(String apiKey)</code>.
* The API key could be passed to the app by + defining Java property
* <code>AIRTABLE_API_KEY</code> (e.g. <code>-DAIRTABLE_API_KEY=foo</code>). +
* defining OS environment variable <code>AIRTABLE_API_KEY</code> (e.g.
* <code>export AIRTABLE_API_KEY=foo</code>). + defining property file
* `credentials.properties` in root classpath containing key/value
* <code>AIRTABLE_API_KEY=foo</code>. + On the other hand the API-key could also
* be added by using the method <code>Airtable.configure(String apiKey)</code>.
*
* @since 0.1
*/
public class Airtable {

private static final Logger LOG = LoggerFactory.getLogger( Airtable.class );
private static final Logger LOG = LoggerFactory.getLogger(Airtable.class);

private static final String AIRTABLE_API_KEY = "AIRTABLE_API_KEY";
private static final String AIRTABLE_BASE = "AIRTABLE_BASE";

private Configuration config;

/**
* Configure, <code>AIRTABLE_API_KEY</code> passed by Java property, enviroment variable
* or within credentials.properties.
* Configure, <code>AIRTABLE_API_KEY</code> passed by Java property,
* enviroment variable or within credentials.properties.
*
* @return An Airtable instance configured with GsonObjectMapper
* @throws com.sybit.airtable.exception.AirtableException Missing API-Key
Expand All @@ -64,8 +64,8 @@ public Airtable configure() throws AirtableException {
}

/**
* Configure, <code>AIRTABLE_API_KEY</code> passed by Java property, enviroment variable
* or within credentials.properties.
* Configure, <code>AIRTABLE_API_KEY</code> passed by Java property,
* enviroment variable or within credentials.properties.
*
* @param objectMapper A custom ObjectMapper implementation
* @return An Airtable instance configured with supplied ObjectMapper
Expand All @@ -74,22 +74,20 @@ public Airtable configure() throws AirtableException {
@SuppressWarnings("UnusedReturnValue")
public Airtable configure(ObjectMapper objectMapper) throws AirtableException {

LOG.info( "System-Property: Using Java property '-D" + AIRTABLE_API_KEY + "' to get apikey.");
LOG.info("System-Property: Using Java property '-D" + AIRTABLE_API_KEY + "' to get apikey.");
String airtableApi = System.getProperty(AIRTABLE_API_KEY);

if(airtableApi == null) {
LOG.info( "Environment-Variable: Using OS environment '" + AIRTABLE_API_KEY + "' to get apikey.");
if (airtableApi == null) {
LOG.info("Environment-Variable: Using OS environment '" + AIRTABLE_API_KEY + "' to get apikey.");
airtableApi = System.getenv(AIRTABLE_API_KEY);
}
if(airtableApi == null) {
if (airtableApi == null) {
airtableApi = getCredentialProperty(AIRTABLE_API_KEY);
}

return this.configure(airtableApi, objectMapper);
}



/**
* Configure Airtable.
*
Expand All @@ -112,103 +110,128 @@ public Airtable configure(String apiKey) throws AirtableException {
*/
@SuppressWarnings("WeakerAccess")
public Airtable configure(String apiKey, ObjectMapper objectMapper) throws AirtableException {
return configure(new Configuration(apiKey, Configuration.ENDPOINT_URL), objectMapper);
return configure(new Configuration(apiKey, Configuration.ENDPOINT_URL, null), objectMapper);
}

/**
*
* @param config
* @return
* @throws com.sybit.airtable.exception.AirtableException Missing API-Key or Endpoint
* @throws com.sybit.airtable.exception.AirtableException Missing API-Key or
* Endpoint
*/
@SuppressWarnings("WeakerAccess")
public Airtable configure(Configuration config) throws AirtableException {
return configure(config, new GsonObjectMapper());
}


/**
*
* @param config
* @param objectMapper A custom ObjectMapper implementation
* @return
* @throws com.sybit.airtable.exception.AirtableException Missing API-Key or Endpoint
* @throws com.sybit.airtable.exception.AirtableException Missing API-Key or
* Endpoint
*/
@SuppressWarnings("WeakerAccess")
public Airtable configure(Configuration config, ObjectMapper objectMapper) throws AirtableException {
if(config.getApiKey() == null) {
assert config != null : "config was null";
assert objectMapper != null : "objectMapper was null";

if (config.getApiKey() == null) {
throw new AirtableException("Missing Airtable API-Key");
}
if(config.getEndpointUrl() == null) {
if (config.getEndpointUrl() == null) {
throw new AirtableException("Missing endpointUrl");
}

this.config = config;

if(config.getTimeout() != null) {
if (config.getTimeout() != null) {
LOG.info("Set connection timeout to: " + config.getTimeout() + "ms.");
Unirest.setTimeouts(config.getTimeout(), config.getTimeout());
}

setProxy(config.getEndpointUrl());
configureProxy(config.getEndpointUrl());

// Only one time
Unirest.setObjectMapper(objectMapper);

// Add specific Converter for Date
DateTimeConverter dtConverter = new DateConverter();
ListConverter lConverter = new ListConverter();
MapConverter thConverter = new MapConverter();

lConverter.setListClass(Attachment.class);
thConverter.setMapClass(Thumbnail.class);
dtConverter.setPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

ConvertUtils.register(dtConverter, Date.class);

ListConverter lConverter = new ListConverter();
lConverter.setListClass(Attachment.class);
ConvertUtils.register(lConverter, List.class);
ConvertUtils.register(thConverter, Map.class);

MapConverter thConverter = new MapConverter();
thConverter.setMapClass(Thumbnail.class);
ConvertUtils.register(thConverter, Map.class);

return this;
}

/**
* Set Proxy environment for Unirest.
* Set Proxy Manually.
*
* @param proxy
*/
public void setProxy(String proxy) {
if (proxy != null && !proxy.isEmpty() && !proxy.equals(" ")) {
this.config.setProxy(proxy);
}
}

/**
* Set Proxy environment in Configuration.
*
* Proxy will be ignored for endpointUrls containing <code>localhost</code>
* or <code>127.0.0.1,/code>
*
* Proxy will be ignored for endpointUrls containing <code>localhost</code> or <code>127.0.0.1,/code>
* @param endpointUrl
*/
private void setProxy(String endpointUrl) {
final String httpProxy = System.getenv("http_proxy");
if(httpProxy != null
&& (endpointUrl.contains("127.0.0.1")
|| endpointUrl.contains("localhost"))) {
LOG.info("Use Proxy: ignored for 'localhost' ann '127.0.0.1'");
Unirest.setProxy(null);
} else if(httpProxy != null) {
LOG.info("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy);
Unirest.setProxy(HttpHost.create(httpProxy));
} else {
Unirest.setProxy(null);
private void configureProxy(String endpointUrl) {
if (this.config.getProxy() == null) {
final String httpProxy = System.getenv("http_proxy");
final String httpsProxy = System.getenv("https_proxy");
if (httpProxy != null
&& (endpointUrl.contains("127.0.0.1")
|| endpointUrl.contains("localhost"))) {
LOG.info("Use Proxy: ignored for 'localhost' ann '127.0.0.1'");
this.config.setProxy(null);
} else if (httpsProxy != null
&& (endpointUrl.contains("https"))) {
LOG.info("Use Proxy: Environment variable 'https_proxy' found and used: " + httpsProxy);
this.config.setProxy(httpProxy);
} else if (httpProxy != null
&& (endpointUrl.contains("http"))) {
LOG.info("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy);
this.config.setProxy(httpsProxy);
} else {
this.config.setProxy(null);
}
}
}

/**
* Getting the base by given property <code>AIRTABLE_BASE</code>.
*
* @return the base object.
* @throws com.sybit.airtable.exception.AirtableException Missing Airtable_BASE
* @throws com.sybit.airtable.exception.AirtableException Missing
* Airtable_BASE
*/
public Base base() throws AirtableException {

LOG.info("Using Java property '-D" + AIRTABLE_BASE + "' to get key.");
String val = System.getProperty(AIRTABLE_BASE);

if(val == null) {
if (val == null) {
LOG.info("Environment-Variable: Using OS environment '" + AIRTABLE_BASE + "' to get base name.");
val = System.getenv(AIRTABLE_BASE);
}
if(val == null) {
if (val == null) {
val = getCredentialProperty(AIRTABLE_BASE);
}

Expand All @@ -217,12 +240,14 @@ public Base base() throws AirtableException {

/**
* Builder method to create base of given base id.
*
* @param base the base id.
* @return
* @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was Null
* @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was
* Null
*/
public Base base(String base) throws AirtableException {
if(base == null) {
if (base == null) {
throw new AirtableException("base was null");
}
final Base b = new Base(base, this);
Expand All @@ -235,8 +260,10 @@ public Configuration getConfig() {
}

public void setConfig(Configuration config) {
assert config != null : "config was null";

this.config = config;
setProxy(config.getEndpointUrl());
configureProxy(config.getEndpointUrl());
}

/**
Expand Down Expand Up @@ -285,6 +312,6 @@ private String getCredentialProperty(String key) {

public void setEndpointUrl(String endpointUrl) {
this.config.setEndpointUrl(endpointUrl);
setProxy(endpointUrl);
configureProxy(endpointUrl);
}
}
22 changes: 13 additions & 9 deletions src/main/java/com/sybit/airtable/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,22 @@ public class Base {

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

private final String base;
private final String baseName;

private final Airtable parent;


/**
* Create Airtable Base with given base ID.
* Create Airtable Base with given baseName ID.
*
* @param base base ID could be found at https://airtable.com if you select your current base.
* @param name base ID could be found at https://airtable.com if you select your current baseName.
* @param airtable parent airtable object
*/
public Base(String base, Airtable airtable) {
this.base = base;
public Base(String name, Airtable airtable) {
assert name != null : "baseName was null";
assert airtable != null : "airtable was null";

this.baseName = name;
this.parent = airtable;
}

Expand All @@ -54,7 +57,6 @@ public Airtable airtable() {
* @return Object to access table.
*/
public Table table(String name) {

return table(name, Records.class);
}

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

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

/**
* Get base id of base.
* @return base id
* Get baseName id of baseName.
* @return baseName id
*/
public String name() {
return base;
return baseName;
}
}
Loading

0 comments on commit f2edf9b

Please sign in to comment.