diff --git a/Fauna.Test/Integration.Tests.cs b/Fauna.Test/Integration.Tests.cs index 7aec4cf8..e4739654 100644 --- a/Fauna.Test/Integration.Tests.cs +++ b/Fauna.Test/Integration.Tests.cs @@ -553,7 +553,7 @@ public async Task CanOpenFeedWithQuery() Assert.NotNull(feed.CurrentPage, "should have loaded a page"); Assert.IsNotEmpty(feed.Cursor, "should have a cursor"); - Assert.IsEmpty(feed.CurrentPage!.Events, "should note have events"); + Assert.IsEmpty(feed.CurrentPage!.Events, "should not have events"); await _client.QueryAsync(FQL($"StreamingSandbox.create({{ foo: 'bar' }})")); diff --git a/Fauna/Client.cs b/Fauna/Client.cs index b139019f..cbe2ab56 100644 --- a/Fauna/Client.cs +++ b/Fauna/Client.cs @@ -173,7 +173,7 @@ internal override async IAsyncEnumerator> SubscribeFeedInternal( { cancel.ThrowIfCancellationRequested(); - var finalOptions = QueryOptions.GetFinalQueryOptions(_config.DefaultQueryOptions, null); + var finalOptions = _config.DefaultQueryOptions; var headers = GetRequestHeaders(finalOptions); while (!cancel.IsCancellationRequested) diff --git a/Fauna/Core/FeedEnumberable.cs b/Fauna/Core/FeedEnumberable.cs index d84f6c09..a7a09c3b 100644 --- a/Fauna/Core/FeedEnumberable.cs +++ b/Fauna/Core/FeedEnumberable.cs @@ -1,84 +1,7 @@ -using System.Collections; -using System.Text.Json; -using Fauna.Exceptions; -using Fauna.Mapping; -using Fauna.Serialization; using Fauna.Types; -using static Fauna.Core.ResponseFields; namespace Fauna.Core; -/// -/// Represents the response from Fauna Event Feed requests. -/// -/// -public class FeedPage where T : notnull -{ - /// - /// List of Events returned by the Feed - /// - public List> Events { get; private init; } = []; - - /// - /// Cursor returned from the Feed - /// - public string Cursor { get; private init; } = null!; - - /// - /// Indicates if there are more pages for pagination. - /// - public bool HasNext { get; private init; } - - /// - /// Stats returned from the Feed. - /// - public QueryStats Stats { get; private init; } - - internal static FeedPage From(string body, MappingContext ctx) - { - var json = JsonSerializer.Deserialize(body); - - var err = GetError(json); - if (err != null) - { - throw new FaunaException(err.Value); - } - - return new FeedPage - { - Cursor = GetCursor(json), - Events = GetEvents(json, ctx), - Stats = GetStats(json), - HasNext = json.TryGetProperty(HasNextFieldName, out var elem) && elem.GetBoolean() - }; - } - - private static List> GetEvents(JsonElement json, MappingContext ctx) - { - if (!json.TryGetProperty(EventsFieldName, out var elem)) - { - return new List>(); - } - - var events = elem.EnumerateArray().Select(e => Event.From(e, ctx)).ToList(); - return events; - } - - private static QueryStats GetStats(JsonElement json) - { - return json.TryGetProperty(StatsFieldName, out var elem) ? elem.Deserialize() : default; - } - - private static string GetCursor(JsonElement json) - { - return json.TryGetProperty(CursorFieldName, out var elem) ? elem.GetString()! : null!; - } - - private static ErrorInfo? GetError(JsonElement json) - { - return json.TryGetProperty(ErrorFieldName, out var elem) ? elem.Deserialize() : null; - } -} /// /// Represents a Fauna Event Feed. @@ -97,7 +20,7 @@ public class FeedEnumerable where T : notnull public string? Cursor => _eventSource.LastCursor; /// - /// The last page returned from the Event Feed enumerator. + /// The latest page returned from the Event Feed enumerator. /// public FeedPage? CurrentPage { get; private set; } diff --git a/Fauna/Core/FeedPage.cs b/Fauna/Core/FeedPage.cs new file mode 100644 index 00000000..6247619f --- /dev/null +++ b/Fauna/Core/FeedPage.cs @@ -0,0 +1,79 @@ +using System.Text.Json; +using Fauna.Exceptions; +using Fauna.Mapping; +using Fauna.Types; +using static Fauna.Core.ResponseFields; + +namespace Fauna.Core; + +/// +/// Represents the response from Fauna Event Feed requests. +/// +/// +public class FeedPage where T : notnull +{ + /// + /// List of Events returned by the Feed + /// + public List> Events { get; private init; } = []; + + /// + /// Cursor returned from the Feed + /// + public string Cursor { get; private init; } = null!; + + /// + /// Indicates if there are more pages for pagination. + /// + public bool HasNext { get; private init; } + + /// + /// Stats returned from the Feed. + /// + public QueryStats Stats { get; private init; } + + internal static FeedPage From(string body, MappingContext ctx) + { + var json = JsonSerializer.Deserialize(body); + + var err = GetError(json); + if (err != null) + { + throw new FaunaException(err.Value); + } + + return new FeedPage + { + Cursor = GetCursor(json), + Events = GetEvents(json, ctx), + Stats = GetStats(json), + HasNext = json.TryGetProperty(HasNextFieldName, out var elem) && elem.GetBoolean() + }; + } + + private static List> GetEvents(JsonElement json, MappingContext ctx) + { + if (!json.TryGetProperty(EventsFieldName, out var elem)) + { + return new List>(); + } + + var events = elem.EnumerateArray().Select(e => Event.From(e, ctx)).ToList(); + return events; + } + + private static QueryStats GetStats(JsonElement json) + { + return json.TryGetProperty(StatsFieldName, out var elem) ? elem.Deserialize() : default; + } + + private static string GetCursor(JsonElement json) + { + return json.TryGetProperty(CursorFieldName, out var elem) ? elem.GetString()! : null!; + } + + private static ErrorInfo? GetError(JsonElement json) + { + return json.TryGetProperty(ErrorFieldName, out var elem) ? elem.Deserialize() : null; + } +} diff --git a/Fauna/IClient.cs b/Fauna/IClient.cs index c1609fd9..4d3f879f 100644 --- a/Fauna/IClient.cs +++ b/Fauna/IClient.cs @@ -641,14 +641,12 @@ public async Task> EventFeedAsync( /// Opens the event feed with Fauna and returns an enumerator for the events. /// /// The query to create the stream from Fauna. - /// The options for the query. /// The options for the feed. /// The cancellation token for the operation. /// Which Type to map the Events to. /// public async Task> EventFeedAsync( Query query, - QueryOptions? queryOptions = null, FeedOptions? feedOptions = null, CancellationToken cancellationToken = default) where T : notnull { @@ -657,7 +655,7 @@ public async Task> EventFeedAsync( throw new InvalidOperationException("Cannot use Cursor when opening an EventFeed with a Query."); } - EventSource eventSource = await GetEventSourceFromQueryAsync(query, queryOptions, cancellationToken); + EventSource eventSource = await GetEventSourceFromQueryAsync(query, null, cancellationToken); return new FeedEnumerable(this, eventSource, feedOptions, cancellationToken); } diff --git a/Fauna/Types/Event.cs b/Fauna/Types/Event.cs index 4f35d1a8..9663800e 100644 --- a/Fauna/Types/Event.cs +++ b/Fauna/Types/Event.cs @@ -66,7 +66,7 @@ public class Event where T : notnull /// A mapping context to influence deserialization. /// An instance of . /// Thrown when the event includes a Fauna error. - public static Event From(JsonElement json, MappingContext ctx) + internal static Event From(JsonElement json, MappingContext ctx) { var err = GetError(json); if (err != null) @@ -93,7 +93,7 @@ public static Event From(JsonElement json, MappingContext ctx) /// A mapping context to influence deserialization. /// An instance of . /// Thrown when the event includes a Fauna error. - public static Event From(string body, MappingContext ctx) + internal static Event From(string body, MappingContext ctx) { var json = JsonSerializer.Deserialize(body);