public async Task <StreamMetadataResult> GetStreamMetadata(
            string streamId,
            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);

            var resource = client.Current.First();

            return(resource.Data <HalStreamMetadataResult>());
        }
        public async Task DeleteStream(
            StreamId streamId,
            int expectedVersion = ExpectedVersion.Any,
            CancellationToken cancellationToken = default)
        {
            Ensure.That(expectedVersion, nameof(expectedVersion)).IsGte(ExpectedVersion.Any);

            GuardAgainstDisposed();

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

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

            ThrowOnError(client);
        }
Ejemplo n.º 3
0
        public async Task DeleteStream(
            StreamId streamId,
            int expectedVersion = ExpectedVersion.Any,
            CancellationToken cancellationToken = default)
        {
            var client = CreateClient(new Resource
            {
                Links =
                {
                    new Link
                    {
                        Href = LinkFormatter.Stream(streamId),
                        Rel  = Constants.Relations.AppendToStream
                    }
                }
            });

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

            ThrowOnError(client);
        }
Ejemplo n.º 4
0
        public async Task <AppendResult> AppendToStream(
            StreamId streamId,
            int expectedVersion,
            NewStreamMessage[] messages,
            CancellationToken cancellationToken = default)
        {
            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 SetStreamMetadata(
            StreamId streamId,
            int expectedStreamMetadataVersion = ExpectedVersion.Any,
            int?maxAge          = null,
            int?maxCount        = null,
            string metadataJson = null,
            CancellationToken cancellationToken = default)
        {
            GuardAgainstDisposed();

            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);
        }