-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Use HttpClient wrappers that ensure success to match FusionCache…
… expectations (#684) ## Description This fixes #671 by implementing a wrapper via extension methods that causes post/get requests that don't return a successful message to throw exceptions. ## Related Issue(s) - #671 ## Verification - [x] **Your** code builds clean without any errors or warnings - [x] Manual testing done (required) - [ ] Relevant automated test added (if you find this hard, leave it and we'll help out) ## Documentation - [ ] Documentation is updated (either in `docs`-directory, Altinnpedia or a separate linked PR in [altinn-studio-docs.](https://github.com/Altinn/altinn-studio-docs), if applicable) --------- Co-authored-by: Ole Jørgen Skogstad <[email protected]>
- Loading branch information
Showing
9 changed files
with
124 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/Digdir.Domain.Dialogporten.Infrastructure/Common/Exceptions/UpstreamServiceException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
namespace Digdir.Domain.Dialogporten.Infrastructure.Common.Exceptions; | ||
|
||
public interface IUpstreamServiceError; | ||
public class UpstreamServiceException(Exception innerException) | ||
: Exception(innerException.Message, innerException), IUpstreamServiceError; |
83 changes: 83 additions & 0 deletions
83
src/Digdir.Domain.Dialogporten.Infrastructure/HttpClientExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
using System.Net.Http.Headers; | ||
using System.Net.Http.Json; | ||
using System.Text.Json; | ||
using Digdir.Domain.Dialogporten.Infrastructure.Common.Exceptions; | ||
|
||
namespace Digdir.Domain.Dialogporten.Infrastructure; | ||
|
||
public static class HttpClientExtensions | ||
{ | ||
public static async Task<T> GetFromJsonEnsuredAsync<T>( | ||
this HttpClient client, | ||
string requestUri, | ||
Action<HttpRequestHeaders>? configureHeaders = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, requestUri); | ||
configureHeaders?.Invoke(httpRequestMessage.Headers); | ||
var response = await client.SendAsync(httpRequestMessage, cancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
var result = await response.Content.ReadFromJsonAsync<T>(cancellationToken: cancellationToken); | ||
return result is null | ||
? throw new JsonException($"Failed to deserialize JSON to type {typeof(T).FullName} from {requestUri}") | ||
: result; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new UpstreamServiceException(e); | ||
} | ||
} | ||
|
||
public static async Task<HttpResponseMessage> PostAsJsonEnsuredAsync( | ||
this HttpClient client, | ||
string requestUri, | ||
object content, | ||
Action<HttpRequestHeaders>? configureHeaders = null, | ||
Action<HttpContentHeaders>? configureContentHeaders = null, | ||
JsonSerializerOptions? serializerOptions = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
var httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) | ||
{ | ||
Content = JsonContent.Create(content, options: serializerOptions) | ||
}; | ||
configureHeaders?.Invoke(httpRequestMessage.Headers); | ||
configureContentHeaders?.Invoke(httpRequestMessage.Content.Headers); | ||
var response = await client.SendAsync(httpRequestMessage, cancellationToken); | ||
response.EnsureSuccessStatusCode(); | ||
return response; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new UpstreamServiceException(e); | ||
} | ||
} | ||
|
||
public static async Task<T> PostAsJsonEnsuredAsync<T>( | ||
this HttpClient client, | ||
string requestUri, | ||
object content, | ||
Action<HttpRequestHeaders>? configureHeaders = null, | ||
Action<HttpContentHeaders>? configureContentHeaders = null, | ||
JsonSerializerOptions? serializerOptions = null, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var response = await client.PostAsJsonEnsuredAsync(requestUri, content, configureHeaders, | ||
configureContentHeaders, serializerOptions, cancellationToken); | ||
try | ||
{ | ||
var result = await response.Content.ReadFromJsonAsync<T>(cancellationToken: cancellationToken); | ||
return result is null | ||
? throw new JsonException($"Failed to deserialize JSON to type {typeof(T).FullName} from {requestUri}") | ||
: result; | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new UpstreamServiceException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters