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
11 changes: 9 additions & 2 deletions src/Dapr.Actors/Communication/ActorStateResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public class ActorStateResponse<T>
/// </summary>
/// <param name="value">The response value.</param>
/// <param name="ttlExpireTime">The time to live expiration time.</param>
public ActorStateResponse(T value, DateTimeOffset? ttlExpireTime)
/// <param name="httpStatusCode">The HTTP status code provided alongside the state response.</param>
public ActorStateResponse(T value, DateTimeOffset? ttlExpireTime, int httpStatusCode = 200)
{
this.Value = value;
this.TTLExpireTime = ttlExpireTime;
this.HttpStatusCode = httpStatusCode;
}

/// <summary>
Expand All @@ -46,4 +48,9 @@ public ActorStateResponse(T value, DateTimeOffset? ttlExpireTime)
/// The time to live expiration time.
/// </value>
public DateTimeOffset? TTLExpireTime { get; }
}

/// <summary>
/// The HTTP status code provided alongside the state response.
/// </summary>
public int HttpStatusCode { get; }
}
31 changes: 15 additions & 16 deletions src/Dapr.Actors/DaprHttpInteractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Dapr.Actors;
/// <summary>
/// Class to interact with Dapr runtime over http.
/// </summary>
internal class DaprHttpInteractor : IDaprInteractor
internal sealed class DaprHttpInteractor : IDaprInteractor
{
private readonly JsonSerializerOptions jsonSerializerOptions = JsonSerializerDefaults.Web;
private readonly string httpEndpoint;
Expand Down Expand Up @@ -71,7 +71,7 @@ HttpRequestMessage RequestFunc()
}

using var response = await this.SendAsync(RequestFunc, relativeUrl, cancellationToken);
var stringResponse = await response.Content.ReadAsStringAsync();
var stringResponse = await response.Content.ReadAsStringAsync(cancellationToken);

DateTimeOffset? ttlExpireTime = null;
if (response.Headers.TryGetValues(Constants.TTLResponseHeaderName, out IEnumerable<string> headerValues))
Expand All @@ -83,13 +83,15 @@ HttpRequestMessage RequestFunc()
}
}

return new ActorStateResponse<string>(stringResponse, ttlExpireTime);
return new ActorStateResponse<string>(stringResponse, ttlExpireTime, (int)response.StatusCode);
}

public Task SaveStateTransactionallyAsync(string actorType, string actorId, string data, CancellationToken cancellationToken = default)
{
var relativeUrl = string.Format(CultureInfo.InvariantCulture, Constants.ActorStateRelativeUrlFormat, actorType, actorId);

return this.SendAsync(RequestFunc, relativeUrl, cancellationToken);

HttpRequestMessage RequestFunc()
{
var request = new HttpRequestMessage()
Expand All @@ -100,8 +102,6 @@ HttpRequestMessage RequestFunc()

return request;
}

return this.SendAsync(RequestFunc, relativeUrl, cancellationToken);
}

public async Task<IActorResponseMessage> InvokeActorMethodWithRemotingAsync(ActorMessageSerializersManager serializersManager, IActorRequestMessage remotingRequestRequestMessage, CancellationToken cancellationToken = default)
Expand All @@ -116,8 +116,8 @@ public async Task<IActorResponseMessage> InvokeActorMethodWithRemotingAsync(Acto
var serializedHeader = serializersManager.GetHeaderSerializer()
.SerializeRequestHeader(remotingRequestRequestMessage.GetHeader());

var msgBodySeriaizer = serializersManager.GetRequestMessageBodySerializer(interfaceId, methodName);
var serializedMsgBody = msgBodySeriaizer.Serialize(remotingRequestRequestMessage.GetBody());
var msgBodySerializer = serializersManager.GetRequestMessageBodySerializer(interfaceId, methodName);
var serializedMsgBody = msgBodySerializer.Serialize(remotingRequestRequestMessage.GetBody());

// Send Request
var relativeUrl = string.Format(CultureInfo.InvariantCulture, Constants.ActorMethodRelativeUrlFormat, actorType, actorId, methodName);
Expand Down Expand Up @@ -166,7 +166,7 @@ HttpRequestMessage RequestFunc()
IActorResponseMessageBody actorResponseMessageBody = null;
if (retval != null && retval.Content != null)
{
var responseMessageBody = await retval.Content.ReadAsStreamAsync();
var responseMessageBody = await retval.Content.ReadAsStreamAsync(cancellationToken);

// Deserialize Actor Response Message Body
// Deserialize to ActorInvokeException when there is response header otherwise normal path
Expand Down Expand Up @@ -242,7 +242,7 @@ HttpRequestMessage RequestFunc()
}

var response = await this.SendAsync(RequestFunc, relativeUrl, cancellationToken);
var stream = await response.Content.ReadAsStreamAsync();
var stream = await response.Content.ReadAsStreamAsync(cancellationToken);
return stream;
}

Expand Down Expand Up @@ -364,9 +364,9 @@ internal async Task<string> SendAsyncGetResponseAsRawJson(
using var response = await this.SendAsyncHandleUnsuccessfulResponse(requestFunc, relativeUri, cancellationToken);
var retValue = default(string);

if (response != null && response.Content != null)
if (response?.Content != null)
{
retValue = await response.Content.ReadAsStringAsync();
retValue = await response.Content.ReadAsStringAsync(cancellationToken);
}

return retValue;
Expand All @@ -376,7 +376,7 @@ internal async Task<string> SendAsyncGetResponseAsRawJson(
/// Disposes resources.
/// </summary>
/// <param name="disposing">False values indicates the method is being called by the runtime, true value indicates the method is called by the user code.</param>
protected virtual void Dispose(bool disposing)
private void Dispose(bool disposing)
{
if (!this.disposed)
{
Expand Down Expand Up @@ -425,10 +425,10 @@ HttpRequestMessage FinalRequestFunc()

try
{
var contentStream = await response.Content.ReadAsStreamAsync();
var contentStream = await response.Content.ReadAsStreamAsync(cancellationToken);
if (contentStream.Length != 0)
{
error = await JsonSerializer.DeserializeAsync<DaprError>(contentStream, jsonSerializerOptions);
error = await JsonSerializer.DeserializeAsync<DaprError>(contentStream, jsonSerializerOptions, cancellationToken);
}
}
catch (Exception ex)
Expand Down Expand Up @@ -505,7 +505,6 @@ private void AddDaprApiTokenHeader(HttpRequestMessage request)
if (!string.IsNullOrWhiteSpace(this.daprApiToken))
{
request.Headers.Add("dapr-api-token", this.daprApiToken);
return;
}
}
}
}
Loading
Loading