Esempio n. 1
0
        public async Task <HttpResponseMessage> Request(HttpRequestMessage httpRequestMessage, int retriedTimes = 0)
        {
            httpRequestMessage.Headers.Add("X-User-Agent", $"{appName}/{appVersion} RingCentral.Net/3.2.0");
            httpRequestMessage.Headers.Authorization =
                httpRequestMessage.RequestUri.AbsolutePath.StartsWith("/restapi/oauth/")
                    ? new AuthenticationHeaderValue("Basic",
                                                    Convert.ToBase64String(
                                                        Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}")))
                    : new AuthenticationHeaderValue("Bearer", token.access_token);
            var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);

            AfterHttpCall?.Invoke(this, new HttpCallEventArgs(httpResponseMessage, httpRequestMessage));
            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                if (!_autoRetry || retriedTimes > _maxRetryTimes ||
                    !_retryableHttpStatusCodes.Contains((int)httpResponseMessage.StatusCode))
                {
                    throw new RestException(httpResponseMessage, httpRequestMessage);
                }
                else
                {
                    var delayTime = 1 << retriedTimes * _retryBaseDelay;
                    delayTime = (delayTime / 2) + _random.Next(0, delayTime / 2); // apply jitter
                    await Task.Delay(delayTime);

                    return(await Request(httpRequestMessage, ++retriedTimes));
                }
            }

            return(httpResponseMessage);
        }
Esempio n. 2
0
        public async Task <HttpResponseMessage> Request(HttpRequestMessage httpRequestMessage)
        {
            httpRequestMessage.Headers.Add("X-User-Agent", $"{appName}/{appVersion} RingCentral.Net/3.0.0");
            httpRequestMessage.Headers.Authorization = token == null
                ? new AuthenticationHeaderValue("Basic",
                                                Convert.ToBase64String(
                                                    Encoding.UTF8.GetBytes($"{clientId}:{clientSecret}")))
                : new AuthenticationHeaderValue("Bearer", token.access_token);
            var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage);

            if (!httpResponseMessage.IsSuccessStatusCode)
            {
                throw new RestException(httpResponseMessage, httpRequestMessage);
            }
            AfterHttpCall?.Invoke(this, new HttpCallEventArgs(httpResponseMessage, httpRequestMessage));
            return(httpResponseMessage);
        }
 private void AfterCall(HttpCall httpCall)
 {
     AfterHttpCall?.Invoke(this, new HttpCallEventArgs(httpCall));
 }