11package io .intercom .api ;
22
3+ import com .fasterxml .jackson .databind .JavaType ;
34import com .fasterxml .jackson .databind .ObjectMapper ;
45import com .fasterxml .jackson .databind .SerializationFeature ;
56import com .google .common .collect .Lists ;
@@ -73,57 +74,46 @@ private HttpClient(URI uri, Map<String, String> headers) {
7374 }
7475
7576 public <T > T get (Class <T > reqres ) throws IntercomException {
76- HttpURLConnection conn = null ;
77- try {
78- conn = initializeConnection ( uri , "GET" );
79- return runRequest ( uri , reqres , conn );
80- } catch ( IOException e ) {
81- return throwLocalException ( e );
82- } finally {
83- IOUtils . disconnectQuietly ( conn );
84- }
77+ return get ( getJavaType ( reqres )) ;
78+ }
79+
80+ < T > T get ( JavaType responseType ) throws IntercomException {
81+ return executeHttpMethod ( "GET" , null , responseType );
82+ }
83+
84+ public < T > T delete ( Class < T > reqres ) {
85+ return executeHttpMethod ( "DELETE" , null , getJavaType ( reqres ));
8586 }
8687
8788 public <T , E > T put (Class <T > reqres , E entity ) {
8889 headers .put ("Content-Type" , APPLICATION_JSON );
89- HttpURLConnection conn = null ;
90- try {
91- conn = initializeConnection (uri , "PUT" );
92- prepareRequestEntity (entity , conn );
93- return runRequest (uri , reqres , conn );
94- } catch (IOException e ) {
95- return throwLocalException (e );
96- } finally {
97- IOUtils .disconnectQuietly (conn );
98- }
90+ return executeHttpMethod ("PUT" , (E ) entity , getJavaType (reqres ));
9991 }
10092
10193 public <T , E > T post (Class <T > reqres , E entity ) {
10294 headers .put ("Content-Type" , APPLICATION_JSON );
103- HttpURLConnection conn = null ;
104- try {
105- conn = initializeConnection (uri , "POST" );
106- prepareRequestEntity (entity , conn );
107- return runRequest (uri , reqres , conn );
108- } catch (IOException e ) {
109- return throwLocalException (e );
110- } finally {
111- IOUtils .disconnectQuietly (conn );
112- }
95+ return executeHttpMethod ("POST" , entity , getJavaType (reqres ));
11396 }
11497
115- public <T > T delete ( Class < T > reqres ) {
98+ private <T , E > T executeHttpMethod ( String method , E entity , JavaType responseType ) {
11699 HttpURLConnection conn = null ;
117100 try {
118- conn = initializeConnection (uri , "DELETE" );
119- return runRequest (uri , reqres , conn );
101+ conn = initializeConnection (uri , method );
102+ if (entity != null ) {
103+ prepareRequestEntity (entity , conn );
104+ }
105+ return runRequest (uri , responseType , conn );
120106 } catch (IOException e ) {
121107 return throwLocalException (e );
122108 } finally {
123109 IOUtils .disconnectQuietly (conn );
124110 }
125111 }
126112
113+ private <T > JavaType getJavaType (Class <T > reqres ) {
114+ return objectMapper .getTypeFactory ().constructType (reqres );
115+ }
116+
127117 // trick java with a dummy return
128118 private <T > T throwLocalException (IOException e ) {
129119 throw new IntercomException (String .format ("Local exception calling [%s]. Check connectivity and settings. [%s]" , uri .toASCIIString (), e .getMessage ()), e );
@@ -151,13 +141,12 @@ private HttpURLConnection initializeConnection(URI uri, String method) throws IO
151141 return conn ;
152142 }
153143
154- private <T > T runRequest (URI uri , Class < T > response , HttpURLConnection conn ) throws IOException {
144+ private <T > T runRequest (URI uri , JavaType javaType , HttpURLConnection conn ) throws IOException {
155145 conn .connect ();
156146 final int responseCode = conn .getResponseCode ();
157147 if (responseCode >= 200 && responseCode < 300 ) {
158- return handleSuccess (response , conn , responseCode );
148+ return handleSuccess (javaType , conn , responseCode );
159149 } else {
160- // errors are redirects for now
161150 return handleError (uri , conn , responseCode );
162151 }
163152 }
@@ -175,23 +164,27 @@ private <T> T handleError(URI uri, HttpURLConnection conn, int responseCode) thr
175164 return throwException (responseCode , errors );
176165 }
177166
178- private <T > T handleSuccess (Class < T > response , HttpURLConnection conn , int responseCode ) throws IOException {
179- if (responseCode == 202 || responseCode == 204 || Void . class . equals ( response )) {
167+ private <T > T handleSuccess (JavaType javaType , HttpURLConnection conn , int responseCode ) throws IOException {
168+ if (shouldSkipResponseEntity ( javaType , conn , responseCode )) {
180169 return null ;
181170 } else {
182- return readEntity (conn , responseCode , response );
171+ return readEntity (conn , responseCode , javaType );
183172 }
184173 }
185174
186- private <T > T readEntity (HttpURLConnection conn , int responseCode , Class <T > entityType ) throws IOException {
175+ private boolean shouldSkipResponseEntity (JavaType javaType , HttpURLConnection conn , int responseCode ) {
176+ return responseCode == 202 || responseCode == 204 || Void .class .equals (javaType .getRawClass ()) || "DELETE" .equals (conn .getRequestMethod ());
177+ }
178+
179+ private <T > T readEntity (HttpURLConnection conn , int responseCode , JavaType javaType ) throws IOException {
187180 final InputStream entityStream = conn .getInputStream ();
188181 try {
189182 if (logger .isDebugEnabled ()) {
190183 final String text = CharStreams .toString (new InputStreamReader (entityStream ));
191184 logger .debug ("api server response status[{}] --\n {}\n -- " , responseCode , text );
192- return objectMapper .readValue (text , entityType );
185+ return objectMapper .readValue (text , javaType );
193186 } else {
194- return objectMapper .readValue (entityStream , entityType );
187+ return objectMapper .readValue (entityStream , javaType );
195188 }
196189 } finally {
197190 IOUtils .closeQuietly (entityStream );
0 commit comments