예제 #1
0
        public async Task <TResponse> SendAsync <TResponse, TResponseData>(GraphQlRequestMessage request) where TResponse : GraphQlResponseMessage <TResponseData>
        {
            var currentAttempt                = 0;
            HttpResponseMessage response      = null;
            Exception           lastException = null;

            do
            {
                response = await _httpClient.SendAsync(new GraphQlRequestMessage(request));

                var rawResponseContent = await response.Content.ReadAsStringAsync();

                TResponse parsedResponse = null;
                if (response.IsSuccessStatusCode && (parsedResponse = JsonConvert.DeserializeObject <TResponse>(rawResponseContent)).IsValid())
                {
                    return(parsedResponse);
                }
                else
                {
                    OnGraphQlError?.Invoke(this, new GraphQlEventArgs <TResponse, TResponseData>
                    {
                        Request  = request,
                        Response = parsedResponse
                    });

                    currentAttempt++;
                    lastException = new HttpRequestException($"Request did not return successful status code (StatusCode: {response.StatusCode}, Content: {rawResponseContent})");
                }
            } while (ShouldRetry(request, response));

            throw new Exception($"Query failed after {currentAttempt} attempt(s) (see inner exception for details)", lastException);
        }
예제 #2
0
 protected virtual bool ShouldRetry(GraphQlRequestMessage request, HttpResponseMessage response)
 {
     return(false);
 }
예제 #3
0
 public async Task <T> SendAsync <T>(GraphQlRequestMessage request)
 {
     return((await SendAsync <GraphQlResponseMessage <T>, T>(request)).Data);
 }