Update to sdk for changes in proto for bidirectional binding in runtime (#320)

* Update proto for bidirectional binding in runtime

* move arg check

Co-authored-by: LM <lemai>
This commit is contained in:
Leon Mai 2020-06-03 11:10:08 -07:00 committed by GitHub
parent f3b5c7db81
commit 041a2d1255
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 86 additions and 18 deletions

View File

@ -36,15 +36,35 @@ namespace Dapr.Client
/// <summary> /// <summary>
/// Invokes an output binding. /// Invokes an output binding.
/// </summary> /// </summary>
/// <typeparam name="TContent"></typeparam> /// <typeparam name="TRequest"></typeparam>
/// <param name="name">The name of the binding to sent the event to.</param> /// <param name="name">The name of the binding to sent the event to.</param>
/// <param name="content">The content of the event to send.</param> /// <param name="operation">The type of operation to perform on the binding.</param>
/// <param name="data">The data of the event to send.</param>
/// <param name="metadata">An open key/value pair that may be consumed by the binding component.</param> /// <param name="metadata">An open key/value pair that may be consumed by the binding component.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param> /// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns> /// <returns>A <see cref="Task" /> that will complete when the operation has completed.</returns>
public abstract Task InvokeBindingAsync<TContent>( public abstract Task InvokeBindingAsync<TRequest>(
string name, string name,
TContent content, string operation,
TRequest data,
Dictionary<string, string> metadata = default,
CancellationToken cancellationToken = default);
/// <summary>
/// Invokes an output binding.
/// </summary>
/// <typeparam name="TRequest">The type of the object for the data to send.</typeparam>
/// <typeparam name="TResponse">The type of the object for the return value.</typeparam>
/// <param name="name">The name of the binding to sent the event to.</param>
/// <param name="operation">The type of operation to perform on the binding.</param>
/// <param name="data">The data of the event to send.</param>
/// <param name="metadata">An open key/value pair that may be consumed by the binding component.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> that can be used to cancel the operation.</param>
/// <returns>A <see cref="ValueTask{T}" /> that will complete when the operation has completed.</returns>
public abstract ValueTask<TResponse> InvokeBindingAsync<TRequest, TResponse>(
string name,
string operation,
TRequest data,
Dictionary<string, string> metadata = default, Dictionary<string, string> metadata = default,
CancellationToken cancellationToken = default); CancellationToken cancellationToken = default);

View File

@ -77,22 +77,58 @@ namespace Dapr.Client
#endregion #endregion
#region InvokeBinding Apis #region InvokeBinding Apis
public override async Task InvokeBindingAsync<TContent>(
/// <inheritdoc/>
public override async Task InvokeBindingAsync<TRequest>(
string name, string name,
TContent content, string operation,
TRequest data,
Dictionary<string, string> metadata = default, Dictionary<string, string> metadata = default,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default)
{ {
ArgumentVerifier.ThrowIfNullOrEmpty(name, nameof(name)); ArgumentVerifier.ThrowIfNullOrEmpty(name, nameof(name));
ArgumentVerifier.ThrowIfNullOrEmpty(operation, nameof(operation));
_ = await MakeInvokeBindingRequestAsync(name, operation, data, metadata, cancellationToken);
}
/// <inheritdoc/>
public override async ValueTask<TResponse> InvokeBindingAsync<TRequest, TResponse>(
string name,
string operation,
TRequest data,
Dictionary<string, string> metadata = default,
CancellationToken cancellationToken = default)
{
ArgumentVerifier.ThrowIfNullOrEmpty(name, nameof(name));
ArgumentVerifier.ThrowIfNullOrEmpty(operation, nameof(operation));
InvokeBindingResponse response = await MakeInvokeBindingRequestAsync(name, operation, data, metadata, cancellationToken);
return ConvertFromInvokeBindingResponse<TResponse>(response, this.jsonSerializerOptions);
}
private static T ConvertFromInvokeBindingResponse<T>(InvokeBindingResponse response, JsonSerializerOptions options = null)
{
var responseData = response.Data.ToStringUtf8();
return JsonSerializer.Deserialize<T>(responseData, options);
}
private async Task<InvokeBindingResponse> MakeInvokeBindingRequestAsync<TContent>(
string name,
string operation,
TContent data,
Dictionary<string, string> metadata = default,
CancellationToken cancellationToken = default)
{
var envelope = new Autogenerated.InvokeBindingRequest() var envelope = new Autogenerated.InvokeBindingRequest()
{ {
Name = name, Name = name,
Operation = operation
}; };
if (content != null) if (data != null)
{ {
envelope.Data = ConvertToByteStringAsync(content, this.jsonSerializerOptions); envelope.Data = ConvertToByteStringAsync(data, this.jsonSerializerOptions);
} }
if (metadata != null) if (metadata != null)
@ -100,7 +136,7 @@ namespace Dapr.Client
envelope.Metadata.Add(metadata); envelope.Metadata.Add(metadata);
} }
await this.MakeGrpcCallHandleError( return await this.MakeGrpcCallHandleError(
(options) => (options) =>
{ {
return client.InvokeBindingAsync(envelope, options); return client.InvokeBindingAsync(envelope, options);

View File

@ -33,7 +33,7 @@ service Dapr {
rpc PublishEvent(PublishEventRequest) returns (google.protobuf.Empty) {} rpc PublishEvent(PublishEventRequest) returns (google.protobuf.Empty) {}
// Invokes binding data to specific output bindings // Invokes binding data to specific output bindings
rpc InvokeBinding(InvokeBindingRequest) returns (google.protobuf.Empty) {} rpc InvokeBinding(InvokeBindingRequest) returns (InvokeBindingResponse) {}
// Gets secrets from secret stores. // Gets secrets from secret stores.
rpc GetSecret(GetSecretRequest) returns (GetSecretResponse) {} rpc GetSecret(GetSecretRequest) returns (GetSecretResponse) {}
@ -121,6 +121,18 @@ message InvokeBindingRequest {
// have a default time to live. The message ttl overrides any value // have a default time to live. The message ttl overrides any value
// in the binding definition. // in the binding definition.
map<string,string> metadata = 3; map<string,string> metadata = 3;
// The name of the operation type for the binding to invoke
string operation = 4;
}
// InvokeBindingResponse is the message returned from an output binding invocation
message InvokeBindingResponse {
// The data which will be sent to output binding.
bytes data = 1;
// The metadata returned from an external system
map<string,string> metadata = 2;
} }
// GetSecretRequest is the message to get secret from secret store. // GetSecretRequest is the message to get secret from secret store.

View File

@ -28,7 +28,7 @@ namespace Dapr.Client.Test
metadata.Add("key1", "value1"); metadata.Add("key1", "value1");
metadata.Add("key2", "value2"); metadata.Add("key2", "value2");
var invokeRequest = new InvokeRequest() { RequestParameter = "Hello " }; var invokeRequest = new InvokeRequest() { RequestParameter = "Hello " };
var task = daprClient.InvokeBindingAsync<InvokeRequest>("test", invokeRequest, metadata); var task = daprClient.InvokeBindingAsync<InvokeRequest>("test", "create", invokeRequest, metadata);
// Get Request and validate // Get Request and validate
httpClient.Requests.TryDequeue(out var entry).Should().BeTrue(); httpClient.Requests.TryDequeue(out var entry).Should().BeTrue();