11using MatthiWare . FinancialModelingPrep . Abstractions . Http ;
22using MatthiWare . FinancialModelingPrep . Model ;
33using MatthiWare . FinancialModelingPrep . Model . Error ;
4+ using Microsoft . Extensions . Logging ;
45using System ;
56using System . Collections . Specialized ;
67using System . Net . Http ;
@@ -14,15 +15,19 @@ public class FinancialModelingPrepHttpClient
1415 private readonly HttpClient client ;
1516 private readonly FinancialModelingPrepOptions options ;
1617 private readonly IRequestRateLimiter rateLimiter ;
18+ private readonly ILogger < FinancialModelingPrepHttpClient > logger ;
1719 private readonly JsonSerializerOptions jsonSerializerOptions ;
1820 private const string EmptyArrayResponse = "[ ]" ;
1921 private const string ErrorMessageResponse = "Error Message" ;
2022
21- public FinancialModelingPrepHttpClient ( HttpClient client , FinancialModelingPrepOptions options , IRequestRateLimiter rateLimiter )
23+ public FinancialModelingPrepHttpClient ( HttpClient client , FinancialModelingPrepOptions options ,
24+ IRequestRateLimiter rateLimiter ,
25+ ILogger < FinancialModelingPrepHttpClient > logger )
2226 {
2327 this . client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
2428 this . options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
2529 this . rateLimiter = rateLimiter ?? throw new ArgumentNullException ( nameof ( rateLimiter ) ) ;
30+ this . logger = logger ?? throw new ArgumentNullException ( nameof ( logger ) ) ;
2631 this . jsonSerializerOptions = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
2732
2833 if ( string . IsNullOrWhiteSpace ( this . options . ApiKey ) )
@@ -35,10 +40,15 @@ public async Task<ApiResponse<string>> GetStringAsync(string urlPattern, NameVal
3540 {
3641 try
3742 {
38- await rateLimiter . ThrottleAsync ( ) ;
43+ var ( wasThrottled , totalDelay ) = await rateLimiter . ThrottleAsync ( ) ;
3944
4045 var response = await CallApiAsync ( urlPattern , pathParams , queryString ) ;
4146
47+ if ( wasThrottled )
48+ {
49+ logger . LogDebug ( "FMP API Call was throttled by {throttle} ms" , totalDelay . TotalMilliseconds ) ;
50+ }
51+
4252 if ( response . HasError )
4353 {
4454 return ApiResponse . FromError < string > ( response . Error ) ;
0 commit comments