2929import okio .BufferedSink ;
3030import okio .Okio ;
3131
32+ import java .io .ByteArrayInputStream ;
3233import java .io .IOException ;
3334import java .io .InputStream ;
3435import java .math .BigDecimal ;
5354/** RequestAdapter implementation for OkHttp */
5455public class OkHttpRequestAdapter implements com .microsoft .kiota .RequestAdapter {
5556 private static final String contentTypeHeaderKey = "Content-Type" ;
57+ private static final String contentLengthHeaderKey = "Content-Length" ;
5658 @ Nonnull private final Call .Factory client ;
5759 @ Nonnull private final AuthenticationProvider authProvider ;
5860 @ Nonnull private final ObservabilityOptions obsOptions ;
@@ -188,7 +190,7 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF
188190 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
189191 Objects .requireNonNull (factory , nullFactoryParameter );
190192
191- final Span span = startSpan (requestInfo , "sendCollectionAsync " );
193+ final Span span = startSpan (requestInfo , "sendCollection " );
192194 try (final Scope scope = span .makeCurrent ()) {
193195 Response response = this .getHttpResponseMessage (requestInfo , span , span , null );
194196 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -269,7 +271,7 @@ private Span startSpan(
269271 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
270272 Objects .requireNonNull (factory , nullFactoryParameter );
271273
272- final Span span = startSpan (requestInfo , "sendAsync " );
274+ final Span span = startSpan (requestInfo , "send " );
273275 try (final Scope scope = span .makeCurrent ()) {
274276 Response response = this .getHttpResponseMessage (requestInfo , span , span , null );
275277 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -331,7 +333,7 @@ private void closeResponse(boolean closeResponse, Response response) {
331333 @ Nonnull final Class <ModelType > targetClass ) {
332334 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
333335 Objects .requireNonNull (targetClass , "parameter targetClass cannot be null" );
334- final Span span = startSpan (requestInfo , "sendPrimitiveAsync " );
336+ final Span span = startSpan (requestInfo , "sendPrimitive " );
335337 try (final Scope scope = span .makeCurrent ()) {
336338 Response response = this .getHttpResponseMessage (requestInfo , span , span , null );
337339 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -425,7 +427,7 @@ private void closeResponse(boolean closeResponse, Response response) {
425427 @ Nonnull final ValuedEnumParser <ModelType > enumParser ) {
426428 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
427429 Objects .requireNonNull (enumParser , nullEnumParserParameter );
428- final Span span = startSpan (requestInfo , "sendEnumAsync " );
430+ final Span span = startSpan (requestInfo , "sendEnum " );
429431 try (final Scope scope = span .makeCurrent ()) {
430432 Response response = this .getHttpResponseMessage (requestInfo , span , span , null );
431433 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -471,7 +473,7 @@ private void closeResponse(boolean closeResponse, Response response) {
471473 @ Nonnull final ValuedEnumParser <ModelType > enumParser ) {
472474 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
473475 Objects .requireNonNull (enumParser , nullEnumParserParameter );
474- final Span span = startSpan (requestInfo , "sendEnumCollectionAsync " );
476+ final Span span = startSpan (requestInfo , "sendEnumCollection " );
475477 try (final Scope scope = span .makeCurrent ()) {
476478 Response response = this .getHttpResponseMessage (requestInfo , span , span , null );
477479 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -518,7 +520,7 @@ private void closeResponse(boolean closeResponse, Response response) {
518520 @ Nonnull final Class <ModelType > targetClass ) {
519521 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
520522
521- final Span span = startSpan (requestInfo , "sendPrimitiveCollectionAsync " );
523+ final Span span = startSpan (requestInfo , "sendPrimitiveCollection " );
522524 try (final Scope scope = span .makeCurrent ()) {
523525 Response response = getHttpResponseMessage (requestInfo , span , span , null );
524526 final ResponseHandler responseHandler = getResponseHandler (requestInfo );
@@ -835,7 +837,7 @@ private void setBaseUrlForRequestInformation(@Nonnull final RequestInformation r
835837 @ SuppressWarnings ("unchecked" )
836838 @ Nonnull public <T > T convertToNativeRequest (@ Nonnull final RequestInformation requestInfo ) {
837839 Objects .requireNonNull (requestInfo , nullRequestInfoParameter );
838- final Span span = startSpan (requestInfo , "convertToNativeRequestAsync " );
840+ final Span span = startSpan (requestInfo , "convertToNativeRequest " );
839841 try (final Scope scope = span .makeCurrent ()) {
840842 this .authProvider .authenticateRequest (requestInfo , null );
841843 return (T ) getRequestFromRequestInformation (requestInfo , span , span );
@@ -885,9 +887,8 @@ private void setBaseUrlForRequestInformation(@Nonnull final RequestInformation r
885887 @ Override
886888 public MediaType contentType () {
887889 final Set <String > contentTypes =
888- requestInfo .headers .containsKey (contentTypeHeaderKey )
889- ? requestInfo .headers .get (contentTypeHeaderKey )
890- : new HashSet <>();
890+ requestInfo .headers .getOrDefault (
891+ contentTypeHeaderKey , new HashSet <>());
891892 if (contentTypes .isEmpty ()) {
892893 return null ;
893894 } else {
@@ -899,6 +900,30 @@ public MediaType contentType() {
899900 }
900901 }
901902
903+ @ Override
904+ public long contentLength () {
905+ long length ;
906+ final Set <String > contentLength =
907+ requestInfo .headers .getOrDefault (
908+ contentLengthHeaderKey , new HashSet <>());
909+ if (contentLength .isEmpty ()
910+ && requestInfo .content
911+ instanceof ByteArrayInputStream ) {
912+ final ByteArrayInputStream contentStream =
913+ (ByteArrayInputStream ) requestInfo .content ;
914+ length = contentStream .available ();
915+ } else {
916+ length =
917+ Long .parseLong (
918+ contentLength .toArray (new String [] {})[0 ]);
919+ }
920+ if (length > 0 ) {
921+ spanForAttributes .setAttribute (
922+ SemanticAttributes .HTTP_REQUEST_BODY_SIZE , length );
923+ }
924+ return length ;
925+ }
926+
902927 @ Override
903928 public void writeTo (@ Nonnull BufferedSink sink ) throws IOException {
904929 sink .writeAll (Okio .source (requestInfo .content ));
@@ -933,17 +958,7 @@ public void writeTo(@Nonnull BufferedSink sink) throws IOException {
933958 requestBuilder .tag (obsOptions .getType (), obsOptions );
934959 }
935960 requestBuilder .tag (Span .class , parentSpan );
936- final Request request = requestBuilder .build ();
937- final List <String > contentLengthHeader = request .headers ().values ("Content-Length" );
938- if (contentLengthHeader != null && !contentLengthHeader .isEmpty ()) {
939- final String firstEntryValue = contentLengthHeader .get (0 );
940- if (firstEntryValue != null && !firstEntryValue .isEmpty ()) {
941- spanForAttributes .setAttribute (
942- SemanticAttributes .HTTP_REQUEST_BODY_SIZE ,
943- Long .parseLong (firstEntryValue ));
944- }
945- }
946- return request ;
961+ return requestBuilder .build ();
947962 } finally {
948963 span .end ();
949964 }
0 commit comments