示例#1
0
        protected IMessage DoRequest(string function, IMessage request, RequestOptions?reqOpts = null)
        {
            var options = BuildRequestOptionsWithDefaults(reqOpts);
            var maxTime = options.RetryOptions?.MaxCallDuration;
            int retries = 0;

            while (true)
            {
                try
                {
                    return(CallMethodOnClient(function, request, options));
                }
                catch (RpcException e)
                {
                    if (_auth != null && e.Status.StatusCode == StatusCode.Unauthenticated)
                    {
                        _auth.InvalidateToken();
                    }
                    var  time           = options.RetryOptions.Pause();
                    bool retryCondition = (e.Status.StatusCode == StatusCode.Unauthenticated && retries < 1) || options.RetryOptions.ShouldRetry(GrpcToHttpCode(e.Status.StatusCode));
                    if (options.RetryOptions != null && retryCondition && IsRetryWithinMaxCallDuration(time, maxTime))
                    {
                        System.Threading.Thread.Sleep(Convert.ToInt32(time));
                        retries++;
                    }
                    else
                    {
                        throw new SdkException(e.Message, e);
                    }
                }
            }
        }
        /// <summary>
        /// Creates a new <see cref="HttpMessage"/> instance.
        /// </summary>
        /// <param name="pipeline"></param>
        /// <param name="options">The message options.</param>
        /// <returns>The message.</returns>
        public static HttpMessage CreateMessage(this HttpPipeline pipeline, RequestOptions?options)
        {
            // TODO: This method will be added as a method on HttpPipeline directly
            // when RequestOptions moves to core. At that time, we expect RequestContext
            // to inherit from RequestOptions, so copying RequestOptions to
            // RequestContext can be removed.

            if (options == null)
            {
                return(pipeline.CreateMessage());
            }

            RequestContext context = new RequestContext();

            context.ErrorOptions = options.ErrorOptions;

            if (options.Policies != null)
            {
                foreach (var policy in options.Policies)
                {
                    context.AddPolicy(policy.Policy, policy.Position);
                }
            }

            return(pipeline.CreateMessage(context));
        }
        public ValueTask <GitLabPageResponse <T> > GetNextPageAsync(RequestOptions?options, CancellationToken cancellationToken = default)
        {
            if (NextPageUrl == null)
            {
                return(new ValueTask <GitLabPageResponse <T> >());
            }

            return(new ValueTask <GitLabPageResponse <T> >(GitLabClient.GetPagedCollectionAsync <T>(NextPageUrl, options, cancellationToken)));
        }
        /// <inheritdoc />
        public virtual async Task MakeRequest(
            Func <TApiService, CancellationToken, Task> operation,
            RequestOptions?options = null)
        {
            options ??= RequestOptions.GetDefaultOptions();

            await CreatePolicy(options)
            .ExecuteAsync(ct => operation(_apiService, ct), options.CancellationToken)
            .ConfigureAwait(false);
        }
示例#5
0
        /// <summary>
        ///     Gets the current weather information for the city specified by <paramref
        ///     name="cityId"/> asynchronously.
        /// </summary>
        /// <param name="cityId">
        ///     the id of the city to get the weather information for ( <see cref="Weather.Id"/>)
        /// </param>
        /// <param name="requestOptions">
        ///     the request options (can change the behavior of requesting and controls caching,
        ///     cancellation, unit and language options of the result)
        /// </param>
        /// <returns>
        ///     a task that represents the asynchronous operation. The task result is the weather
        ///     information; or <see langword="null"/> if the city could not be found.
        /// </returns>
        public Task <Weather?> GetCurrentWeatherAsync(int cityId, RequestOptions?requestOptions = default)
        {
            requestOptions ??= RequestOptions.Default;
            requestOptions.CancellationToken.ThrowIfCancellationRequested();

            // send request
            return(RequestAsync <Weather>("weather", new NameValueCollection {
                { "id", cityId.ToString() }
            }, requestOptions));
        }
        /// <inheritdoc />
        public virtual async Task <TResult> MakeRequest <TResult>(
            Func <TApiService, CancellationToken, Task <TResult> > operation,
            RequestOptions?options = null)
        {
            options ??= RequestOptions.GetDefaultOptions();

            return(await CreatePolicy(options)
                   .AsAsyncPolicy <TResult>()
                   .ExecuteAsync(ct => operation(_apiService, ct), options.CancellationToken)
                   .ConfigureAwait(false));
        }
