Esempio n. 1
0
        private async Task <Autogenerated.InvokeResponse> MakeInvokeRequestAsync(
            string appId,
            string methodName,
            Any data,
            HTTPExtension httpExtension,
            CancellationToken cancellationToken = default)
        {
            var      protoHTTPExtension = new Autogenerated.HTTPExtension();
            var      contentType        = "";
            Metadata headers            = null;

            if (httpExtension != null)
            {
                protoHTTPExtension.Verb = ConvertHTTPVerb(httpExtension.Verb);

                if (httpExtension.QueryString != null)
                {
                    foreach (var(key, value) in httpExtension.QueryString)
                    {
                        protoHTTPExtension.Querystring.Add(key, value);
                    }
                }

                if (httpExtension.Headers != null)
                {
                    headers = new Metadata();
                    foreach (var(key, value) in httpExtension.Headers)
                    {
                        headers.Add(key, value);
                    }
                }

                contentType = httpExtension.ContentType ?? Constants.ContentTypeApplicationJson;
            }
            else
            {
                protoHTTPExtension.Verb = Autogenerated.HTTPExtension.Types.Verb.Post;
                contentType             = Constants.ContentTypeApplicationJson;
            }

            var invokeRequest = new Autogenerated.InvokeRequest()
            {
                Method        = methodName,
                Data          = data,
                ContentType   = contentType,
                HttpExtension = protoHTTPExtension
            };

            var request = new Autogenerated.InvokeServiceRequest()
            {
                Id      = appId,
                Message = invokeRequest,
            };

            return(await this.MakeGrpcCallHandleError(
                       options => client.InvokeServiceAsync(request, options),
                       headers,
                       cancellationToken));
        }
Esempio n. 2
0
        private (Autogenerated.InvokeServiceRequest, CallOptions) MakeInvokeRequestAsync(
            string appId,
            string methodName,
            Any data,
            HTTPExtension httpExtension,
            CancellationToken cancellationToken = default)
        {
            var protoHTTPExtension = new Autogenerated.HTTPExtension();
            var contentType = "";
            Metadata headers = null;

            if (httpExtension != null)
            {
                protoHTTPExtension.Verb = ConvertHTTPVerb(httpExtension.Verb);

                if (httpExtension.QueryString != null)
                {
                    foreach (var (key, value) in httpExtension.QueryString)
                    {
                        protoHTTPExtension.Querystring.Add(key, value);
                    }
                }

                if (httpExtension.Headers != null)
                {
                    headers = new Metadata();
                    foreach (var (key, value) in httpExtension.Headers)
                    {
                        headers.Add(key, value);
                    }
                }

                contentType = httpExtension.ContentType ?? Constants.ContentTypeApplicationJson;
            }
            else
            {
                protoHTTPExtension.Verb = Autogenerated.HTTPExtension.Types.Verb.Post;
                contentType = Constants.ContentTypeApplicationJson;
            }

            var invokeRequest = new Autogenerated.InvokeRequest()
            {
                Method = methodName,
                Data = data,
                ContentType = contentType,
                HttpExtension = protoHTTPExtension
            };

            var request = new Autogenerated.InvokeServiceRequest()
            {
                Id = appId,
                Message = invokeRequest,
            };

            var callOptions = new CallOptions(headers: headers ?? new Metadata(), cancellationToken: cancellationToken);

            // add token for dapr api token based authentication
            var daprApiToken = Environment.GetEnvironmentVariable("DAPR_API_TOKEN");

            if (daprApiToken != null)
            {
                callOptions.Headers.Add("dapr-api-token", daprApiToken);
            }

            return (request, callOptions);
        }