Exemple #1
0
        private static HttpRequestMessage BuildRequest(RestApiEndpoint endpoint, HttpMethod httpMethod, string methodName = null, object[] args = null)
        {
            var payload = httpMethod == HttpMethod.Post ? new PayloadMessage {
                Target = methodName, Arguments = args
            } : null;

            return(GenerateHttpRequest(endpoint.Audience, payload, endpoint.Token, httpMethod));
        }
Exemple #2
0
        private HttpRequestMessage BuildRequest(RestApiEndpoint api, HttpMethod httpMethod, string productInfo, string methodName = null, object[] args = null)
        {
            var payload = httpMethod == HttpMethod.Post ? new PayloadMessage {
                Target = methodName, Arguments = args
            } : null;

            return(GenerateHttpRequest(api.Audience, api.Query, httpMethod, payload, api.Token, productInfo));
        }
        private Task CallRestApi(RestApiEndpoint endpoint, HttpMethod httpMethod, string methodName = null, object[] args = null, CancellationToken cancellationToken = default)
        {
            var payload = httpMethod == HttpMethod.Post ? new PayloadMessage {
                Target = methodName, Arguments = args
            } : null;
            var httpClient = HttpClientFactory.CreateClient();
            var request    = GenerateHttpRequest(endpoint.Audience, payload, endpoint.Token, HttpMethod.Post);

            return(httpClient.SendAsync(request));
        }
Exemple #4
0
        public Task SendAsync(
            RestApiEndpoint api,
            HttpMethod httpMethod,
            string productInfo,
            string methodName = null,
            object[] args     = null,
            Func <HttpResponseMessage, bool> handleExpectedResponse = null,
            CancellationToken cancellationToken = default)
        {
            if (handleExpectedResponse == null)
            {
                return(SendAsync(api, httpMethod, productInfo, methodName, args, handleExpectedResponseAsync: null, cancellationToken));
            }

            return(SendAsync(api, httpMethod, productInfo, methodName, args, response => Task.FromResult(handleExpectedResponse(response)), cancellationToken));
        }
Exemple #5
0
        public async Task SendAsync(
            RestApiEndpoint api,
            HttpMethod httpMethod,
            string productInfo,
            string methodName = null,
            object[] args     = null,
            Func <HttpResponseMessage, Task <bool> > handleExpectedResponseAsync = null,
            CancellationToken cancellationToken = default)
        {
            var httpClient = HttpClientFactory.CreateClient();
            var request    = BuildRequest(api, httpMethod, productInfo, methodName, args);
            HttpResponseMessage response = null;

            try
            {
                response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
            }
            catch (HttpRequestException ex)
            {
                throw new AzureSignalRInaccessibleEndpointException(request.RequestUri.ToString(), ex);
            }

            if (handleExpectedResponseAsync == null)
            {
                await ThrowExceptionOnResponseFailureAsync(response);
            }
            else
            {
                if (!await handleExpectedResponseAsync(response))
                {
                    await ThrowExceptionOnResponseFailureAsync(response);
                }
            }

            response.Dispose();
        }