Skip to content

Commit

Permalink
Remove StatusCode exceptions. Refactoring. (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
evgenyvalavin authored Jul 6, 2023
1 parent 2e09fcc commit 4a85b99
Show file tree
Hide file tree
Showing 23 changed files with 116 additions and 207 deletions.
4 changes: 1 addition & 3 deletions samples/Arbus.Network.Demo/GetAllOrdersApiEndpoint.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Arbus.Network.Abstractions;

namespace Arbus.Network.Demo;
namespace Arbus.Network.Demo;

public class GetAllOrdersApiEndpoint : ApiEndpoint<OrdersResponseDto>
{
Expand Down
4 changes: 1 addition & 3 deletions samples/Arbus.Network.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Arbus.Network.Abstractions;
using Arbus.Network.Exceptions;
using Arbus.Network.Implementations;
using Arbus.Network.Exceptions;

namespace Arbus.Network.Demo;

Expand Down
6 changes: 2 additions & 4 deletions src/Arbus.Network.UnitTests/Tests/NativeHttpClientTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Arbus.Network.Abstractions;
using Arbus.Network.Exceptions;
using Arbus.Network.Exceptions;
using Arbus.Network.Extensions;
using Arbus.Network.Implementations;

namespace Arbus.Network.UnitTests.Tests;

Expand Down Expand Up @@ -32,7 +30,7 @@ public void EnsureNetworkAvailable_NetworkNotAvailable_ThrowsNoNetworkConnection

NativeHttpClient nativeHttpClient = new(mockNetworkManager.Object);

Assert.Throws<NoNetoworkConnectionException>(() => nativeHttpClient.EnsureNetworkAvailable());
Assert.Throws<NoNetworkConnectionException>(() => nativeHttpClient.EnsureNetworkAvailable());
}

[Test]
Expand Down
9 changes: 0 additions & 9 deletions src/Arbus.Network/Abstractions/IHttpContext.cs

This file was deleted.

7 changes: 0 additions & 7 deletions src/Arbus.Network/Abstractions/INetworkManager.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Net.Http.Headers;
using System.Text;

namespace Arbus.Network.Abstractions;
namespace Arbus.Network;

public abstract class ApiEndpoint
{
Expand Down Expand Up @@ -71,31 +71,34 @@ public virtual Task ValidateResponse(HttpResponseMessage responseMessage)
private async Task EnsureSuccessResponse(HttpResponseMessage responseMessage)
{
if (responseMessage.IsSuccessStatusCode is false)
throw await HandleNotSuccessStatusCode(responseMessage).ConfigureAwait(false);
await HandleNotSuccessStatusCode(responseMessage).ConfigureAwait(false);

//In case of an exception above wil not be thrown make sure to throw an exception
responseMessage.EnsureSuccessStatusCode();
}

public virtual Task<Exception> HandleNotSuccessStatusCode(HttpResponseMessage responseMessage)
public virtual Task HandleNotSuccessStatusCode(HttpResponseMessage responseMessage)
{
if (responseMessage.Content.Headers.ContentType?.MediaType == HttpContentType.Application.ProblemJson)
return HandleProblemDetailsResponse(responseMessage);
return HandleAnyResponse(responseMessage);
}

public static async Task<Exception> HandleProblemDetailsResponse(HttpResponseMessage responseMessage)
public static async Task HandleProblemDetailsResponse(HttpResponseMessage responseMessage)
{
var responseStream = await responseMessage.Content.ReadAsStreamAsync().ConfigureAwait(false);

var problemDetails = await JsonSerializer.DeserializeAsync<ProblemDetails>(
responseStream, GlobalJsonSerializerOptions.Options).ConfigureAwait(false)
?? throw new Exception("Failed to deserialize ProblemDetails.");

return NetworkExceptionFactory.Create(responseMessage.StatusCode, problemDetails);
throw new NetworkException(responseMessage.StatusCode, problemDetails);
}

public virtual async Task<Exception> HandleAnyResponse(HttpResponseMessage responseMessage)
public virtual async Task HandleAnyResponse(HttpResponseMessage responseMessage)
{
var responseString = await responseMessage.Content.ReadAsStringAsync().ConfigureAwait(false);
return NetworkExceptionFactory.Create(responseMessage.StatusCode, responseString);
throw new NetworkException(responseMessage.StatusCode, responseString);
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/Arbus.Network/Arbus.Network.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
<Compile Remove="IsExternalInit.cs" />
<PackageReference Include="System.Text.Json" Version="7.0.3" />
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 0 additions & 14 deletions src/Arbus.Network/Exceptions/BadRequestException.cs

This file was deleted.

10 changes: 5 additions & 5 deletions src/Arbus.Network/Exceptions/NetworkException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ public class NetworkException : Exception

public ProblemDetails? ProblemDetails { get; }

public NetworkException(HttpStatusCode httpStatusCode, string stringContent) : base(stringContent)
{
StatusCode = httpStatusCode;
}

public NetworkException(HttpStatusCode httpStatusCode, ProblemDetails problemDetails)
: this(httpStatusCode, problemDetails.Detail ?? problemDetails.Title ?? string.Empty)
{
ProblemDetails = problemDetails;
}

public NetworkException(HttpStatusCode httpStatusCode, string message) : this(message)
{
StatusCode = httpStatusCode;
}

public NetworkException(string message) : base(message)
{
}
Expand Down
20 changes: 0 additions & 20 deletions src/Arbus.Network/Exceptions/NetworkExceptionFactory.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/Arbus.Network/Exceptions/NoNetoworkConnectionException.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
namespace Arbus.Network.Exceptions;

public class NoNetoworkConnectionException : NetworkException
public class NoNetworkConnectionException : NetworkException
{
public NoNetoworkConnectionException() : base("No network connection.")
public NoNetworkConnectionException() : base("No network connection.")
{
}
}
14 changes: 0 additions & 14 deletions src/Arbus.Network/Exceptions/UnauthorizedException.cs

This file was deleted.

9 changes: 0 additions & 9 deletions src/Arbus.Network/Extensions/HttpClientExtensions.cs

This file was deleted.

38 changes: 19 additions & 19 deletions src/Arbus.Network/Extensions/HttpContentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,56 @@ namespace Arbus.Network.Extensions;
public static class HttpContentExtensions
{
public static bool IsApplicationJson(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Application.Json);
IsRequiredContentType(content, HttpContentType.Application.Json);

public static bool IsApplicationText(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Application.Text);
IsRequiredContentType(content, HttpContentType.Application.Text);

public static bool IsApplicationProtobuf(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Application.Protobuf);
IsRequiredContentType(content, HttpContentType.Application.Protobuf);

public static bool IsApplicationProblemJson(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Application.ProblemJson);
IsRequiredContentType(content, HttpContentType.Application.ProblemJson);

public static bool IsTextPlain(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Text.Plain);
IsRequiredContentType(content, HttpContentType.Text.Plain);

public static bool IsMultipartFormData(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Multipart.FormData);
IsRequiredContentType(content, HttpContentType.Multipart.FormData);

public static bool IsMultipartMixed(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Multipart.Mixed);
IsRequiredContentType(content, HttpContentType.Multipart.Mixed);

public static bool IsMultipartAlternative(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Multipart.Alternative);
IsRequiredContentType(content, HttpContentType.Multipart.Alternative);

public static bool IsMultipartRelated(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Multipart.Related);
IsRequiredContentType(content, HttpContentType.Multipart.Related);

public static bool IsImageSvgXml(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.SvgXml);
IsRequiredContentType(content, HttpContentType.Image.SvgXml);

public static bool IsImageGif(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.Gif);
IsRequiredContentType(content, HttpContentType.Image.Gif);

public static bool IsImageJpeg(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.Jpeg);
IsRequiredContentType(content, HttpContentType.Image.Jpeg);

public static bool IsImagePng(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.Png);
IsRequiredContentType(content, HttpContentType.Image.Png);

public static bool IsImageTiff(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.Tiff);
IsRequiredContentType(content, HttpContentType.Image.Tiff);

public static bool IsImageVndMicrosoftIcon(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.VndMicrosoftIcon);
IsRequiredContentType(content, HttpContentType.Image.VndMicrosoftIcon);

public static bool IsImageXIcon(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.XIcon);
IsRequiredContentType(content, HttpContentType.Image.XIcon);

public static bool IsImageVndDjvu(this HttpContent content) =>
content.IsEqualsTo(HttpContentType.Image.VndDjvu);
IsRequiredContentType(content, HttpContentType.Image.VndDjvu);

private static bool IsEqualsTo(this HttpContent content, string contentType) =>
content.Headers.ContentType.MediaType == contentType;
private static bool IsRequiredContentType(HttpContent content, string contentType) =>
content.Headers.ContentType?.MediaType == contentType;
}
50 changes: 50 additions & 0 deletions src/Arbus.Network/HttpClientContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Net.Http.Headers;

namespace Arbus.Network;

public class HttpClientContext : IHttpClientContext
{
protected INativeHttpClient _nativeHttpClient;

public HttpClientContext(INativeHttpClient httpClientHandler)
{
_nativeHttpClient = httpClientHandler;
}

public async Task RunEndpoint(ApiEndpoint endpoint)
{
using var responseMessage = await RunEndpointInternal(endpoint).ConfigureAwait(false);
}

public async Task<TResponse> RunEndpoint<TResponse>(ApiEndpoint<TResponse> endpoint)
{
using var responseMessage = await RunEndpointInternal(endpoint).ConfigureAwait(false);
return await endpoint.GetResponse(responseMessage).ConfigureAwait(false);
}

public virtual async Task<HttpResponseMessage> RunEndpointInternal(ApiEndpoint endpoint)
{
var request = CreateRequest(endpoint);

var response = await _nativeHttpClient.SendRequest(
request, endpoint.CancellationToken ?? default).ConfigureAwait(false);

await endpoint.ValidateResponse(response).ConfigureAwait(false);

return response;
}

private HttpRequestMessage CreateRequest(ApiEndpoint endpoint)
{
var request = endpoint.CreateRequest(
GetBaseUri());
AddHeaders(request.Headers);
return request;
}

public virtual Uri? GetBaseUri() => default;

protected virtual void AddHeaders(HttpRequestHeaders headers)
{
}
}
7 changes: 7 additions & 0 deletions src/Arbus.Network/IHttpContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Arbus.Network;

public interface IHttpClientContext
{
Task RunEndpoint(ApiEndpoint endpoint);
Task<TResponse> RunEndpoint<TResponse>(ApiEndpoint<TResponse> endpoint);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Arbus.Network.Abstractions;
namespace Arbus.Network;

public interface INativeHttpClient
{
Expand Down
7 changes: 7 additions & 0 deletions src/Arbus.Network/INetworkManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Arbus.Network;

public interface INetworkManager
{
bool IsNetworkAvailable { get; }
event EventHandler<bool>? NetworkAvailabilityChanged;
}
Loading

0 comments on commit 4a85b99

Please sign in to comment.