Skip to content

Commit 54a201f

Browse files
authored
Merge pull request #21213 from yuxl01/feat-#21205
Add `ProxyHttpClientPreSendActions` to `AbpHttpClientOptions`.
2 parents 13d61bb + ae59802 commit 54a201f

File tree

6 files changed

+50
-1
lines changed

6 files changed

+50
-1
lines changed

framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/AbpHttpClientOptions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Net.Http;
4+
using Volo.Abp.Http.Client.ClientProxying;
35
using Volo.Abp.Http.Client.Proxying;
46

57
namespace Volo.Abp.Http.Client;
@@ -8,8 +10,17 @@ public class AbpHttpClientOptions
810
{
911
public Dictionary<Type, HttpClientProxyConfig> HttpClientProxies { get; set; }
1012

13+
public Dictionary<string, List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>> ProxyHttpClientPreSendActions { get; }
14+
1115
public AbpHttpClientOptions()
1216
{
1317
HttpClientProxies = new Dictionary<Type, HttpClientProxyConfig>();
18+
ProxyHttpClientPreSendActions = new Dictionary<string, List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>>();
19+
}
20+
21+
public AbpHttpClientOptions AddPreSendAction(string remoteServiceName, Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient> action)
22+
{
23+
ProxyHttpClientPreSendActions.GetOrAdd(remoteServiceName, () => new List<Action<HttpClientProxyConfig, ClientProxyRequestContext, HttpClient>>()).Add(action);
24+
return this;
1425
}
1526
}

framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/ClientProxying/ClientProxyBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ await ClientAuthenticator.Authenticate(
146146
HttpResponseMessage response;
147147
try
148148
{
149+
foreach (var preSendAction in ClientOptions.Value.ProxyHttpClientPreSendActions.Where(x => x.Key == clientConfig.RemoteServiceName).SelectMany(x => x.Value))
150+
{
151+
preSendAction(clientConfig, requestContext, client);
152+
}
153+
149154
response = await client.SendAsync(
150155
requestMessage,
151156
HttpCompletionOption.ResponseHeadersRead /*this will buffer only the headers, the content will be used as a stream*/,

framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/AbpHttpClientTestModule.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Net.Http;
24
using Microsoft.Extensions.DependencyInjection;
35
using Volo.Abp.AspNetCore.Mvc;
46
using Volo.Abp.Http.Client;
57
using Volo.Abp.Http.Client.ClientProxying;
8+
using Volo.Abp.Http.Client.Proxying;
69
using Volo.Abp.Http.DynamicProxying;
710
using Volo.Abp.Http.Localization;
811
using Volo.Abp.Localization;
@@ -60,5 +63,16 @@ public override void ConfigureServices(ServiceConfigurationContext context)
6063
options.FormDataConverts.Add(typeof(List<GetParamsNameValue>), typeof(TestObjectToFormData));
6164
options.PathConverts.Add(typeof(int), typeof(TestObjectToPath));
6265
});
66+
67+
Configure<AbpHttpClientOptions>(options =>
68+
{
69+
options.AddPreSendAction("Default", (_, requestContext, httpclient) =>
70+
{
71+
if (requestContext.Action.Name.Equals("TimeOutRequestAsync"))
72+
{
73+
httpclient.Timeout = TimeSpan.FromMilliseconds(1);
74+
}
75+
});
76+
});
6377
}
6478
}

framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/IRegularTestController.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ public interface IRegularTestController
4343
Task<int> DeleteByIdAsync(int id);
4444

4545
Task<string> AbortRequestAsync(CancellationToken cancellationToken = default);
46+
47+
Task<string> TimeOutRequestAsync();
48+
4649
}

framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestController.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,14 @@ public async Task<string> AbortRequestAsync(CancellationToken cancellationToken
152152
await Task.Delay(100, cancellationToken);
153153
return "AbortRequestAsync";
154154
}
155+
156+
[HttpGet]
157+
[Route("timeout-request")]
158+
public async Task<string> TimeOutRequestAsync()
159+
{
160+
await Task.Delay(100);
161+
return "TimeOutRequestAsync";
162+
}
155163
}
156164

157165
public class Car

framework/test/Volo.Abp.Http.Client.Tests/Volo/Abp/Http/DynamicProxying/RegularTestControllerClientProxy_Tests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Net.Http;
23
using System.Threading;
34
using System.Threading.Tasks;
45
using Microsoft.Extensions.DependencyInjection;
@@ -187,4 +188,11 @@ public async Task AbortRequestAsync()
187188
var exception = await Assert.ThrowsAsync<AbpRemoteCallException>(async () => await _controller.AbortRequestAsync(cts.Token));
188189
exception.InnerException.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
189190
}
191+
192+
[Fact]
193+
public async Task TimeOutRequestAsync()
194+
{
195+
var exception = await Assert.ThrowsAsync<HttpRequestException>(async () => await _controller.TimeOutRequestAsync());
196+
exception.InnerException.InnerException.Message.ShouldBe("The client aborted the request.");
197+
}
190198
}

0 commit comments

Comments
 (0)