diff --git a/src/TypedRest.Reactive/Endpoints/Reactive/StreamingEndpoint.cs b/src/TypedRest.Reactive/Endpoints/Reactive/StreamingEndpoint.cs index 3ae77554..a9e55fc0 100644 --- a/src/TypedRest.Reactive/Endpoints/Reactive/StreamingEndpoint.cs +++ b/src/TypedRest.Reactive/Endpoints/Reactive/StreamingEndpoint.cs @@ -47,7 +47,7 @@ public virtual IObservable GetObservable() using var response = await HttpClient.GetAsync(Uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken); await ErrorHandler.HandleAsync(response); - var entityStream = new HttpEntityStream(response.Content, Serializers[0], _separator, BufferSize); + var entityStream = new HttpEntityStream(response.Content, Serializer, _separator, BufferSize); while (!cancellationToken.IsCancellationRequested) { diff --git a/src/TypedRest/Endpoints/EndpointBase.cs b/src/TypedRest/Endpoints/EndpointBase.cs index cc2ae308..d09f0f87 100644 --- a/src/TypedRest/Endpoints/EndpointBase.cs +++ b/src/TypedRest/Endpoints/EndpointBase.cs @@ -12,9 +12,21 @@ namespace TypedRest.Endpoints; public abstract class EndpointBase : IEndpoint { public Uri Uri { get; } + public HttpClient HttpClient { get; } + public IReadOnlyList Serializers { get; } + + /// + /// The serializer used for entities sent to the server. Equal to the first entry in . + /// + /// is empty. + protected virtual MediaTypeFormatter Serializer + => Serializers.FirstOrDefault() + ?? throw new InvalidOperationException($"{nameof(Serializers)} is empty."); + public IErrorHandler ErrorHandler { get; } + public ILinkExtractor LinkExtractor { get; } /// diff --git a/src/TypedRest/Endpoints/Generic/CollectionEndpoint.cs b/src/TypedRest/Endpoints/Generic/CollectionEndpoint.cs index f5aa765f..95f46d9b 100644 --- a/src/TypedRest/Endpoints/Generic/CollectionEndpoint.cs +++ b/src/TypedRest/Endpoints/Generic/CollectionEndpoint.cs @@ -109,7 +109,7 @@ public virtual async Task> ReadRangeAsync(RangeItemHead { if (entity == null) throw new ArgumentNullException(nameof(entity)); - var response = await HandleAsync(() => HttpClient.PostAsync(Uri, entity, Serializers[0], cancellationToken)).NoContext(); + var response = await HandleAsync(() => HttpClient.PostAsync(Uri, entity, Serializer, cancellationToken)).NoContext(); TElementEndpoint elementEndpoint; if (response.Headers.Location == null) @@ -143,7 +143,7 @@ public virtual async Task CreateAllAsync(IEnumerable entities, Cancella { if (entities == null) throw new ArgumentNullException(nameof(entities)); - await FinalizeAsync(() => HttpClient.PatchAsync(Uri, entities, Serializers[0], cancellationToken)).NoContext(); + await FinalizeAsync(() => HttpClient.PatchAsync(Uri, entities, Serializer, cancellationToken)).NoContext(); } public bool? SetAllAllowed => IsMethodAllowed(HttpMethod.Put); @@ -152,7 +152,7 @@ public async Task SetAllAsync(IEnumerable entities, CancellationToken c { if (entities == null) throw new ArgumentNullException(nameof(entities)); - using var content = new ObjectContent>(entities, Serializers[0]); + using var content = new ObjectContent>(entities, Serializer); (await PutContentAsync(content, cancellationToken)).Dispose(); } } diff --git a/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs b/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs index aec5f2f9..a47b2a18 100644 --- a/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs +++ b/src/TypedRest/Endpoints/Generic/ElementEndpoint.cs @@ -54,7 +54,7 @@ public async Task ExistsAsync(CancellationToken cancellationToken = defaul { if (entity == null) throw new ArgumentNullException(nameof(entity)); - using var content = new ObjectContent(entity, Serializers[0]); + using var content = new ObjectContent(entity, Serializer); using var response = await PutContentAsync(content, cancellationToken); return await TryReadAsAsync(response, cancellationToken); } @@ -65,7 +65,7 @@ public async Task ExistsAsync(CancellationToken cancellationToken = defaul { if (entity == null) throw new ArgumentNullException(nameof(entity)); - using var response = await HandleAsync(() => HttpClient.PatchAsync(Uri, entity, Serializers[0], cancellationToken)).NoContext(); + using var response = await HandleAsync(() => HttpClient.PatchAsync(Uri, entity, Serializer, cancellationToken)).NoContext(); ResponseCache = null; return await TryReadAsAsync(response, cancellationToken); } diff --git a/src/TypedRest/Endpoints/Rpc/ConsumerEndpoint.cs b/src/TypedRest/Endpoints/Rpc/ConsumerEndpoint.cs index 9af7f022..e0b71726 100644 --- a/src/TypedRest/Endpoints/Rpc/ConsumerEndpoint.cs +++ b/src/TypedRest/Endpoints/Rpc/ConsumerEndpoint.cs @@ -28,6 +28,6 @@ public async Task InvokeAsync(TEntity entity, CancellationToken cancellationToke { if (entity == null) throw new ArgumentNullException(nameof(entity)); - await FinalizeAsync(() => HttpClient.PostAsync(Uri, entity, Serializers[0], cancellationToken)).NoContext(); + await FinalizeAsync(() => HttpClient.PostAsync(Uri, entity, Serializer, cancellationToken)).NoContext(); } } diff --git a/src/TypedRest/Endpoints/Rpc/FunctionEndpoint.cs b/src/TypedRest/Endpoints/Rpc/FunctionEndpoint.cs index 7214f81b..af7dc829 100644 --- a/src/TypedRest/Endpoints/Rpc/FunctionEndpoint.cs +++ b/src/TypedRest/Endpoints/Rpc/FunctionEndpoint.cs @@ -29,7 +29,7 @@ public async Task InvokeAsync(TEntity entity, CancellationToken cancell { if (entity == null) throw new ArgumentNullException(nameof(entity)); - using var response = await HandleAsync(() => HttpClient.PostAsync(Uri, entity, Serializers[0], cancellationToken)).NoContext(); + using var response = await HandleAsync(() => HttpClient.PostAsync(Uri, entity, Serializer, cancellationToken)).NoContext(); return await response.Content.ReadAsAsync(Serializers, cancellationToken).NoContext(); } }