-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #285 from Lombiq/issue/OFFI-101
OFFI-101: New configuration extension medhods
- Loading branch information
Showing
6 changed files
with
95 additions
and
10 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
23 changes: 23 additions & 0 deletions
23
Lombiq.HelpfulLibraries.Common/Extensions/HttpResponseExtensions.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,23 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace System.Net.Http; | ||
|
||
public static class HttpResponseExtensions | ||
{ | ||
public static async Task ThrowIfNotSuccessAsync( | ||
this HttpResponseMessage response, | ||
string errorMessage, | ||
HttpContent requestBody = null) | ||
{ | ||
if (!response.IsSuccessStatusCode) | ||
{ | ||
var requestContent = requestBody == null ? "<NULL>" : await requestBody.ReadAsStringAsync(); | ||
throw new InvalidOperationException(string.Join( | ||
separator: '\n', | ||
errorMessage, | ||
$"Response: {response}", | ||
$"Response Content: {await response.Content.ReadAsStringAsync()}", | ||
$"Request Content: {requestContent}")); | ||
} | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
Lombiq.HelpfulLibraries.Common/Extensions/TaskExtensions.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,21 @@ | ||
namespace System.Threading.Tasks; | ||
|
||
public static class TaskExtensions | ||
{ | ||
/// <summary> | ||
/// A shortcut for <see cref="MulticastDelegateExtensions.InvokeAsync{TDelegate}"/> when the delegate is a <see | ||
/// cref="Task"/> returning <see cref="Func{TIn, TResult}"/>. | ||
/// </summary> | ||
public static Task InvokeFuncAsync<TIn>(this Func<TIn, Task> @delegate, TIn argument) => | ||
@delegate.InvokeAsync<Func<TIn, Task>>(func => func(argument)); | ||
|
||
/// <summary> | ||
/// Converts a <see cref="Task{TResult}"/> that returns an <see cref="IDisposable"/> instance into <see | ||
/// cref="Task"/> by disposing and discarding its result. | ||
/// </summary> | ||
public static Task DisposeResultAsync<T>(this Task<T> task) | ||
where T : IDisposable => | ||
task.ContinueWith( | ||
finishedTask => finishedTask.Result?.Dispose(), | ||
TaskScheduler.Current); | ||
} |
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