66 */
77package com .sybit .airtable ;
88
9- import com .google .gson .Gson ;
10- import com .google .gson .GsonBuilder ;
11- import com .mashape .unirest .http .ObjectMapper ;
9+
1210import com .mashape .unirest .http .Unirest ;
1311import com .sybit .airtable .exception .AirtableException ;
12+ import com .sybit .airtable .vo .Attachment ;
13+ import com .sybit .airtable .vo .Thumbnail ;
14+
15+ import org .apache .http .HttpHost ;
1416import org .apache .commons .beanutils .ConvertUtils ;
1517import org .apache .commons .beanutils .converters .DateConverter ;
1618import org .apache .commons .beanutils .converters .DateTimeConverter ;
2022import java .io .IOException ;
2123import java .io .InputStream ;
2224import java .util .Date ;
25+ import java .util .List ;
26+ import java .util .Map ;
2327import java .util .Properties ;
2428
29+
2530/**
2631 * Representation Class of Airtable.
2732 * It is the entry class to access Airtable data.
@@ -49,6 +54,7 @@ public class Airtable {
4954 * or within credentials.properties.
5055 *
5156 * @return configured Airtable object.
57+ * @throws com.sybit.airtable.exception.AirtableException Missing API-Key
5258 */
5359 @ SuppressWarnings ("UnusedReturnValue" )
5460 public Airtable configure () throws AirtableException {
@@ -74,6 +80,7 @@ public Airtable configure() throws AirtableException {
7480 *
7581 * @param apiKey API-Key of Airtable.
7682 * @return
83+ * @throws com.sybit.airtable.exception.AirtableException Missing API-Key
7784 */
7885 @ SuppressWarnings ("WeakerAccess" )
7986 public Airtable configure (String apiKey ) throws AirtableException {
@@ -85,6 +92,7 @@ public Airtable configure(String apiKey) throws AirtableException {
8592 * @param apiKey
8693 * @param endpointUrl
8794 * @return
95+ * @throws com.sybit.airtable.exception.AirtableException Missing API-Key or Endpoint
8896 */
8997 @ SuppressWarnings ("WeakerAccess" )
9098 public Airtable configure (String apiKey , String endpointUrl ) throws AirtableException {
@@ -98,40 +106,55 @@ public Airtable configure(String apiKey, String endpointUrl) throws AirtableExce
98106 this .apiKey = apiKey ;
99107 this .endpointUrl = endpointUrl ;
100108
101-
102- final String httpProxy = System .getenv ("http_proxy" );
103- if (httpProxy != null ) {
104- LOG .info ("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy );
105- //Unirest.setProxy(HttpHost.create(httpProxy));
106- }
107-
109+ setProxy (endpointUrl );
108110
109111 // Only one time
110- Unirest .setObjectMapper (new ObjectMapper () {
111- final Gson gson = new GsonBuilder ().create ();
112-
113- public <T > T readValue (String value , Class <T > valueType ) {
114- LOG .debug ("readValue: \n " + value );
115- return gson .fromJson (value , valueType );
116- }
117-
118- public String writeValue (Object value ) {
119- return gson .toJson (value );
120- }
121- });
112+ Unirest .setObjectMapper (new GsonObjectMapper ());
122113
114+
123115 // Add specific Converter for Date
124116 DateTimeConverter dtConverter = new DateConverter ();
117+ ListConverter lConverter = new ListConverter ();
118+ MapConverter thConverter = new MapConverter ();
119+
120+ lConverter .setListClass (Attachment .class );
121+ thConverter .setMapClass (Thumbnail .class );
125122 dtConverter .setPattern ("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
123+
126124 ConvertUtils .register (dtConverter , Date .class );
125+ ConvertUtils .register (lConverter , List .class );
126+ ConvertUtils .register (thConverter , Map .class );
127+
127128
128129 return this ;
129130 }
130131
132+ /**
133+ * Set Proxy environment for Unirest.
134+ *
135+ * Proxy will be ignored for endpointUrls containing <code>localhost</code> or <code>127.0.0.1,/code>
136+ * @param endpointUrl
137+ */
138+ private void setProxy (String endpointUrl ) {
139+ final String httpProxy = System .getenv ("http_proxy" );
140+ if (httpProxy != null
141+ && (endpointUrl .contains ("127.0.0.1" )
142+ || endpointUrl .contains ("localhost" ))) {
143+ LOG .info ("Use Proxy: ignored for 'localhost' ann '127.0.0.1'" );
144+ Unirest .setProxy (null );
145+ } else if (httpProxy != null ) {
146+ LOG .info ("Use Proxy: Environment variable 'http_proxy' found and used: " + httpProxy );
147+ Unirest .setProxy (HttpHost .create (httpProxy ));
148+ } else {
149+ Unirest .setProxy (null );
150+ }
151+ }
152+
131153 /**
132154 * Getting the base by given property <code>AIRTABLE_BASE</code>.
133155 *
134156 * @return the base object.
157+ * @throws com.sybit.airtable.exception.AirtableException Missing Airtable_BASE
135158 */
136159 public Base base () throws AirtableException {
137160
@@ -153,6 +176,7 @@ public Base base() throws AirtableException {
153176 * Builder method to create base of given base id.
154177 * @param base the base id.
155178 * @return
179+ * @throws com.sybit.airtable.exception.AirtableException AIRTABLE_BASE was Null
156180 */
157181 public Base base (String base ) throws AirtableException {
158182 if (base == null ) {
@@ -210,5 +234,6 @@ private String getCredentialProperty(String key) {
210234
211235 public void setEndpointUrl (String endpointUrl ) {
212236 this .endpointUrl = endpointUrl ;
237+ setProxy (endpointUrl );
213238 }
214239}
0 commit comments