File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed
src/OpenAI.Net.Integration.Tests Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 11using Microsoft . Extensions . Configuration ;
22using Microsoft . Extensions . DependencyInjection ;
3-
3+ using Polly ;
4+ using Polly . CircuitBreaker ;
5+ using Polly . Extensions . Http ;
6+ using System . Net ;
7+
48namespace OpenAI . Net . Integration . Tests
59{
610 public class BaseTest
@@ -18,7 +22,12 @@ public BaseTest()
1822 Config = new TestConfig ( configuration ) ;
1923
2024 var serviceCollection = new ServiceCollection ( ) ;
21- serviceCollection . AddOpenAIServices ( Config . Apikey ) ;
25+ serviceCollection . AddOpenAIServices ( options => {
26+ options . ApiKey = Config . Apikey ;
27+ } ,
28+ ( httpClientOptions ) => {
29+ httpClientOptions . AddPolicyHandler ( GetRetryPolicy ( ) ) ;
30+ } ) ;
2231 _serviceProvider = serviceCollection . BuildServiceProvider ( ) ;
2332 }
2433
@@ -29,6 +38,16 @@ public IOpenAIService OpenAIService
2938 return _serviceProvider . GetRequiredService < IOpenAIService > ( ) ;
3039 }
3140 }
41+
42+ static IAsyncPolicy < HttpResponseMessage > GetRetryPolicy ( )
43+ {
44+ var policy = Policy < HttpResponseMessage >
45+ . HandleResult ( response => response . StatusCode == HttpStatusCode . TooManyRequests )
46+ . OrTransientHttpError ( ) ;
47+
48+ return policy
49+ . WaitAndRetryAsync ( 3 , retryAttempt => TimeSpan . FromSeconds ( 30 ) ) ;
50+ }
3251 }
3352
3453
Original file line number Diff line number Diff line change 2222 <PackageReference Include =" Microsoft.Extensions.Configuration.EnvironmentVariables" Version =" 6.0.0" />
2323 <PackageReference Include =" Microsoft.Extensions.Configuration.UserSecrets" Version =" 6.0.1" />
2424 <PackageReference Include =" Microsoft.Extensions.DependencyInjection.Abstractions" Version =" 7.0.0" />
25+ <PackageReference Include =" Microsoft.Extensions.Http.Polly" Version =" 6.0.24" />
2526 <PackageReference Include =" Microsoft.NET.Test.Sdk" Version =" 17.3.2" />
2627 <PackageReference Include =" NUnit" Version =" 3.13.3" />
2728 <PackageReference Include =" NUnit3TestAdapter" Version =" 4.2.1" />
2829 <PackageReference Include =" NUnit.Analyzers" Version =" 3.3.0" />
2930 <PackageReference Include =" coverlet.collector" Version =" 3.1.2" />
31+ <PackageReference Include =" Polly" Version =" 8.1.0" />
32+ <PackageReference Include =" Polly.Extensions.Http" Version =" 3.0.0" />
3033 </ItemGroup >
3134
3235 <ItemGroup >
You can’t perform that action at this time.
0 commit comments