Exemplo n.º 1
0
        public override HttpRequestMessage CreateInvokeMethodRequest(HttpMethod httpMethod, string appId, string methodName)
        {
            ArgumentVerifier.ThrowIfNull(httpMethod, nameof(httpMethod));
            ArgumentVerifier.ThrowIfNullOrEmpty(appId, nameof(appId));
            ArgumentVerifier.ThrowIfNull(methodName, nameof(methodName));

            // Note about this, it's possible to construct invalid stuff using path navigation operators
            // like `../..`. But the principle of garbage in -> garbage out holds.
            //
            // This approach avoids some common pitfalls that could lead to undesired encoding.
            var path    = $"/v1.0/invoke/{appId}/method/{methodName.TrimStart('/')}";
            var request = new HttpRequestMessage(httpMethod, new Uri(this.httpEndpoint, path))
            {
                Properties =
                {
                    { AppIdKey,      appId      },
                    { MethodNameKey, methodName },
                }
            };

            if (this.apiTokenHeader is not null)
            {
                request.Headers.TryAddWithoutValidation(this.apiTokenHeader.Value.Key, this.apiTokenHeader.Value.Value);
            }

            return(request);
        }
Exemplo n.º 2
0
 /// <inheritdoc/>
 public override Task PublishEventAsync <TData>(string pubsubName, string topicName, TData data, CancellationToken cancellationToken = default)
 {
     ArgumentVerifier.ThrowIfNullOrEmpty(pubsubName, nameof(pubsubName));
     ArgumentVerifier.ThrowIfNullOrEmpty(topicName, nameof(topicName));
     ArgumentVerifier.ThrowIfNull(data, nameof(data));
     return(MakePublishRequest(pubsubName, topicName, data, cancellationToken));
 }
