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.
- Loading branch information
1 parent
4516689
commit e4428fc
Showing
20 changed files
with
709 additions
and
4 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
153 changes: 153 additions & 0 deletions
153
src/Caller/MASA.Utils.Caller.Core/AbstractCallerProvider.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,153 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public abstract class AbstractCallerProvider : ICallerProvider | ||
{ | ||
public abstract Task<TResponse> SendAsync<TResponse>(HttpRequestMessage request, CancellationToken cancellationToken = default); | ||
|
||
public abstract HttpRequestMessage CreateRequest(HttpMethod method, string? methodName); | ||
|
||
public abstract HttpRequestMessage CreateRequest<TRequest>(HttpMethod method, string? methodName, TRequest data); | ||
|
||
public abstract Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default); | ||
|
||
public virtual Task<HttpResponseMessage> SendAsync(HttpMethod method, string? methodName, HttpContent? content, CancellationToken cancellationToken = default) | ||
{ | ||
HttpRequestMessage request = CreateRequest(method, methodName); | ||
request.Content = content; | ||
return SendAsync(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> SendAsync<TRequest>(HttpMethod method, string? methodName, TRequest data, CancellationToken cancellationToken = default) | ||
{ | ||
HttpRequestMessage request = CreateRequest(method, methodName, data); | ||
return SendAsync(request); | ||
} | ||
|
||
public virtual Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string? methodName, TRequest data, CancellationToken cancellationToken = default) | ||
{ | ||
HttpRequestMessage request = CreateRequest(method, methodName, data); | ||
return SendAsync<TResponse>(request, cancellationToken); | ||
} | ||
|
||
public abstract Task SendGrpcAsync(string methodName, CancellationToken cancellationToken = default); | ||
|
||
public abstract Task<TResponse> SendGrpcAsync<TResponse>(string methodName, CancellationToken cancellationToken = default) | ||
where TResponse : IMessage, new(); | ||
|
||
public abstract Task SendGrpcAsync<TRequest>(string methodName, TRequest request, CancellationToken cancellationToken = default) | ||
where TRequest : IMessage; | ||
|
||
public abstract Task<TResponse> SendGrpcAsync<TRequest, TResponse>(string methodName, TRequest request, CancellationToken cancellationToken = default) | ||
where TRequest : IMessage | ||
where TResponse : IMessage, new(); | ||
|
||
public virtual async Task<string> GetStringAsync(string? methodName, CancellationToken cancellationToken = default) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Get, methodName); | ||
HttpResponseMessage content = await SendAsync(request, cancellationToken); | ||
return await content.Content.ReadAsStringAsync(); | ||
} | ||
|
||
public virtual async Task<byte[]> GetByteArrayAsync(string? methodName, CancellationToken cancellationToken) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Get, methodName); | ||
HttpResponseMessage content = await SendAsync(request, cancellationToken); | ||
return await content.Content.ReadAsByteArrayAsync(); | ||
} | ||
|
||
public virtual async Task<Stream> GetStreamAsync(string? methodName, CancellationToken cancellationToken) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Get, methodName); | ||
HttpResponseMessage content = await SendAsync(request, cancellationToken); | ||
return await content.Content.ReadAsStreamAsync(); | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> GetAsync(string? methodName, CancellationToken cancellationToken) | ||
=> SendAsync(HttpMethod.Get, methodName, null, cancellationToken); | ||
|
||
public virtual Task<HttpResponseMessage> GetAsync(string? methodName, Dictionary<string, string> data, CancellationToken cancellationToken) | ||
{ | ||
methodName = GetUrl(methodName ?? String.Empty, data); | ||
return GetAsync(methodName, cancellationToken); | ||
} | ||
|
||
public Task<TResponse> GetAsync<TResponse>(string? methodName, Dictionary<string, string> data, CancellationToken cancellationToken) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Get, methodName, data); | ||
return SendAsync<TResponse>(request, cancellationToken); | ||
} | ||
|
||
protected virtual string GetUrl(string url, Dictionary<string, string> properties) | ||
{ | ||
foreach (var property in properties) | ||
{ | ||
string value = property.Value ?? ""; | ||
|
||
url = !url.Contains("?") ? | ||
$"{url}?{property.Key}={value}" : | ||
$"{url}&{property.Key}={value}"; | ||
} | ||
|
||
return url; | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> PostAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken) | ||
=> SendAsync(HttpMethod.Post, methodName, content, cancellationToken); | ||
|
||
public virtual Task<HttpResponseMessage> PostAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Post, methodName, data); | ||
return SendAsync(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<TResponse> PostAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Post, methodName, data); | ||
return SendAsync<TResponse>(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> PatchAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken) | ||
=> SendAsync(HttpMethod.Patch, methodName, content, cancellationToken); | ||
|
||
public virtual Task<HttpResponseMessage> PatchAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Patch, methodName, data); | ||
return SendAsync(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<TResponse> PatchAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
HttpRequestMessage request = CreateRequest(HttpMethod.Post, methodName, data); | ||
return SendAsync<TResponse>(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> PutAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken) | ||
=> SendAsync(HttpMethod.Put, methodName, content, cancellationToken); | ||
|
||
public virtual Task<HttpResponseMessage> PutAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Put, methodName, data); | ||
return SendAsync(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<TResponse> PutAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Put, methodName, data); | ||
return SendAsync<TResponse>(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<HttpResponseMessage> DeleteAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken) | ||
=> SendAsync(HttpMethod.Delete, methodName, content, cancellationToken); | ||
|
||
public virtual Task<HttpResponseMessage> DeleteAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Delete, methodName, data); | ||
return SendAsync(request, cancellationToken); | ||
} | ||
|
||
public virtual Task<TResponse> DeleteAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken) | ||
{ | ||
var request = CreateRequest(HttpMethod.Delete, methodName, data); | ||
return SendAsync<TResponse>(request, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public class CallerOptions | ||
{ | ||
internal List<CallerRelations> Callers = new(); | ||
|
||
public IServiceCollection Services { get; } | ||
|
||
public JsonSerializerOptions? JsonSerializerOptions { get; } | ||
|
||
public CallerOptions(IServiceCollection services, JsonSerializerOptions? jsonSerializerOptions = null) | ||
{ | ||
Services = services; | ||
JsonSerializerOptions = jsonSerializerOptions; | ||
} | ||
|
||
public void AddCaller(string name, bool isDefault, Func<IServiceProvider, ICallerProvider> func) | ||
{ | ||
if (Callers.Any(c => c.Name == name)) | ||
throw new ArgumentException("The current name already exists, please change the name"); | ||
|
||
Callers.Add(new CallerRelations(name, isDefault, func)); | ||
} | ||
} |
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,28 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
internal class DefaultCallerFactory : ICallerFactory | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
private readonly List<CallerRelations> _callers; | ||
|
||
public DefaultCallerFactory(IServiceProvider serviceProvider, List<CallerRelations> callers) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
_callers = callers; | ||
} | ||
|
||
public ICallerProvider CreateClient() | ||
{ | ||
var caller = _callers.SingleOrDefault(c => c.IsDefault) ?? _callers.FirstOrDefault()!; | ||
return caller.Func.Invoke(_serviceProvider); | ||
} | ||
|
||
public ICallerProvider CreateClient(string name) | ||
{ | ||
var caller = _callers.SingleOrDefault(c => c.Name == name); | ||
if (caller == null) | ||
throw new NotSupportedException($"Please make sure you have used {name} Caller"); | ||
|
||
return caller.Func.Invoke(_serviceProvider); | ||
} | ||
} |
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,8 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public interface ICallerFactory | ||
{ | ||
ICallerProvider CreateClient(); | ||
|
||
ICallerProvider CreateClient(string name); | ||
} |
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,62 @@ | ||
namespace MASA.Utils.Caller.Core; | ||
|
||
public interface ICallerProvider | ||
{ | ||
Task<TResponse> SendAsync<TResponse>(HttpRequestMessage request, CancellationToken cancellationToken = default); | ||
|
||
Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken = default); | ||
|
||
Task<HttpResponseMessage> SendAsync(HttpMethod method, string? methodName, HttpContent? content, CancellationToken cancellationToken = default); | ||
|
||
Task<HttpResponseMessage> SendAsync<TRequest>(HttpMethod method, string? methodName, TRequest data, CancellationToken cancellationToken = default); | ||
|
||
Task<TResponse> SendAsync<TRequest, TResponse>(HttpMethod method, string? methodName, TRequest data, CancellationToken cancellationToken = default); | ||
|
||
Task SendGrpcAsync(string methodName, CancellationToken cancellationToken = default); | ||
|
||
Task<TResponse> SendGrpcAsync<TResponse>(string methodName, CancellationToken cancellationToken = default) | ||
where TResponse : IMessage, new(); | ||
|
||
Task SendGrpcAsync<TRequest>(string methodName, TRequest request, CancellationToken cancellationToken = default) | ||
where TRequest : IMessage; | ||
|
||
Task<TResponse> SendGrpcAsync<TRequest, TResponse>(string methodName, TRequest request, CancellationToken cancellationToken = default) | ||
where TRequest : IMessage | ||
where TResponse : IMessage, new(); | ||
|
||
Task<string> GetStringAsync(string? methodName, CancellationToken cancellationToken = default); | ||
|
||
Task<byte[]> GetByteArrayAsync(string? methodName, CancellationToken cancellationToken); | ||
|
||
Task<Stream> GetStreamAsync(string? methodName, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> GetAsync(string? methodName, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> GetAsync(string? methodName, Dictionary<string, string> data, CancellationToken cancellationToken); | ||
|
||
Task<TResponse> GetAsync<TResponse>(string? methodName, Dictionary<string, string> data, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PostAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PostAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<TResponse> PostAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PatchAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PatchAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<TResponse> PatchAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PutAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> PutAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<TResponse> PutAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> DeleteAsync(string? methodName, HttpContent? content, CancellationToken cancellationToken); | ||
|
||
Task<HttpResponseMessage> DeleteAsync<TRequest>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
|
||
Task<TResponse> DeleteAsync<TRequest, TResponse>(string? methodName, TRequest data, CancellationToken cancellationToken); | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Caller/MASA.Utils.Caller.Core/Internal/CallerRelations.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,17 @@ | ||
namespace MASA.Utils.Caller.Core.Internal; | ||
|
||
internal class CallerRelations | ||
{ | ||
public string Name { get; } = default!; | ||
|
||
public bool IsDefault { get; } | ||
|
||
public Func<IServiceProvider, ICallerProvider> Func { get; } = default!; | ||
|
||
public CallerRelations(string name, bool isDefault, Func<IServiceProvider, ICallerProvider> func) | ||
{ | ||
Name = name; | ||
IsDefault = isDefault; | ||
Func = func; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/Caller/MASA.Utils.Caller.Core/MASA.Utils.Caller.Core.csproj
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,14 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Google.Protobuf" Version="3.19.1" /> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0-rc.2.21480.5" /> | ||
</ItemGroup> | ||
|
||
</Project> |
Oops, something went wrong.