Exemple #1
0
        public async Task <CommandResponse> Execute(Message message)
        {
            if (message is null)
            {
                throw new ArgumentNullException(nameof(message));
            }
            try
            {
                _logger.Debug(message.Content);
                var parameters = message.Content.Split(" ").Skip(1);
                if (parameters.Any())
                {
                    string nationName = Helpers.ToID(string.Join(" ", parameters));
                    var    request    = new Request($"nation={nationName}&q=flag+wa+gavote+scvote+fullname+freedom+demonym2plural+category+population+region+founded+foundedtime+influence+lastactivity+census;mode=score;scale=0+1+2+65+66+80", ResponseFormat.Xml);
                    _dispatcher.Dispatch(request, 0);
                    await request.WaitForResponseAsync(token).ConfigureAwait(false);
                    await ProcessResultAsync(request, nationName).ConfigureAwait(false);

                    CommandResponse commandResponse = _responseBuilder.Build();
                    await message.Channel.ReplyToAsync(message, commandResponse).ConfigureAwait(false);

                    return(commandResponse);
                }
                else
                {
                    return(await FailCommandAsync(message, "No parameter passed.").ConfigureAwait(false));
                }
            }
            catch (TaskCanceledException e)
            {
                _logger.Error(e.ToString());
                return(await FailCommandAsync(message, "Request/Command has been canceled. Sorry :(").ConfigureAwait(false));
            }
            catch (HttpRequestFailedException e)
            {
                _logger.Error(e.ToString());
                return(await FailCommandAsync(message, e.Message).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                _logger.Error(e.ToString());
                return(await FailCommandAsync(message, "An unexpected error occured. Please contact the bot administrator.").ConfigureAwait(false));
            }
        }
        public Response Construct(IRequest request)
        {
            _viewDirectory = Environment.GetEnvironmentVariable("ViewDirectory");
            var fullPath = _viewDirectory + FormPath;
            _responseBuilder = new ResponseBuilder(request);
            _responseBuilder.SetStatusCode(200);

            AddContentType();
            AddQuoteForm(fullPath);
            return _responseBuilder.Build();
        }
        public async Task <RestResponse <T> > SendAsync <T>(HttpRequestMessage request)
        {
            try
            {
                using var response = await _client.SendAsync(request).ConfigureAwait(false);

                return(await _builder.Build <T>(response).ConfigureAwait(false));
            }
            catch (Exception e)
            {
                return(_builder.Error <T>(null, "HttpClientException", e.Message));
            }
        }
Exemple #4
0
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var dto = context.ActionArguments.ContainsKey(DtoVariableName) ? context.ActionArguments[DtoVariableName] : null;

            if (dto == null)
            {
                context.Result = new ApiUnprocessableEntityResult(_responseBuilder.AddError(ApiResources.ModelDataMissing).Build());
            }
            else if (!context.ModelState.IsValid)
            {
                _modelStateResolver.GetModelErrors(context.ModelState).ForEach(error => _responseBuilder.AddError(error.Message, error.Field));
                context.Result = new ApiUnprocessableEntityResult(_responseBuilder.Build());
            }
        }
        public void OnActionExecuting(ActionExecutingContext context)
        {
            var queryString = context.ActionArguments["queryString"] as QueryString;

            if (queryString.ValidateQueryString)
            {
                queryString.Errors.ForEach(error => _responseBuilder.AddError(
                                               string.Format(ApiResources.ResourceManager.GetString(error.Type.ToString()), error.ParameterName), error.Info));

                if (_responseBuilder.HasErrors())
                {
                    context.Result = new InvalidQueryStringResult(_responseBuilder.Build());
                }
            }
        }
        protected async Task <CommandResponse> FailCommandAsync(Message message, string reason, Discord.Color color, string title = null)
        {
            _responseBuilder.Clear();
            _responseBuilder.WithFooter(_config.Footer)
            .FailWithDescription(reason)
            .WithColor(color);
            if (title != null)
            {
                _responseBuilder.WithTitle(title);
            }
            var response = _responseBuilder.Build();
            await message.Channel.ReplyToAsync(message, response).ConfigureAwait(false);

            return(response);
        }
 /// <summary>
 /// Builds a <see cref="QueryResponse{T}"/> from the source <see cref="IResponseBuilder"/>.
 /// </summary>
 /// <typeparam name="T">Type of the response results</typeparam>
 /// <param name="builder"><see cref="IResponseBuilder"/></param>
 /// <returns><see cref="QueryResponse{T}"/></returns>
 public static QueryResponse <T> BuildQuery <T>(this IResponseBuilder builder)
 {
     return((QueryResponse <T>)builder.Build());
 }
 /// <summary>
 /// Builds a <see cref="Response{T}"/> from the source <see cref="IResponseBuilder"/>.
 /// </summary>
 /// <typeparam name="T">Type of the response value</typeparam>
 /// <param name="builder"><see cref="IResponseBuilder"/></param>
 /// <returns><see cref="Response{T}"/></returns>
 public static Response <T> Build <T>(this IResponseBuilder builder)
 {
     return((Response <T>)builder.Build());
 }
Exemple #9
0
 public async Task <RestResponse <T> > SendAsync <T>(HttpRequestMessage request)
 {
     using (var response = await _client.SendAsync(request))
         return(await _builder.Build <T>(response));
 }