Exemplo n.º 3
0
        public override async Task <HttpResponseMessage> InvokeMethodWithResponseAsync(HttpRequestMessage request, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNull(request, nameof(request));

            if (!this.httpEndpoint.IsBaseOf(request.RequestUri))
            {
                throw new InvalidOperationException("The provided request URI is not a Dapr service invocation URI.");
            }

            // Note: we intentionally DO NOT validate the status code here.
            // This method allows you to 'invoke' without exceptions on non-2xx.
            try
            {
                return(await this.httpClient.SendAsync(request, cancellationToken));
            }
            catch (HttpRequestException ex)
            {
                // Our code path for creating requests places these keys in the request properties. We don't want to fail
                // if they are not present.
                request.Properties.TryGetValue(AppIdKey, out var appId);
                request.Properties.TryGetValue(MethodNameKey, out var methodName);

                throw new InvocationException(
                          appId: appId as string,
                          methodName: methodName as string,
                          innerException: ex,
                          response: null);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Initializes a new <see cref="BindingResponse" />.`
        /// </summary>
        /// <param name="request">The <see cref="BindingRequest" /> assocated with this response.</param>
        /// <param name="data">The response payload.</param>
        /// <param name="metadata">The response metadata.</param>
        public BindingResponse(BindingRequest request, ReadOnlyMemory <byte> data, IReadOnlyDictionary <string, string> metadata)
        {
            ArgumentVerifier.ThrowIfNull(request, nameof(request));
            ArgumentVerifier.ThrowIfNull(data, nameof(data));
            ArgumentVerifier.ThrowIfNull(metadata, nameof(metadata));

            this.Request  = request;
            this.Data     = data;
            this.Metadata = metadata;
        }
Exemplo n.º 5
0
        public override HttpRequestMessage CreateInvokeMethodRequest <TRequest>(HttpMethod httpMethod, string appId, string methodName, TRequest data)
        {
            ArgumentVerifier.ThrowIfNull(httpMethod, nameof(httpMethod));
            ArgumentVerifier.ThrowIfNullOrEmpty(appId, nameof(appId));
            ArgumentVerifier.ThrowIfNull(methodName, nameof(methodName));

            var request = CreateInvokeMethodRequest(httpMethod, appId, methodName);

            request.Content = JsonContent.Create <TRequest>(data, options: this.JsonSerializerOptions);
            return(request);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StateTransactionRequest"/> class.
        /// </summary>
        /// <param name="key">The state key.</param>
        /// <param name="value">The serialized state value.</param>
        /// <param name="operationType">The operation type.</param>
        /// <param name="etag">The etag (optional).</param>
        /// <param name="metadata">Additional key value pairs for the state (optional).</param>
        /// <param name="options">State options (optional).</param>
        public StateTransactionRequest(string key, byte[] value, StateOperationType operationType, string etag = default, IReadOnlyDictionary <string, string> metadata = default, StateOptions options = default)
        {
            ArgumentVerifier.ThrowIfNull(key, nameof(key));

            this.Key           = key;
            this.Value         = value;
            this.OperationType = operationType;
            this.ETag          = etag;
            this.Metadata      = metadata;
            this.Options       = options;
        }
Exemplo n.º 7
0
 public override Task PublishEventAsync(
     string pubsubName,
     string topicName,
     Dictionary <string, string> metadata,
     CancellationToken cancellationToken = default)
 {
     ArgumentVerifier.ThrowIfNullOrEmpty(pubsubName, nameof(pubsubName));
     ArgumentVerifier.ThrowIfNullOrEmpty(topicName, nameof(topicName));
     ArgumentVerifier.ThrowIfNull(metadata, nameof(metadata));
     return(MakePublishRequest(pubsubName, topicName, string.Empty, metadata, cancellationToken));
 }
Exemplo n.º 8
0
        public async override Task <TResponse> InvokeMethodAsync <TResponse>(HttpRequestMessage request, CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNull(request, nameof(request));

            var response = await InvokeMethodWithResponseAsync(request, cancellationToken);

            try
            {
                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException ex)
            {
                // Our code path for creating requests places these keys in the request properties. We don't want to fail
                // if they are not present.
                request.Properties.TryGetValue(AppIdKey, out var appId);
                request.Properties.TryGetValue(MethodNameKey, out var methodName);

                throw new InvocationException(
                          appId: appId as string,
                          methodName: methodName as string,
                          innerException: ex,
                          response: response);
            }

            try
            {
                return(await response.Content.ReadFromJsonAsync <TResponse>(this.jsonSerializerOptions, cancellationToken));
            }
            catch (HttpRequestException ex)
            {
                // Our code path for creating requests places these keys in the request properties. We don't want to fail
                // if they are not present.
                request.Properties.TryGetValue(AppIdKey, out var appId);
                request.Properties.TryGetValue(MethodNameKey, out var methodName);

                throw new InvocationException(
                          appId: appId as string,
                          methodName: methodName as string,
                          innerException: ex,
                          response: response);
            }
            catch (JsonException ex)
            {
                request.Properties.TryGetValue(AppIdKey, out var appId);
                request.Properties.TryGetValue(MethodNameKey, out var methodName);

                throw new InvocationException(
                          appId: appId as string,
                          methodName: methodName as string,
                          innerException: ex,
                          response: response);
            }
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public override Task PublishEventAsync <TData>(
            string pubsubName,
            string topicName,
            TData data,
            CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNullOrEmpty(pubsubName, nameof(pubsubName));
            ArgumentVerifier.ThrowIfNullOrEmpty(topicName, nameof(topicName));
            ArgumentVerifier.ThrowIfNull(data, nameof(data));

            var content = TypeConverters.ToJsonByteString(data, this.JsonSerializerOptions);

            return(MakePublishRequest(pubsubName, topicName, content, null, cancellationToken));
        }
Exemplo n.º 10
0
        public override async Task<InvocationResponse<TResponse>> InvokeMethodWithResponseAsync<TRequest, TResponse>(
            string appId,
            string methodName,
            TRequest data,
            Dapr.Client.Http.HTTPExtension httpExtension = default,
            CancellationToken cancellationToken = default)
        {
            ArgumentVerifier.ThrowIfNull(appId, nameof(appId));
            ArgumentVerifier.ThrowIfNull(methodName, nameof(methodName));

            var request = new InvocationRequest<TRequest>
            {
                AppId = appId,
                MethodName = methodName,
                Body = data,
                HttpExtension = httpExtension,
            };

            var invokeResponse = await this.MakeInvokeRequestAsyncWithResponse<TRequest, TResponse>(request, false, cancellationToken);

            return invokeResponse;
        }
Exemplo n.º 11
0
 /// <inheritdoc/>
 public override Task PublishEventAsync <TContent>(string topicName, TContent content, CancellationToken cancellationToken = default)
 {
     ArgumentVerifier.ThrowIfNullOrEmpty(topicName, nameof(topicName));
     ArgumentVerifier.ThrowIfNull(content, nameof(content));
     return(MakePublishRequest(topicName, content, cancellationToken));
 }