11package com .skyflow .utils ;
22
3+ import com .google .gson .JsonElement ;
34import com .google .gson .JsonObject ;
4- import com .google .gson .JsonParser ;
55import com .skyflow .errors .SkyflowException ;
66
77import java .io .*;
88import java .net .HttpURLConnection ;
99import java .net .URL ;
1010import java .nio .charset .StandardCharsets ;
1111import java .util .HashMap ;
12+ import java .util .List ;
1213import java .util .Map ;
14+ import java .util .Objects ;
1315
1416public final class HttpUtility {
1517
@@ -33,18 +35,18 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
3335 connection .setRequestProperty ("content-type" , "application/json" );
3436 connection .setRequestProperty ("Accept" , "*/*" );
3537
36- if (headers != null && headers .size () > 0 ) {
38+ if (headers != null && ! headers .isEmpty () ) {
3739 for (Map .Entry <String , String > entry : headers .entrySet ())
3840 connection .setRequestProperty (entry .getKey (), entry .getValue ());
3941
4042 // append dynamic boundary if content-type is multipart/form-data
4143 if (headers .containsKey ("content-type" )) {
42- if (headers .get ("content-type" ) == "multipart/form-data" ) {
44+ if (Objects . equals ( headers .get ("content-type" ), "multipart/form-data" ) ) {
4345 connection .setRequestProperty ("content-type" , "multipart/form-data; boundary=" + boundary );
4446 }
4547 }
4648 }
47- if (params != null && params .size () > 0 ) {
49+ if (params != null && ! params .isEmpty () ) {
4850 connection .setDoOutput (true );
4951 try (DataOutputStream wr = new DataOutputStream (connection .getOutputStream ())) {
5052 byte [] input = null ;
@@ -63,12 +65,12 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
6365 }
6466 }
6567
66- int status = connection .getResponseCode ();
68+ int httpCode = connection .getResponseCode ();
6769 String requestID = connection .getHeaderField ("x-request-id" );
6870 HttpUtility .requestID = requestID .split ("," )[0 ];
69-
71+ Map < String , List < String >> responseHeaders = connection . getHeaderFields ();
7072 Reader streamReader ;
71- if (status > 299 ) {
73+ if (httpCode > 299 ) {
7274 if (connection .getErrorStream () != null )
7375 streamReader = new InputStreamReader (connection .getErrorStream ());
7476 else {
@@ -86,9 +88,8 @@ public static String sendRequest(String method, URL url, JsonObject params, Map<
8688 response .append (inputLine );
8789 }
8890
89- if (status > 299 ) {
90- String errorMsg = appendRequestIdToErrorObj (status , response .toString (), requestID );
91- throw new SkyflowException (errorMsg );
91+ if (httpCode > 299 ) {
92+ throw new SkyflowException (httpCode , new Throwable (), responseHeaders , response .toString ());
9293 }
9394 } finally {
9495 if (in != null ) {
@@ -127,13 +128,14 @@ public static String formatJsonToMultiPartFormDataString(JsonObject requestBody,
127128
128129 private static HashMap <String , String > convertJsonToMap (JsonObject json , String rootKey ) {
129130 HashMap <String , String > currentMap = new HashMap <>();
130- for (Object key : json .keySet ()) {
131- Object currentValue = json .get ((String ) key );
132- String currentKey = rootKey .length () != 0 ? rootKey + '[' + key .toString () + ']' : rootKey + key .toString ();
133- if (currentValue instanceof JsonObject ) {
131+ Map <String , JsonElement > jsonMap = json .asMap ();
132+ for (String key : jsonMap .keySet ()) {
133+ JsonElement currentValue = jsonMap .get (key );
134+ String currentKey = !rootKey .isEmpty () ? rootKey + '[' + key + ']' : rootKey + key ;
135+ if (currentValue .isJsonObject ()) {
134136 currentMap .putAll (convertJsonToMap ((JsonObject ) currentValue , currentKey ));
135137 } else {
136- currentMap .put (currentKey , currentValue .toString ());
138+ currentMap .put (currentKey , currentValue .getAsString ());
137139 }
138140 }
139141 return currentMap ;
@@ -156,22 +158,6 @@ public static String appendRequestId(String message, String requestId) {
156158 return message ;
157159 }
158160
159- public static String appendRequestIdToErrorObj (int status , String error , String requestId ) throws SkyflowException {
160- if (requestId != null && !requestId .isEmpty ()) {
161- JsonObject errorObject = (JsonObject ) new JsonParser ().parse (error );
162- JsonObject tempError = (JsonObject ) errorObject .get ("error" );
163- if (tempError != null ) {
164- String message = String .valueOf (tempError .get ("message" ));
165- message = message + " - requestId: " + requestId ;
166-
167- tempError .addProperty ("message" , message );
168- errorObject .add ("error" , tempError );
169- }
170- error = errorObject .toString ();
171- }
172- return error ;
173- }
174-
175161 private static String makeFormEncodeKeyValuePair (String key , String value ) {
176162 return key + "=" + value + "&" ;
177163 }
0 commit comments