Task<IResourceDataResult> IAsynchronousFilterChain.FilterAsync(IResourceDataRequest request, ILogger logger, CancellationToken cancellationToken)
        {
            bool hasFilters = !this.filters.IsNullOrEmpty();
            if (!hasFilters)
            {
                throw new ApplicationException("Empty filter chain");
            }

            if (this.filters.Count == 1)
            {
                return this.filters.Single().FilterAsync(
                    request,
                    chain: null,
                    logger: logger,
                    cancellationToken: cancellationToken);
            }

            var remainingChain = new DefaultAsynchronousFilterChain(this.dataStore, this.filters.Skip(1));
            return this.filters.First().FilterAsync(request, remainingChain, logger, cancellationToken);
        }
        Task <IResourceDataResult> IAsynchronousFilterChain.FilterAsync(IResourceDataRequest request, ILogger logger, CancellationToken cancellationToken)
        {
            bool hasFilters = !this.filters.IsNullOrEmpty();

            if (!hasFilters)
            {
                throw new ApplicationException("Empty filter chain");
            }

            if (this.filters.Count == 1)
            {
                return(this.filters.Single().FilterAsync(
                           request,
                           chain: null,
                           logger: logger,
                           cancellationToken: cancellationToken));
            }

            var remainingChain = new DefaultAsynchronousFilterChain(this.dataStore, this.filters.Skip(1));

            return(this.filters.First().FilterAsync(request, remainingChain, logger, cancellationToken));
        }
            public async Task Async_with_inline_filter()
            {
                IAsynchronousFilterChain defaultFilterChain = new DefaultAsynchronousFilterChain(dataStore: null)
                    .Add(new DeleteInterceptorFilter());

                IAsynchronousFilterChain finalChain = new DefaultAsynchronousFilterChain(defaultFilterChain as DefaultAsynchronousFilterChain)
                    .Add(new DefaultAsynchronousFilter((req, next, logger, ct) =>
                    {
                        return Task.FromResult<IResourceDataResult>(new DefaultResourceDataResult(
                            ResourceAction.Create,
                            typeof(IDictionary<string, object>),
                            req.Uri,
                            httpStatus: 200,
                            body: new Dictionary<string, object>() { { "Foo", "bar" } }));
                    }));

                var request = new DefaultResourceDataRequest(ResourceAction.Create, typeof(IAccount), new CanonicalUri("http://api.foo.bar"), false);
                var result = await finalChain.FilterAsync(request, Substitute.For<ILogger>(), CancellationToken.None);

                result.Action.ShouldBe(ResourceAction.Create);
                result.Body.ShouldContainKeyAndValue("Foo", "bar");
            }
            public async Task Async_chain_terminating_on_second()
            {
                IAsynchronousFilterChain filterChain = new DefaultAsynchronousFilterChain(dataStore: null)
                    .Add(new CreateInterceptorFilter())
                    .Add(new DeleteInterceptorFilter());

                var request = new DefaultResourceDataRequest(ResourceAction.Delete, typeof(IAccount), new CanonicalUri("http://api.foo.bar"), false);
                var result = await filterChain.FilterAsync(request, Substitute.For<ILogger>(), CancellationToken.None);

                result.Action.ShouldBe(ResourceAction.Delete);
                result.Body.ShouldBeNull();
            }
 public DefaultAsynchronousFilterChain(DefaultAsynchronousFilterChain original)
     : this(original.dataStore, original.filters)
 {
 }
 public DefaultAsynchronousFilterChain(DefaultAsynchronousFilterChain original)
     : this(original.dataStore, original.filters)
 {
 }