示例#7
0
        /// <summary>
        ///     Gets the current weather information for the specified <paramref name="cityIds"/> asynchronously.
        /// </summary>
        /// <param name="cityIds">the ids of the cities to get the weather for</param>
        /// <param name="requestOptions">
        ///     the request options (can change the behavior of requesting and controls caching,
        ///     cancellation, unit and language options of the result)
        /// </param>
        /// <returns>
        ///     a task that represents the asynchronous operation. The task result is the weather
        ///     information; or <see langword="null"/> if the city could not be found.
        /// </returns>
        public Task <WeatherList?> GetCurrentWeatherAsync(IEnumerable <int> cityIds, RequestOptions?requestOptions = default)
        {
            requestOptions ??= RequestOptions.Default;
            requestOptions.CancellationToken.ThrowIfCancellationRequested();

            var parameters = new NameValueCollection {
                { "id", string.Join(",", cityIds) }
            };

            // send request
            return(RequestAsync <WeatherList>("group", parameters, requestOptions));
        }
示例#8
0
        protected RequestOptions BuildRequestOptionsWithDefaults(RequestOptions?reqOpts)
        {
            if (reqOpts == null)
            {
                return(_defaultRequestOptions);
            }

            return(new RequestOptions(
                       reqOpts.Value.Timeout ?? _defaultRequestOptions.Timeout,
                       reqOpts.Value.IncludeToken ?? _defaultRequestOptions.IncludeToken,
                       reqOpts.Value.RetryOptions ?? _defaultRequestOptions.RetryOptions
                       ));
        }
 public Task <IApplicationCommand> GetApplicationCommandAsync(ulong id, CacheMode mode = CacheMode.AllowDownload, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).GetApplicationCommandAsync(id, mode, options));
 }
