예제 #1
0
        private async Task <T> WebApiRequest <T>(Func <HttpRequestMessage> createRequest, CancellationToken?cancellationToken) where T : class
        {
            while (true)
            {
                try
                {
                    var request = createRequest();
                    if (!string.IsNullOrEmpty(_token)) // Token is cleared by methods that don't require authentication (e.g. OAuthV2Api.Access)
                    {
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _token);
                    }

                    var response = await _http.Execute <WebApiResponse>(request, cancellationToken ?? CancellationToken.None).ConfigureAwait(false)
                                   ?? new WebApiResponse
                    {
                        Ok = true
                    };

                    return(Deserialize <T>(response));
                }
                catch (SlackRateLimitException e) when(!DisableRetryOnRateLimit)
                {
                    await Task.Delay(e.RetryAfter ?? TimeSpan.FromSeconds(1)).ConfigureAwait(false);
                }
            }
        }
예제 #2
0
 private async Task <T> WebApiRequest <T>(Func <HttpRequestMessage> createRequest, CancellationToken?cancellationToken) where T : class
 {
     while (true)
     {
         try
         {
             var response = await _http.Execute <WebApiResponse>(createRequest(), cancellationToken ?? CancellationToken.None).ConfigureAwait(false)
                            ?? new WebApiResponse
             {
                 Ok = true
             };
             return(Deserialize <T>(response));
         }
         catch (SlackRateLimitException e) when(!DisableRetryOnRateLimit)
         {
             await Task.Delay(e.RetryAfter ?? TimeSpan.FromSeconds(1)).ConfigureAwait(false);
         }
     }
 }
예제 #3
0
        /// <summary>
        /// Calls a Slack API method.
        /// </summary>
        /// <typeparam name="T">Type of response expected.</typeparam>
        /// <param name="apiMethod">Name of Slack method.</param>
        /// <param name="args">Arguments to send to Slack. The "token" parameter will be filled in automatically.</param>
        /// <param name="cancellationToken"></param>
        public async Task <T> Get <T>(string apiMethod, Args args, CancellationToken?cancellationToken) where T : class
        {
            var requestMessage = new HttpRequestMessage(HttpMethod.Get, Url(apiMethod, args));

            return(Deserialize <T>(await _http.Execute <WebApiResponse>(requestMessage, cancellationToken ?? CancellationToken.None).ConfigureAwait(false)));
        }