Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions sdk/src/main/java/io/dapr/client/DaprBodyPublishers.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ private DaprBodyPublishers() {
* <p>Callers are still responsible for setting an appropriate
* {@code Content-Type} header (typically {@code application/json}).
*
* <p>This helper is a convenience for the default-serializer case. It does
* <em>not</em> honor a custom {@link io.dapr.serializer.DaprObjectSerializer}
* configured on the {@link DaprClientBuilder}. Callers with a custom serializer
* should serialize the value themselves and wrap the resulting bytes:
* <pre>{@code
* byte[] bytes = mySerializer.serialize(value);
* BodyPublisher body = HttpRequest.BodyPublishers.ofByteArray(bytes);
* }</pre>
* The only behavior this helper adds over a direct {@code ofByteArray} call is
* choosing a length-known {@link BodyPublisher} so the JDK emits
* {@code Content-Length} rather than {@code Transfer-Encoding: chunked}.
*
* @param value object to serialize; {@code null} yields an empty body.
* @return a body publisher carrying the JSON-encoded bytes.
* @throws UncheckedIOException if serialization fails.
Expand Down
11 changes: 8 additions & 3 deletions sdk/src/main/java/io/dapr/client/DaprInvokeHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package io.dapr.client;

import io.dapr.utils.Version;

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -100,8 +102,9 @@ public URI baseUri() {

/**
* Creates an {@link HttpRequest.Builder} pre-bound to the Dapr invoke URL for the
* configured app id, with the {@code dapr-api-token} header attached (when one is
* configured) and the SDK's HTTP read timeout applied.
* configured app id, with the SDK {@code User-Agent} header attached, the
* {@code dapr-api-token} header attached (when one is configured) and the SDK's
* HTTP read timeout applied.
*
* <p>The {@code relativePath} is resolved against {@link #baseUri()} via
* {@link URI#resolve(String)}. Per {@link URI#resolve(String)} semantics, a leading
Expand All @@ -113,7 +116,9 @@ public URI baseUri() {
*/
public HttpRequest.Builder newRequestBuilder(String relativePath) {
Objects.requireNonNull(relativePath, "relativePath");
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(baseUri.resolve(relativePath));
HttpRequest.Builder builder = HttpRequest.newBuilder()
.uri(baseUri.resolve(relativePath))
.header(Headers.DAPR_USER_AGENT, Version.getSdkVersion());
if (daprApiToken != null && !daprApiToken.isEmpty()) {
builder.header(Headers.DAPR_API_TOKEN, daprApiToken);
}
Expand Down
11 changes: 11 additions & 0 deletions sdk/src/test/java/io/dapr/client/DaprInvokeHttpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

package io.dapr.client;

import io.dapr.utils.Version;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -80,6 +81,16 @@ public void newRequestBuilder_resolvesRelativePathAgainstBaseUri() {
request.uri().toString());
}

@Test
public void newRequestBuilder_attachesSdkUserAgentHeader() {
DaprInvokeHttpClient invoker = new DaprInvokeHttpClient(httpClient, BASE_URI, null, null);

HttpRequest request = invoker.newRequestBuilder("orders").GET().build();

assertEquals(Version.getSdkVersion(),
request.headers().firstValue(Headers.DAPR_USER_AGENT).orElse(null));
}

@Test
public void newRequestBuilder_attachesApiTokenHeaderWhenConfigured() {
DaprInvokeHttpClient invoker = new DaprInvokeHttpClient(httpClient, BASE_URI, "xyz", null);
Expand Down
Loading