Skip to content

Commit 6bf7c12

Browse files
authored
refactor:add websitemessageservice method and httpClientAuthorization… (#143)
* refactor:add websitemessageservice method and httpClientAuthorizationDelegatingHandler * refactor:adjust sendnotification * refactor:mcClient add NotificationService * Revert "refactor:add websitemessageservice method and httpClientAuthorizationDelegatingHandler" This reverts commit a8abaf8. * refactor:reduction
1 parent 8486809 commit 6bf7c12

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.BasicAbility.Mc;
5+
6+
public class HttpClientAuthorizationDelegatingHandler : DelegatingHandler
7+
{
8+
private readonly IHttpContextAccessor _httpContextAccessor;
9+
10+
public HttpClientAuthorizationDelegatingHandler(IHttpContextAccessor httpContextAccessor)
11+
{
12+
_httpContextAccessor = httpContextAccessor;
13+
}
14+
15+
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
16+
{
17+
if (_httpContextAccessor.HttpContext != null)
18+
{
19+
var accessToken = await _httpContextAccessor.HttpContext.GetTokenAsync("access_token");
20+
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
21+
}
22+
return await base.SendAsync(request, cancellationToken);
23+
}
24+
}

src/BasicAbility/Masa.Contrib.BasicAbility.Mc/Service/WebsiteMessageService.cs

+12
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,16 @@ public async Task SetAllReadAsync(ReadAllWebsiteMessageModel options)
5757
var requestUri = $"{_party}/SetAllRead";
5858
await _callerProvider.PostAsync(requestUri, options);
5959
}
60+
61+
public async Task SendCheckNotificationAsync()
62+
{
63+
var requestUri = $"{_party}/SendCheckNotification";
64+
await _callerProvider.PostAsync(requestUri, null);
65+
}
66+
67+
public async Task SendGetNotificationAsync(List<string> userIds)
68+
{
69+
var requestUri = $"{_party}/SendGetNotification";
70+
await _callerProvider.PostAsync(requestUri, userIds);
71+
}
6072
}

src/BasicAbility/Masa.Contrib.BasicAbility.Mc/ServiceCollectionExtensions.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, s
1818
{
1919
builder.Name = DEFAULT_CLIENT_NAME;
2020
builder.Configure = opt => opt.BaseAddress = new Uri(mcServiceBaseAddress);
21-
});
21+
}).AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>();
2222
});
2323
}
2424

@@ -29,9 +29,11 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, A
2929
if (services.Any(service => service.ServiceType == typeof(IMcClient)))
3030
return services;
3131

32-
services.AddCaller(callerOptions.Invoke);
32+
services.AddHttpContextAccessor();
33+
services.AddScoped<HttpClientAuthorizationDelegatingHandler>();
34+
services.AddCaller(callerOptions);
3335

34-
services.AddSingleton<IMcClient>(serviceProvider =>
36+
services.AddScoped<IMcClient>(serviceProvider =>
3537
{
3638
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
3739
var mcCaching = new McClient(callProvider);

src/BasicAbility/Masa.Contrib.BasicAbility.Mc/_Imports.cs

+2
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@
1414
global using System.Diagnostics.CodeAnalysis;
1515
global using System.Globalization;
1616
global using System.Reflection;
17+
global using Microsoft.AspNetCore.Http;
18+
global using Microsoft.AspNetCore.Authentication;

0 commit comments

Comments
 (0)