Skip to content

Commit

Permalink
add caller
Browse files Browse the repository at this point in the history
  • Loading branch information
zhenlei520 authored and blazor-component committed Nov 12, 2021
1 parent 4516689 commit e4428fc
Show file tree
Hide file tree
Showing 20 changed files with 709 additions and 4 deletions.
32 changes: 28 additions & 4 deletions MASA.Utils.sln
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Extensions.Depen
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Data.EntityFrameworkCore", "src\Data\MASA.Utils.Data.EntityFrameworkCore\MASA.Utils.Data.EntityFrameworkCore.csproj", "{66CE7FFE-E2DD-43EA-92DB-82A46D1B2215}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MASA.Utils.Caching.Core", "src\Caching\MASA.Utils.Caching.Core\MASA.Utils.Caching.Core.csproj", "{6DBF3335-F4FB-46DE-9FBB-0FA13FB8E822}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caching.Core", "src\Caching\MASA.Utils.Caching.Core\MASA.Utils.Caching.Core.csproj", "{6DBF3335-F4FB-46DE-9FBB-0FA13FB8E822}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MASA.Utils.Caching.DistributedMemory", "src\Caching\MASA.Utils.Caching.DistributedMemory\MASA.Utils.Caching.DistributedMemory.csproj", "{582FB57C-0B6C-4B6C-9766-5E5D8998C0BE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caching.DistributedMemory", "src\Caching\MASA.Utils.Caching.DistributedMemory\MASA.Utils.Caching.DistributedMemory.csproj", "{582FB57C-0B6C-4B6C-9766-5E5D8998C0BE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MASA.Utils.Caching.Memory", "src\Caching\MASA.Utils.Caching.Memory\MASA.Utils.Caching.Memory.csproj", "{6AA5B8CB-E5A7-44E6-8E67-554CB235E6C2}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caching.Memory", "src\Caching\MASA.Utils.Caching.Memory\MASA.Utils.Caching.Memory.csproj", "{6AA5B8CB-E5A7-44E6-8E67-554CB235E6C2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MASA.Utils.Caching.Redis", "src\Caching\MASA.Utils.Caching.Redis\MASA.Utils.Caching.Redis.csproj", "{60473B6E-3823-4ADE-A960-53B3F88C9C23}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caching.Redis", "src\Caching\MASA.Utils.Caching.Redis\MASA.Utils.Caching.Redis.csproj", "{60473B6E-3823-4ADE-A960-53B3F88C9C23}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Caller", "Caller", "{72E67141-9CD8-48A9-B27B-F0C924FD4A12}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caller.Core", "src\Caller\MASA.Utils.Caller.Core\MASA.Utils.Caller.Core.csproj", "{3BEAA614-6327-485F-A81D-3937EC7E16B5}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caller.HttpClient", "src\Caller\MASA.Utils.Caller.HttpClient\MASA.Utils.Caller.HttpClient.csproj", "{C19B010B-AA4D-46C0-A229-034C3F36C266}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MASA.Utils.Caller.DaprClient", "src\Caller\MASA.Utils.Caller.DaprClient\MASA.Utils.Caller.DaprClient.csproj", "{68B51DE3-FEA2-4704-B1D0-B9924F754A76}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -101,6 +109,18 @@ Global
{60473B6E-3823-4ADE-A960-53B3F88C9C23}.Debug|Any CPU.Build.0 = Debug|Any CPU
{60473B6E-3823-4ADE-A960-53B3F88C9C23}.Release|Any CPU.ActiveCfg = Release|Any CPU
{60473B6E-3823-4ADE-A960-53B3F88C9C23}.Release|Any CPU.Build.0 = Release|Any CPU
{3BEAA614-6327-485F-A81D-3937EC7E16B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BEAA614-6327-485F-A81D-3937EC7E16B5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BEAA614-6327-485F-A81D-3937EC7E16B5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BEAA614-6327-485F-A81D-3937EC7E16B5}.Release|Any CPU.Build.0 = Release|Any CPU
{C19B010B-AA4D-46C0-A229-034C3F36C266}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C19B010B-AA4D-46C0-A229-034C3F36C266}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C19B010B-AA4D-46C0-A229-034C3F36C266}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C19B010B-AA4D-46C0-A229-034C3F36C266}.Release|Any CPU.Build.0 = Release|Any CPU
{68B51DE3-FEA2-4704-B1D0-B9924F754A76}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{68B51DE3-FEA2-4704-B1D0-B9924F754A76}.Debug|Any CPU.Build.0 = Debug|Any CPU
{68B51DE3-FEA2-4704-B1D0-B9924F754A76}.Release|Any CPU.ActiveCfg = Release|Any CPU
{68B51DE3-FEA2-4704-B1D0-B9924F754A76}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -123,6 +143,10 @@ Global
{582FB57C-0B6C-4B6C-9766-5E5D8998C0BE} = {A7C058C9-B4A3-472A-A82B-FC4A0A12AC9D}
{6AA5B8CB-E5A7-44E6-8E67-554CB235E6C2} = {A7C058C9-B4A3-472A-A82B-FC4A0A12AC9D}
{60473B6E-3823-4ADE-A960-53B3F88C9C23} = {A7C058C9-B4A3-472A-A82B-FC4A0A12AC9D}
{72E67141-9CD8-48A9-B27B-F0C924FD4A12} = {D956582F-4071-47E6-A8E7-4C5A83770045}
{3BEAA614-6327-485F-A81D-3937EC7E16B5} = {72E67141-9CD8-48A9-B27B-F0C924FD4A12}
{C19B010B-AA4D-46C0-A229-034C3F36C266} = {72E67141-9CD8-48A9-B27B-F0C924FD4A12}
{68B51DE3-FEA2-4704-B1D0-B9924F754A76} = {72E67141-9CD8-48A9-B27B-F0C924FD4A12}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {D7DAA0E6-098F-4B18-8775-64FDA96F1FF0}
Expand Down
153 changes: 153 additions & 0 deletions src/Caller/MASA.Utils.Caller.Core/AbstractCallerProvider.cs
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);
}
}
24 changes: 24 additions & 0 deletions src/Caller/MASA.Utils.Caller.Core/CallerOptions.cs
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));
}
}
28 changes: 28 additions & 0 deletions src/Caller/MASA.Utils.Caller.Core/DefaultCallerFactory.cs
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);
}
}
8 changes: 8 additions & 0 deletions src/Caller/MASA.Utils.Caller.Core/ICallerFactory.cs
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);
}
62 changes: 62 additions & 0 deletions src/Caller/MASA.Utils.Caller.Core/ICallerProvider.cs
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 src/Caller/MASA.Utils.Caller.Core/Internal/CallerRelations.cs
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 src/Caller/MASA.Utils.Caller.Core/MASA.Utils.Caller.Core.csproj
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>
Loading

0 comments on commit e4428fc

Please sign in to comment.