forked from masastack/MASA.Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: Caller adds handling of Service exception reporting * chore: adjust GlobalException * chore: Fix redis set error * chore: Response supports null Co-authored-by: zhenlei520 <[email protected]>
- Loading branch information
1 parent
a9b31c1
commit 4fdfd4f
Showing
22 changed files
with
154 additions
and
76 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
35 changes: 35 additions & 0 deletions
35
src/Caller/MASA.Utils.Caller.Core/DefaultRequestMessage.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,35 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public class DefaultRequestMessage : IRequestMessage | ||
{ | ||
private readonly CallerOptions _callerOptions; | ||
|
||
public DefaultRequestMessage(CallerOptions callerOptions) | ||
{ | ||
_callerOptions = callerOptions; | ||
} | ||
|
||
public async Task<TResponse?> ProcessResponseAsync<TResponse>(HttpResponseMessage response, CancellationToken cancellationToken = default) | ||
{ | ||
if (response.IsSuccessStatusCode) | ||
{ | ||
switch (response.StatusCode) | ||
{ | ||
case HttpStatusCode.Accepted: | ||
case HttpStatusCode.NoContent: | ||
return default; | ||
case (HttpStatusCode)MasaHttpStatusCode.UserFriendlyException: | ||
throw new UserFriendlyException(await response.Content.ReadAsStringAsync(cancellationToken)); | ||
default: | ||
if (typeof(TResponse).GetInterfaces().Any(type => type == typeof(IConvertible))) | ||
{ | ||
var content = await response.Content.ReadAsStringAsync(cancellationToken); | ||
return (TResponse)Convert.ChangeType(content, typeof(TResponse)); | ||
} | ||
return await response.Content.ReadFromJsonAsync<TResponse>(_callerOptions.JsonSerializerOptions, cancellationToken) | ||
?? throw new ArgumentException("Response cannot be empty"); | ||
} | ||
} | ||
throw new Exception(await response.Content.ReadAsStringAsync(cancellationToken)); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public interface IRequestMessage | ||
{ | ||
Task<TResponse?> ProcessResponseAsync<TResponse>(HttpResponseMessage response, CancellationToken cancellationToken = default); | ||
} |
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
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
Oops, something went wrong.