1212import com .google .gson .JsonElement ;
1313import com .google .gson .JsonObject ;
1414import com .google .gson .JsonParser ;
15+ import com .microsoft .graph .logger .DefaultLogger ;
16+ import com .microsoft .graph .logger .ILogger ;
1517import com .google .gson .JsonParseException ;
1618
1719import okhttp3 .MediaType ;
@@ -32,11 +34,23 @@ public class MSBatchResponseContent {
3234 private LinkedHashMap <String , Request > batchRequestsHashMap = new LinkedHashMap <>();
3335 private JsonArray batchResponseArray ;
3436 private String nextLink ;
37+ private final ILogger logger ;
3538
3639 /**
3740 * @param batchResponse OkHttp batch response on execution of batch requests
3841 */
3942 public MSBatchResponseContent (@ Nullable final Response batchResponse ) {
43+ this (new DefaultLogger (), batchResponse );
44+ }
45+
46+ /**
47+ * @param batchResponse OkHttp batch response on execution of batch requests
48+ * @param logger logger to use for telemetry
49+ */
50+ public MSBatchResponseContent (@ Nonnull final ILogger logger , @ Nullable final Response batchResponse ) {
51+ if (logger == null )
52+ throw new IllegalArgumentException ("logger parameter cannot be null" );
53+ this .logger = logger ;
4054 update (batchResponse );
4155 this .message = batchResponse .message ();
4256 this .protocol = batchResponse .protocol ();
@@ -47,8 +61,10 @@ public MSBatchResponseContent(@Nullable final Response batchResponse) {
4761 * @param baseUrl the base service URL without a trailing slash
4862 * @param batchRequestData the batch request payload data as a JSON string
4963 * @param batchResponseData the batch response body as a JSON string
64+ * @param logger logger to use for telemetry
5065 */
51- protected MSBatchResponseContent (@ Nonnull final String baseUrl , @ Nonnull final JsonObject batchRequestData , @ Nonnull final JsonObject batchResponseData ) {
66+ protected MSBatchResponseContent (@ Nonnull final ILogger logger , @ Nonnull final String baseUrl , @ Nonnull final JsonObject batchRequestData , @ Nonnull final JsonObject batchResponseData ) {
67+ this .logger = logger ;
5268 this .protocol = Protocol .HTTP_1_1 ;
5369 this .message = "OK" ;
5470 final Map <String , Request > requestMap = createBatchRequestsHashMap (baseUrl , batchRequestData );
@@ -171,7 +187,7 @@ public void update(@Nonnull final Response batchResponse) {
171187 updateFromResponseBody (stringToJSONObject (batchResponseData ));
172188 }
173189 } catch (final IOException e ) {
174- e . printStackTrace ( );
190+ logger . logError ( "error updating batch response content from response body" , e );
175191 }
176192 }
177193 }
@@ -208,7 +224,7 @@ private Map<String, Request> createBatchRequestsHashMap(final Response batchResp
208224 final String baseUrl = batchResponse .request ().url ().toString ().replace ("$batch" , "" );
209225 return createBatchRequestsHashMap (baseUrl , requestJSONObject );
210226 } catch (IOException ex ) {
211- ex . printStackTrace ( );
227+ logger . logError ( "error parsing batch requests to get batch response content" , ex );
212228 return null ;
213229 }
214230 }
@@ -268,7 +284,7 @@ private Map<String, Request> createBatchRequestsHashMap(@Nonnull final String ba
268284 return batchRequestsHashMap ;
269285
270286 } catch (JsonParseException e ) {
271- e . printStackTrace ( );
287+ logger . logError ( "error parsing batch request" , e );
272288 }
273289 return null ;
274290 }
@@ -280,7 +296,7 @@ private JsonObject stringToJSONObject(final String input) {
280296 JsonObject = JsonParser .parseString (input ).getAsJsonObject ();
281297 }
282298 } catch (final Exception e ) {
283- e . printStackTrace ( );
299+ logger . logError ( "error parsing input into JSONObject" , e );
284300 }
285301 return JsonObject ;
286302 }
0 commit comments