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 void AddTracingId(RestApiEndpoint api) { var id = MessageWithTracingIdHelper.Generate(); if (api.Query == null) { api.Query = new Dictionary<string, StringValues>(); } api.Query.Add(Constants.Headers.AsrsMessageTracingId, id.ToString()); }
private async Task AuthorizeWithTokenAsync(string accessToken, CancellationToken token = default) { var api = new RestApiEndpoint(AuthorizeUrl, accessToken); await new RestClient().SendAsync( api, HttpMethod.Get, "", handleExpectedResponseAsync: HandleHttpResponseAsync, cancellationToken: token); }
private async Task AuthorizeWithTokenAsync(string endpoint, int?port, string accessToken, CancellationToken token = default) { if (port != null && port != 443) { endpoint += $":{port}"; } var api = new RestApiEndpoint(endpoint + "/api/v1/auth/accessKey", accessToken); await new RestClient().SendAsync( api, HttpMethod.Get, "", handleExpectedResponseAsync: HandleHttpResponseAsync, cancellationToken: token); }
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)); }
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) { using var httpClient = _httpClientFactory.CreateClient(); using var request = BuildRequest(api, httpMethod, productInfo, methodName, args); HttpResponseMessage response = null; try { response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken); if (handleExpectedResponseAsync == null) { await ThrowExceptionOnResponseFailureAsync(response); } else { if (!await handleExpectedResponseAsync(response)) { await ThrowExceptionOnResponseFailureAsync(response); } } } catch (HttpRequestException ex) { throw new AzureSignalRInaccessibleEndpointException(request.RequestUri.ToString(), ex); } finally { response?.Dispose(); } }