Skip to content

Commit

Permalink
Added EndpointBase.Serializer as alias for .Serializers[0]
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianeicher committed Oct 3, 2023
1 parent a76a2f9 commit d209c11
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public virtual IObservable<TEntity> GetObservable()
using var response = await HttpClient.GetAsync(Uri, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
await ErrorHandler.HandleAsync(response);

var entityStream = new HttpEntityStream<TEntity>(response.Content, Serializers[0], _separator, BufferSize);
var entityStream = new HttpEntityStream<TEntity>(response.Content, Serializer, _separator, BufferSize);

while (!cancellationToken.IsCancellationRequested)
{
Expand Down
12 changes: 12 additions & 0 deletions src/TypedRest/Endpoints/EndpointBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,21 @@ namespace TypedRest.Endpoints;
public abstract class EndpointBase : IEndpoint
{
public Uri Uri { get; }

public HttpClient HttpClient { get; }

public IReadOnlyList<MediaTypeFormatter> Serializers { get; }

/// <summary>
/// The serializer used for entities sent to the server. Equal to the first entry in <see cref="Serializers"/>.
/// </summary>
/// <exception cref="InvalidOperationException"><see cref="Serializers"/> is empty.</exception>
protected virtual MediaTypeFormatter Serializer
=> Serializers.FirstOrDefault()
?? throw new InvalidOperationException($"{nameof(Serializers)} is empty.");

public IErrorHandler ErrorHandler { get; }

public ILinkExtractor LinkExtractor { get; }

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/TypedRest/Endpoints/Generic/CollectionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public virtual async Task<PartialResponse<TEntity>> 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)
Expand Down Expand Up @@ -143,7 +143,7 @@ public virtual async Task CreateAllAsync(IEnumerable<TEntity> 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);
Expand All @@ -152,7 +152,7 @@ public async Task SetAllAsync(IEnumerable<TEntity> entities, CancellationToken c
{
if (entities == null) throw new ArgumentNullException(nameof(entities));

using var content = new ObjectContent<IEnumerable<TEntity>>(entities, Serializers[0]);
using var content = new ObjectContent<IEnumerable<TEntity>>(entities, Serializer);
(await PutContentAsync(content, cancellationToken)).Dispose();
}
}
4 changes: 2 additions & 2 deletions src/TypedRest/Endpoints/Generic/ElementEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public async Task<bool> ExistsAsync(CancellationToken cancellationToken = defaul
{
if (entity == null) throw new ArgumentNullException(nameof(entity));

using var content = new ObjectContent<TEntity>(entity, Serializers[0]);
using var content = new ObjectContent<TEntity>(entity, Serializer);
using var response = await PutContentAsync(content, cancellationToken);
return await TryReadAsAsync(response, cancellationToken);
}
Expand All @@ -65,7 +65,7 @@ public async Task<bool> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/TypedRest/Endpoints/Rpc/ConsumerEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
2 changes: 1 addition & 1 deletion src/TypedRest/Endpoints/Rpc/FunctionEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public async Task<TResult> 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<TResult>(Serializers, cancellationToken).NoContext();
}
}

0 comments on commit d209c11

Please sign in to comment.