1- using MatthiWare . FinancialModelingPrep . Model ;
1+ using MatthiWare . FinancialModelingPrep . Abstractions . Http ;
2+ using MatthiWare . FinancialModelingPrep . Model ;
23using MatthiWare . FinancialModelingPrep . Model . Error ;
34using System ;
45using System . Collections . Specialized ;
56using System . Net . Http ;
67using System . Text . Json ;
7- using System . Threading ;
88using System . Threading . Tasks ;
99
1010namespace MatthiWare . FinancialModelingPrep . Core . Http
@@ -13,17 +13,17 @@ public class FinancialModelingPrepHttpClient
1313 {
1414 private readonly HttpClient client ;
1515 private readonly FinancialModelingPrepOptions options ;
16+ private readonly IRequestRateLimiter rateLimiter ;
1617 private readonly JsonSerializerOptions jsonSerializerOptions ;
1718 private const string EmptyArrayResponse = "[ ]" ;
1819 private const string ErrorMessageResponse = "Error Message" ;
19- private readonly SemaphoreSlim throttler ;
2020
21- public FinancialModelingPrepHttpClient ( HttpClient client , FinancialModelingPrepOptions options )
21+ public FinancialModelingPrepHttpClient ( HttpClient client , FinancialModelingPrepOptions options , IRequestRateLimiter rateLimiter )
2222 {
2323 this . client = client ?? throw new ArgumentNullException ( nameof ( client ) ) ;
2424 this . options = options ?? throw new ArgumentNullException ( nameof ( options ) ) ;
25+ this . rateLimiter = rateLimiter ?? throw new ArgumentNullException ( nameof ( rateLimiter ) ) ;
2526 this . jsonSerializerOptions = new JsonSerializerOptions ( JsonSerializerDefaults . Web ) ;
26- this . throttler = new SemaphoreSlim ( options . MaxRequestLimit ) ;
2727
2828 if ( string . IsNullOrWhiteSpace ( this . options . ApiKey ) )
2929 {
@@ -33,30 +33,35 @@ public FinancialModelingPrepHttpClient(HttpClient client, FinancialModelingPrepO
3333
3434 public async Task < ApiResponse < string > > GetStringAsync ( string urlPattern , NameValueCollection pathParams , QueryStringBuilder queryString )
3535 {
36- await throttler . WaitAsync ( ) ;
36+ try
37+ {
38+ await rateLimiter . ThrottleAsync ( ) ;
3739
38- var response = await CallApiAsync ( urlPattern , pathParams , queryString ) ;
40+ var response = await CallApiAsync ( urlPattern , pathParams , queryString ) ;
3941
40- throttler . Release ( ) ;
42+ if ( response . HasError )
43+ {
44+ return ApiResponse . FromError < string > ( response . Error ) ;
45+ }
4146
42- if ( response . HasError )
43- {
44- return ApiResponse . FromError < string > ( response . Error ) ;
45- }
47+ if ( response . Data . Contains ( ErrorMessageResponse ) )
48+ {
49+ var errorData = JsonSerializer . Deserialize < ErrorResponse > ( response . Data ) ;
4650
47- if ( response . Data . Contains ( ErrorMessageResponse ) )
48- {
49- var errorData = JsonSerializer . Deserialize < ErrorResponse > ( response . Data ) ;
51+ return ApiResponse . FromError < string > ( errorData . ErrorMessage ) ;
52+ }
5053
51- return ApiResponse . FromError < string > ( errorData . ErrorMessage ) ;
52- }
54+ if ( response . Data . Equals ( EmptyArrayResponse , StringComparison . OrdinalIgnoreCase ) )
55+ {
56+ return ApiResponse . FromError < string > ( "Invalid parameters" ) ;
57+ }
5358
54- if ( response . Data . Equals ( EmptyArrayResponse , StringComparison . OrdinalIgnoreCase ) )
59+ return ApiResponse . FromSucces ( response . Data ) ;
60+ }
61+ finally
5562 {
56- return ApiResponse . FromError < string > ( "Invalid parameters" ) ;
63+ rateLimiter . ReleaseThrottle ( ) ;
5764 }
58-
59- return ApiResponse . FromSucces ( response . Data ) ;
6065 }
6166
6267 public async Task < ApiResponse < T > > GetJsonAsync < T > ( string urlPattern , NameValueCollection pathParams , QueryStringBuilder queryString )
0 commit comments