public async Task Handle(ProcessIncoming command, CancellationToken cancellationToken)
        {
            if (command is null)
            {
                throw new ArgumentNullException(nameof(command));
            }

            _logger.LogInformation("processing request '{MessageId}' ...", command.Id);

            var processedAt = DateTime.UtcNow;

            // just pretend we're doing something...
            await Task.Delay(2000);

            if (_rand.NextDouble() > .5)
            {
                throw new Exception($"whooops!");
            }

            BsonDocument payload = null;

            if (command.Payload is not null)
            {
                var json = System.Text.Json.JsonSerializer.Serialize(command.Payload);
                payload = BsonDocument.Parse(json);
            }

            var model = new ProcessedRequest(command.Id, command.CreatedAt, processedAt, DateTime.UtcNow, payload);
            await _dbContext.ProcessedRequests.InsertOneAsync(model, options : null, cancellationToken);

            _logger.LogInformation("request '{MessageId}' processed!", command.Id);
        }
Пример #2
0
 public static HttpError Process(ProcessedRequest request, IMiddleware[] middleware)
 {
     foreach (var m in middleware)
     {
         m.Process(request);
         if (request.HasErrors())
         {
             return(request.FirstError);
         }
     }
     return(null);
 }
Пример #3
0
        public ProcessedRequest Process(ProcessedRequest request)
        {
            var scheduleToken = AppConfig.GetConfiguration("auth:schedule:token");

            if (
                string.IsNullOrEmpty(scheduleToken) || scheduleToken != request.GetRequestStr("schedule_token")
                )
            {
                request.AddError(new HttpError(HttpStatusCode.Unauthorized, "Schedule token is invalid"));
            }

            return(request);
        }
Пример #4
0
        public ProcessedRequest Process(ProcessedRequest request)
        {
            var userId = Jwt.GetUserIdFromToken((string)request.Request.Query["api_token"] ?? "");

            if (userId == 0)
            {
                request.AddError(
                    new HttpError(HttpStatusCode.Unauthorized, "Invalid api_token")
                    );
            }

            request.UserId = userId;

            return(request);
        }
Пример #5
0
        public BaseController()
        {
            Before += ctx => {
                CurrentRequest = new ProcessedRequest(Request);

                var middlewareError = MiddlewareProcessor.Process(CurrentRequest, Middleware());

                if (middlewareError != null)
                {
                    return(HttpResponse.Error(middlewareError));
                }

                return(null);
            };
        }
        public ProcessedRequest Process(ProcessedRequest request)
        {
            var me = UserRepository.Find(request.UserId);

            var entityGuid = _forcedGuid ?? request.GetRequestStr(_entityGuidParam);

            if (!EntityUtils.IsEntityExists(entityGuid, _entityType))
            {
                request.AddError(new HttpError(HttpStatusCode.NotFound, $"Target {_entityType} doesn't exist"));
            }

            if (!PermissionUtils.HasEntityPermission(me, entityGuid, _entityType))
            {
                request.AddError(
                    new HttpError(HttpStatusCode.Forbidden, "You don't have write permissions for this " + _entityType)
                    );
            }

            return(request);
        }
Пример #7
0
        public static ProcessedRequest ProcessAll(Request request, IMiddleware[] middleware, IValidatorRule[] rules)
        {
            var processed = new ProcessedRequest(request);

            foreach (var mid in middleware)
            {
                mid.Process(processed);
                if (processed.HasErrors())
                {
                    return(processed);
                }
            }

            foreach (var rule in rules)
            {
                var result = rule.Process(processed.Request);
                if (result != null)
                {
                    processed.AddError(result);
                }
            }

            return(processed);
        }
Пример #8
0
 /// <summary>
 ///     Invokes the <see cref="ProcessedRequest" /> event
 /// </summary>
 protected virtual void OnProcessedRequest(IHttpContext context)
 {
     ProcessedRequest?.Invoke(this, new EndpointRequestEventArgs(context));
 }