Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.0.4] - 2023-02-22

### Changed

- Added contentLength property to RequestInformation to facilitate in setting the content length of the Okhttp3 RequestBody object within the OkhttpRequestAdapter.

## [1.0.3] - 2024-02-21

### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import okio.BufferedSink;
import okio.Okio;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
Expand All @@ -53,6 +54,7 @@
/** RequestAdapter implementation for OkHttp */
public class OkHttpRequestAdapter implements com.microsoft.kiota.RequestAdapter {
private static final String contentTypeHeaderKey = "Content-Type";
private static final String contentLengthHeaderKey = "Content-Length";
@Nonnull private final Call.Factory client;
@Nonnull private final AuthenticationProvider authProvider;
@Nonnull private final ObservabilityOptions obsOptions;
Expand Down Expand Up @@ -188,7 +190,7 @@ public void enableBackingStore(@Nullable final BackingStoreFactory backingStoreF
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Objects.requireNonNull(factory, nullFactoryParameter);

final Span span = startSpan(requestInfo, "sendCollectionAsync");
final Span span = startSpan(requestInfo, "sendCollection");
try (final Scope scope = span.makeCurrent()) {
Response response = this.getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -269,7 +271,7 @@ private Span startSpan(
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Objects.requireNonNull(factory, nullFactoryParameter);

final Span span = startSpan(requestInfo, "sendAsync");
final Span span = startSpan(requestInfo, "send");
try (final Scope scope = span.makeCurrent()) {
Response response = this.getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -331,7 +333,7 @@ private void closeResponse(boolean closeResponse, Response response) {
@Nonnull final Class<ModelType> targetClass) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Objects.requireNonNull(targetClass, "parameter targetClass cannot be null");
final Span span = startSpan(requestInfo, "sendPrimitiveAsync");
final Span span = startSpan(requestInfo, "sendPrimitive");
try (final Scope scope = span.makeCurrent()) {
Response response = this.getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -425,7 +427,7 @@ private void closeResponse(boolean closeResponse, Response response) {
@Nonnull final ValuedEnumParser<ModelType> enumParser) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Objects.requireNonNull(enumParser, nullEnumParserParameter);
final Span span = startSpan(requestInfo, "sendEnumAsync");
final Span span = startSpan(requestInfo, "sendEnum");
try (final Scope scope = span.makeCurrent()) {
Response response = this.getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -471,7 +473,7 @@ private void closeResponse(boolean closeResponse, Response response) {
@Nonnull final ValuedEnumParser<ModelType> enumParser) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
Objects.requireNonNull(enumParser, nullEnumParserParameter);
final Span span = startSpan(requestInfo, "sendEnumCollectionAsync");
final Span span = startSpan(requestInfo, "sendEnumCollection");
try (final Scope scope = span.makeCurrent()) {
Response response = this.getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -518,7 +520,7 @@ private void closeResponse(boolean closeResponse, Response response) {
@Nonnull final Class<ModelType> targetClass) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);

final Span span = startSpan(requestInfo, "sendPrimitiveCollectionAsync");
final Span span = startSpan(requestInfo, "sendPrimitiveCollection");
try (final Scope scope = span.makeCurrent()) {
Response response = getHttpResponseMessage(requestInfo, span, span, null);
final ResponseHandler responseHandler = getResponseHandler(requestInfo);
Expand Down Expand Up @@ -835,7 +837,7 @@ private void setBaseUrlForRequestInformation(@Nonnull final RequestInformation r
@SuppressWarnings("unchecked")
@Nonnull public <T> T convertToNativeRequest(@Nonnull final RequestInformation requestInfo) {
Objects.requireNonNull(requestInfo, nullRequestInfoParameter);
final Span span = startSpan(requestInfo, "convertToNativeRequestAsync");
final Span span = startSpan(requestInfo, "convertToNativeRequest");
try (final Scope scope = span.makeCurrent()) {
this.authProvider.authenticateRequest(requestInfo, null);
return (T) getRequestFromRequestInformation(requestInfo, span, span);
Expand Down Expand Up @@ -885,9 +887,8 @@ private void setBaseUrlForRequestInformation(@Nonnull final RequestInformation r
@Override
public MediaType contentType() {
final Set<String> contentTypes =
requestInfo.headers.containsKey(contentTypeHeaderKey)
? requestInfo.headers.get(contentTypeHeaderKey)
: new HashSet<>();
requestInfo.headers.getOrDefault(
contentTypeHeaderKey, new HashSet<>());
if (contentTypes.isEmpty()) {
return null;
} else {
Expand All @@ -899,6 +900,31 @@ public MediaType contentType() {
}
}

@Override
public long contentLength() {
long length;
final Set<String> contentLength =
requestInfo.headers.getOrDefault(
contentLengthHeaderKey, new HashSet<>());
if (contentLength.isEmpty()) {
try (final ByteArrayInputStream contentStream =
(ByteArrayInputStream) requestInfo.content) {
length = contentStream.available();
} catch (IOException ex) {
length = -1L;
}
Comment thread
ramsessanchez marked this conversation as resolved.
Outdated
} else {
length =
Long.parseLong(
contentLength.toArray(new String[] {})[0]);
}
if (length != -1L) {
spanForAttributes.setAttribute(
SemanticAttributes.HTTP_REQUEST_BODY_SIZE, length);
}
return length;
}

@Override
public void writeTo(@Nonnull BufferedSink sink) throws IOException {
sink.writeAll(Okio.source(requestInfo.content));
Expand Down Expand Up @@ -933,17 +959,7 @@ public void writeTo(@Nonnull BufferedSink sink) throws IOException {
requestBuilder.tag(obsOptions.getType(), obsOptions);
}
requestBuilder.tag(Span.class, parentSpan);
final Request request = requestBuilder.build();
final List<String> contentLengthHeader = request.headers().values("Content-Length");
if (contentLengthHeader != null && !contentLengthHeader.isEmpty()) {
final String firstEntryValue = contentLengthHeader.get(0);
if (firstEntryValue != null && !firstEntryValue.isEmpty()) {
spanForAttributes.setAttribute(
SemanticAttributes.HTTP_REQUEST_BODY_SIZE,
Long.parseLong(firstEntryValue));
}
}
return request;
return requestBuilder.build();
} finally {
span.end();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public UserAgentHandlerOption() {}

private boolean enabled = true;
@Nonnull private String productName = "kiota-java";
@Nonnull private String productVersion = "1.0.0";
@Nonnull private String productVersion = "1.0.4";

/**
* Gets the product name to be used in the user agent header
Expand Down