示例#10
0
        /// <summary>
        /// Responds to the interaction message and returns the original message
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="text"></param>
        /// <param name="embeds"></param>
        /// <param name="isTTS"></param>
        /// <param name="ephemeral"></param>
        /// <param name="allowedMentions"></param>
        /// <param name="components"></param>
        /// <param name="embed"></param>
        /// <param name="options"></param>
        /// <returns>The original interaction message</returns>
        public static async Task <RestInteractionMessage> Respond(this SocketSlashCommand cmd, string?text = null, Embed[]?embeds = null, bool isTTS = false, bool ephemeral = false, AllowedMentions?allowedMentions = null, MessageComponent?components = null, Embed?embed = null, RequestOptions?options = null)
        {
            await cmd.RespondAsync(text, embeds, isTTS, ephemeral, allowedMentions, components, embed, options);

            return(await cmd.GetOriginalResponseAsync());
        }
 public Task DeleteIntegrationAsync(ulong id, RequestOptions?options = null)
 {
     return(DeleteIntegrationAsync(id, options));
 }
 public virtual Task AddRoleAsync(IRole role, RequestOptions?options = null)
 {
     return(_socketGuildUser.AddRoleAsync(role, options));
 }
 public Task DeleteStickerAsync(ICustomSticker sticker, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).DeleteStickerAsync(sticker, options));
 }
 public virtual Task RemoveRoleAsync(ulong roleId, RequestOptions?options = null)
 {
     return(_socketGuildUser.RemoveRoleAsync(roleId, options));
 }
 public Task RemoveTimeOutAsync(RequestOptions?options = null)
 {
     return(_socketGuildUser.RemoveTimeOutAsync(options));
 }
 public virtual Task KickAsync(string?reason = null, RequestOptions?options = null)
 {
     return(_socketGuildUser.KickAsync(reason, options));
 }
 public Task <IReadOnlyCollection <IApplicationCommand> > GetApplicationCommandsAsync(RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).GetApplicationCommandsAsync(options));
 }
 public Task <IGuildScheduledEvent> CreateEventAsync(string name, DateTimeOffset startTime, GuildScheduledEventType type, GuildScheduledEventPrivacyLevel privacyLevel = GuildScheduledEventPrivacyLevel.Private, string?description = null, DateTimeOffset?endTime = null, ulong?channelId = null, string?location = null, Image?coverImage = null, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).CreateEventAsync(name, startTime, type, privacyLevel, description, endTime, channelId, location, coverImage, options));
 }
 public Task <IReadOnlyCollection <IGuildScheduledEvent> > GetEventsAsync(RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).GetEventsAsync(options));
 }
 public Task <IGuildScheduledEvent> GetEventAsync(ulong id, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).GetEventAsync(id, options));
 }
 public virtual Task ModifyAsync(Action <GuildUserProperties> func, RequestOptions?options = null)
 {
     return(_socketGuildUser.ModifyAsync(func, options));
 }
 public Task <IApplicationCommand> CreateApplicationCommandAsync(ApplicationCommandProperties properties, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).CreateApplicationCommandAsync(properties, options));
 }
 public virtual Task RemoveRolesAsync(IEnumerable <ulong> roleIds, RequestOptions?options = null)
 {
     return(_socketGuildUser.RemoveRolesAsync(roleIds, options));
 }
 public Task <IReadOnlyCollection <IApplicationCommand> > BulkOverwriteApplicationCommandsAsync(ApplicationCommandProperties[] properties, RequestOptions?options = null)
 {
     return(((IGuild)_socketGuild).BulkOverwriteApplicationCommandsAsync(properties, options));
 }
 public Task SetTimeOutAsync(TimeSpan span, RequestOptions?options = null)
 {
     return(_socketGuildUser.SetTimeOutAsync(span, options));
 }
 public IAsyncEnumerable <IReadOnlyCollection <IBan> > GetBansAsync(ulong fromUserId, Direction dir, int limit = 1000, RequestOptions?options = null)
 {
     return(_socketGuild.GetBansAsync(fromUserId, dir, limit, options));
 }
 public virtual Task AddRolesAsync(IEnumerable <IRole> roles, RequestOptions?options = null)
 {
     return(_socketGuildUser.AddRolesAsync(roles, options));
 }
 public IAsyncEnumerable <IReadOnlyCollection <IBan> > GetBansAsync(IUser fromUser, Direction dir, int limit = 1000, RequestOptions?options = null)
 {
     return(GetBansAsync(fromUser, dir, limit, options));
 }
示例#29
0
        public static async Task <IUserMessage> SendErrorMessage(this IUser user, string?message   = null, bool isTTS = false, Embed?embed = null, RequestOptions?options = null, AllowedMentions?allowedMentions = null,
                                                                 MessageReference?messageReference = null, MessageComponent?components = null, ISticker[]?stickers = null, Embed[]?embeds = null)
        {
            var dmChannel = await user.CreateDMChannelAsync();

            if (dmChannel is IErrorChannel errorChannel)
            {
                return(await errorChannel.SendErrorMessageAsync(message ?? "?"));
            }
            else
            {
                return(await dmChannel.SendMessageAsync(message, isTTS, embed, options, allowedMentions, messageReference, components, stickers, embeds));
            }
        }
 public Task <IReadOnlyCollection <IIntegration> > GetIntegrationsAsync(RequestOptions?options = null)
 {
     return(GetIntegrationsAsync(options));
 }