public async Task SetStreamMetadata(
            StreamId streamId,
            int expectedStreamMetadataVersion = ExpectedVersion.Any,
            int?maxAge          = null,
            int?maxCount        = null,
            string metadataJson = null,
            CancellationToken cancellationToken = default)
        {
            var client = CreateClient(new Resource
            {
                Links =
                {
                    new Link
                    {
                        Href = LinkFormatter.Stream(streamId)
                    }
                }
            });

            client = await client.GetAsync(client.Current.First(), null);

            if (client.StatusCode != HttpStatusCode.NotFound)
            {
                ThrowOnError(client);
            }

            client = await client.GetAsync(client.Current.First(), Constants.Relations.Metadata);

            if (client.StatusCode != HttpStatusCode.NotFound)
            {
                ThrowOnError(client);
            }

            var metadata = new Dictionary <string, object>
            {
                ["maxAge"]   = maxAge,
                ["maxCount"] = maxCount
            };

            if (!string.IsNullOrEmpty(metadataJson))
            {
                metadata["metadataJson"] = TryParseMetadataJson(metadataJson);
            }

            client = await client.Post(
                Constants.Relations.Self,
                metadata,
                null,
                null,
                new Dictionary <string, string[]>
            {
                [Constants.Headers.ExpectedVersion] = new[] { $"{expectedStreamMetadataVersion}" }
            },
                cancellationToken);

            ThrowOnError(client);
        }
        public async Task <ReadAllPage> ReadAllBackwards(
            long fromPositionInclusive,
            int maxCount,
            bool prefetchJsonData = true,
            CancellationToken cancellationToken = default)
        {
            var client = CreateClient();

            client = await client.RootAsync(
                LinkFormatter.ReadAllBackwards(fromPositionInclusive, maxCount, prefetchJsonData),
                cancellationToken);

            return(ReadAllBackwardsInternal(client, prefetchJsonData));
        }
Esempio n. 3
0
        public async Task <long> ReadHeadPosition(CancellationToken cancellationToken = default)
        {
            var client   = CreateClient();
            var response = await client.Client.HeadAsync(LinkFormatter.AllStream(), cancellationToken);

            response.EnsureSuccessStatusCode();

            response.Headers.TryGetValues(Constants.Headers.HeadPosition, out var headPositionHeaders);

            if (!long.TryParse(headPositionHeaders.Single(), out var headPosition))
            {
                throw new InvalidOperationException();
            }

            return(headPosition);
        }
Esempio n. 4
0
        public async Task <ReadStreamPage> ReadStreamBackwards(
            StreamId streamId,
            int fromVersionInclusive,
            int maxCount,
            bool prefetchJsonData = true,
            CancellationToken cancellationToken = default)
        {
            var client = CreateClient();

            client = await client.RootAsync(
                LinkFormatter.ReadStreamBackwards(streamId, fromVersionInclusive, maxCount, prefetchJsonData),
                cancellationToken);

            return(ReadStreamBackwardsInternal(
                       client,
                       streamId,
                       fromVersionInclusive,
                       prefetchJsonData));
        }
Esempio n. 5
0
        public async Task <ReadAllPage> ReadAllBackwards(
            long fromPositionInclusive,
            int maxCount,
            bool prefetchJsonData = true,
            CancellationToken cancellationToken = default)
        {
            Ensure.That(fromPositionInclusive, nameof(fromPositionInclusive)).IsGte(-1);
            Ensure.That(maxCount, nameof(maxCount)).IsGte(1);

            GuardAgainstDisposed();

            var client = CreateClient();

            client = await client.RootAsync(
                LinkFormatter.ReadAllBackwards(fromPositionInclusive, maxCount, prefetchJsonData),
                cancellationToken);

            return(ReadAllBackwardsInternal(client, prefetchJsonData));
        }
        public async Task <AppendResult> AppendToStream(
            StreamId streamId,
            int expectedVersion,
            NewStreamMessage[] messages,
            CancellationToken cancellationToken = default)
        {
            Ensure.That(expectedVersion, nameof(expectedVersion)).IsGte(ExpectedVersion.NoStream);
            Ensure.That(messages, nameof(messages)).IsNotNull();

            GuardAgainstDisposed();

            var client = CreateClient(new Resource
            {
                Links =
                {
                    new Link
                    {
                        Href = LinkFormatter.Stream(streamId),
                        Rel  = Constants.Relations.AppendToStream
                    }
                }
            });

            client = await client.Post(
                Constants.Relations.AppendToStream,
                messages,
                null,
                null,
                new Dictionary <string, string[]>
            {
                [Constants.Headers.ExpectedVersion] = new[] { $"{expectedVersion}" }
            },
                cancellationToken);

            ThrowOnError(client);

            var resource = client.Current.First();

            return(resource.Data <HalAppendResult>());
        }
        public async Task <ReadStreamPage> ReadStreamForwards(
            StreamId streamId,
            int fromVersionInclusive,
            int maxCount,
            bool prefetchJsonData = true,
            CancellationToken cancellationToken = default)
        {
            Ensure.That(fromVersionInclusive, nameof(fromVersionInclusive)).IsGte(0);
            Ensure.That(maxCount, nameof(maxCount)).IsGte(1);

            GuardAgainstDisposed();

            var client = CreateClient();

            client = await client.RootAsync(
                LinkFormatter.ReadStreamForwards(streamId, fromVersionInclusive, maxCount, prefetchJsonData),
                cancellationToken);

            return(ReadStreamForwardsInternal(
                       client,
                       streamId,
                       fromVersionInclusive,
                       prefetchJsonData));
        }