Skip to content

Commit a8abaf8

Browse files
committed
refactor:add websitemessageservice method and httpClientAuthorizationDelegatingHandler
1 parent 5b0260a commit a8abaf8

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
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

+13-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public async Task DeleteAsync(Guid id)
3131
public async Task<List<WebsiteMessageChannelModel>> GetChannelListAsync()
3232
{
3333
var requestUri = $"{_party}/GetChannelList";
34-
return await _callerProvider.GetAsync<List<WebsiteMessageChannelModel>>(requestUri)??new();
34+
return await _callerProvider.GetAsync<List<WebsiteMessageChannelModel>>(requestUri) ?? new();
3535
}
3636

3737
public async Task<PaginatedListModel<WebsiteMessageModel>> GetListAsync(GetWebsiteMessageModel options)
@@ -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

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, s
1212
throw new ArgumentNullException(nameof(mcServiceBaseAddress));
1313
}
1414

15+
services.AddSingleton<IRequestMessage, JsonRequestMessage>();
1516
return services.AddMcClient(callerOptions =>
1617
{
1718
callerOptions.UseHttpClient(builder =>
1819
{
1920
builder.Name = DEFAULT_CLIENT_NAME;
2021
builder.Configure = opt => opt.BaseAddress = new Uri(mcServiceBaseAddress);
21-
});
22+
}).AddHttpMessageHandler<HttpClientAuthorizationDelegatingHandler>();
2223
});
2324
}
2425

@@ -29,9 +30,11 @@ public static IServiceCollection AddMcClient(this IServiceCollection services, A
2930
if (services.Any(service => service.ServiceType == typeof(IMcClient)))
3031
return services;
3132

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

34-
services.AddSingleton<IMcClient>(serviceProvider =>
37+
services.AddScoped<IMcClient>(serviceProvider =>
3538
{
3639
var callProvider = serviceProvider.GetRequiredService<ICallerFactory>().CreateClient(DEFAULT_CLIENT_NAME);
3740
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)