From f0e8a87f3addd156c00fbbe57f071bd4100f5ffb Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 16:14:37 +0800 Subject: [PATCH 1/5] feat:restore middleware --- .../EnvironmentMiddleware.cs | 30 +++++++++++++++++++ .../ServiceCollectionExtensions.cs | 1 + .../Masa.Contrib.StackSdks.Auth/_Imports.cs | 1 + 3 files changed, 32 insertions(+) create mode 100644 src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs new file mode 100644 index 000000000..6d6481d45 --- /dev/null +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs @@ -0,0 +1,30 @@ +// Copyright (c) MASA Stack All rights reserved. +// Licensed under the MIT License. See LICENSE.txt in the project root for license information. + +namespace Masa.Contrib.StackSdks.Auth; + +public class EnvironmentMiddleware : ICallerMiddleware +{ + public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDelegate next, CancellationToken cancellationToken = default) + { + + try + { + if (masaHttpContext.RequestMessage.Content != null && masaHttpContext.RequestMessage.Content.Headers.ContentType?.MediaType == "application/json") + { + var body = await masaHttpContext.RequestMessage.Content.ReadFromJsonAsync(new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true + }, cancellationToken: CancellationToken.None); + if (!string.IsNullOrEmpty(body?.Environment)) + { + masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, body?.Environment); + } + } + } + finally + { + await next(); + } + } +} diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs index 32e739e3e..0fda17c98 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/ServiceCollectionExtensions.cs @@ -26,6 +26,7 @@ public static IServiceCollection AddAuthClient(this IServiceCollection services, { callerBuilder .UseHttpClient(builder => builder.BaseAddress = authServiceBaseAddress) + .AddMiddleware() .UseAuthentication(); }, redisOptions); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs index 638242e4f..92d57aaa9 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/_Imports.cs @@ -18,5 +18,6 @@ global using Masa.Contrib.StackSdks.Auth.Model; global using Masa.Contrib.StackSdks.Auth.Service; global using Microsoft.Extensions.Configuration; +global using System.Net.Http.Json; global using System.Text.Json; global using static Masa.Contrib.StackSdks.Auth.Constants; From be3e2b575e231ea32d0cd1254e471b0427e4c763 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 16:21:52 +0800 Subject: [PATCH 2/5] fix:stream read error --- .../Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs index 6d6481d45..ea0f77dc7 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/EnvironmentMiddleware.cs @@ -12,13 +12,14 @@ public async Task HandleAsync(MasaHttpContext masaHttpContext, CallerHandlerDele { if (masaHttpContext.RequestMessage.Content != null && masaHttpContext.RequestMessage.Content.Headers.ContentType?.MediaType == "application/json") { - var body = await masaHttpContext.RequestMessage.Content.ReadFromJsonAsync(new JsonSerializerOptions + var body = await masaHttpContext.RequestMessage.Content.ReadAsStringAsync(CancellationToken.None); + var obj = JsonSerializer.Deserialize(body, new JsonSerializerOptions { PropertyNameCaseInsensitive = true - }, cancellationToken: CancellationToken.None); - if (!string.IsNullOrEmpty(body?.Environment)) + }); + if (!string.IsNullOrEmpty(obj?.Environment)) { - masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, body?.Environment); + masaHttpContext.RequestMessage.Headers.Add(IsolationConsts.ENVIRONMENT, obj?.Environment); } } } From 639c2852bfb45d048682afc7d9bfbaacc754dc85 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 16:50:36 +0800 Subject: [PATCH 3/5] feat:team list add envitonment --- .../Service/ITeamService.cs | 2 +- .../Masa.Contrib.StackSdks.Auth/Service/TeamService.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/ITeamService.cs b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/ITeamService.cs index a8c0b74d5..6d6370d08 100644 --- a/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/ITeamService.cs +++ b/src/BuildingBlocks/StackSdks/Auth/Masa.BuildingBlocks.StackSdks.Auth/Service/ITeamService.cs @@ -7,7 +7,7 @@ public interface ITeamService { Task GetDetailAsync(Guid id); - Task> GetAllAsync(); + Task> GetAllAsync(string environment); Task> GetUserTeamsAsync(); } diff --git a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/TeamService.cs b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/TeamService.cs index 0be09d4ef..d76c887bf 100644 --- a/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/TeamService.cs +++ b/src/Contrib/StackSdks/Masa.Contrib.StackSdks.Auth/Service/TeamService.cs @@ -31,9 +31,9 @@ public async Task> GetListAsync(Guid userId = default) return await _caller.GetAsync>(requestUri) ?? new(); } - public async Task> GetAllAsync() + public async Task> GetAllAsync(string environment) { - var requestUri = $"{_party}list"; + var requestUri = $"{_party}list?{IsolationConsts.ENVIRONMENT}={environment}"; return await _caller.GetAsync>(requestUri) ?? new(); } From 8823bf58a00523570de16be2d80e83e99ff59ca1 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 17:45:10 +0800 Subject: [PATCH 4/5] fix:test error --- .../Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs index eae95720b..aa1074a9d 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs @@ -30,7 +30,7 @@ public async Task TestGetAllAsync() caller.Setup(provider => provider.GetAsync>(requestUri, default)).ReturnsAsync(data).Verifiable(); var userContext = new Mock(); var teamService = new Mock(caller.Object, userContext.Object); - var result = await teamService.Object.GetAllAsync(); + var result = await teamService.Object.GetAllAsync(""); caller.Verify(provider => provider.GetAsync>(requestUri, default), Times.Once); Assert.IsTrue(result is not null); } From 87eada6515140c8b92a9cea72f1cfdfae95eaa65 Mon Sep 17 00:00:00 2001 From: Mayue Date: Mon, 10 Jul 2023 17:45:10 +0800 Subject: [PATCH 5/5] fix:test error --- .../Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs | 4 ++-- .../Tests/Masa.Contrib.StackSdks.Auth.Tests/_Imports.cs | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs index eae95720b..9dd6bed8b 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/TeamServiceTest.cs @@ -25,12 +25,12 @@ public async Task TestGetDetailAsync() public async Task TestGetAllAsync() { var data = new List(); - var requestUri = $"api/team/list"; + var requestUri = $"api/team/list?{IsolationConsts.ENVIRONMENT}=test"; var caller = new Mock(); caller.Setup(provider => provider.GetAsync>(requestUri, default)).ReturnsAsync(data).Verifiable(); var userContext = new Mock(); var teamService = new Mock(caller.Object, userContext.Object); - var result = await teamService.Object.GetAllAsync(); + var result = await teamService.Object.GetAllAsync("test"); caller.Verify(provider => provider.GetAsync>(requestUri, default), Times.Once); Assert.IsTrue(result is not null); } diff --git a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/_Imports.cs b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/_Imports.cs index 9bd85ac21..c04e59f4f 100644 --- a/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/_Imports.cs +++ b/src/Contrib/StackSdks/Tests/Masa.Contrib.StackSdks.Auth.Tests/_Imports.cs @@ -5,14 +5,14 @@ global using Masa.BuildingBlocks.Caching; global using Masa.BuildingBlocks.Service.Caller; global using Masa.BuildingBlocks.StackSdks.Auth; +global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts; global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Enum; global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Model; +global using Masa.BuildingBlocks.StackSdks.Isolation; global using Masa.Contrib.StackSdks.Auth.Service; global using Microsoft.Extensions.DependencyInjection; global using Microsoft.VisualStudio.TestTools.UnitTesting; global using Moq; -global using System.Text.Json; -global using IdentityModel.Client; global using Moq.Protected; global using System.Net; -global using Masa.BuildingBlocks.StackSdks.Auth.Contracts.Consts; +global using System.Text.Json;