22// Licensed under the MIT License.
33
44using Microsoft . Extensions . Configuration ;
5+ using Microsoft . Extensions . Logging ;
56using Microsoft . DevProxy . Abstractions ;
67using System . Net ;
78using System . Text . Json ;
@@ -49,6 +50,10 @@ public class RateLimitingPlugin : BaseProxyPlugin
4950 private DateTime _resetTime = DateTime . MinValue ;
5051 private RateLimitingCustomResponseLoader ? _loader = null ;
5152
53+ public RateLimitingPlugin ( IPluginEvents pluginEvents , IProxyContext context , ILogger logger , ISet < UrlToWatch > urlsToWatch , IConfigurationSection ? configSection = null ) : base ( pluginEvents , context , logger , urlsToWatch , configSection )
54+ {
55+ }
56+
5257 private ThrottlingInfo ShouldThrottle ( Request request , string throttlingKey )
5358 {
5459 var throttleKeyForRequest = BuildThrottleKey ( request ) ;
@@ -123,31 +128,28 @@ private string BuildThrottleKey(Request r)
123128 }
124129 }
125130
126- public override void Register ( IPluginEvents pluginEvents ,
127- IProxyContext context ,
128- ISet < UrlToWatch > urlsToWatch ,
129- IConfigurationSection ? configSection = null )
131+ public override void Register ( )
130132 {
131- base . Register ( pluginEvents , context , urlsToWatch , configSection ) ;
133+ base . Register ( ) ;
132134
133- configSection ? . Bind ( _configuration ) ;
135+ ConfigSection ? . Bind ( _configuration ) ;
134136 if ( _configuration . WhenLimitExceeded == RateLimitResponseWhenLimitExceeded . Custom )
135137 {
136- _configuration . CustomResponseFile = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . CustomResponseFile ) , Path . GetDirectoryName ( context . Configuration ? . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
137- _loader = new RateLimitingCustomResponseLoader ( _logger ! , _configuration ) ;
138+ _configuration . CustomResponseFile = Path . GetFullPath ( ProxyUtils . ReplacePathTokens ( _configuration . CustomResponseFile ) , Path . GetDirectoryName ( Context . Configuration . ConfigFile ?? string . Empty ) ?? string . Empty ) ;
139+ _loader = new RateLimitingCustomResponseLoader ( Logger , _configuration ) ;
138140 // load the responses from the configured mocks file
139141 _loader . InitResponsesWatcher ( ) ;
140142 }
141143
142- pluginEvents . BeforeRequest += OnRequest ;
143- pluginEvents . BeforeResponse += OnResponse ;
144+ PluginEvents . BeforeRequest += OnRequest ;
145+ PluginEvents . BeforeResponse += OnResponse ;
144146 }
145147
146148 // add rate limiting headers to the response from the API
147149 private Task OnResponse ( object ? sender , ProxyResponseArgs e )
148150 {
149- if ( _urlsToWatch is null ||
150- ! e . HasRequestUrlMatch ( _urlsToWatch ) )
151+ if ( UrlsToWatch is null ||
152+ ! e . HasRequestUrlMatch ( UrlsToWatch ) )
151153 {
152154 return Task . CompletedTask ;
153155 }
@@ -161,8 +163,8 @@ private Task OnRequest(object? sender, ProxyRequestArgs e)
161163 var session = e . Session ;
162164 var state = e . ResponseState ;
163165 if ( e . ResponseState . HasBeenSet ||
164- _urlsToWatch is null ||
165- ! e . ShouldExecute ( _urlsToWatch ) )
166+ UrlsToWatch is null ||
167+ ! e . ShouldExecute ( UrlsToWatch ) )
166168 {
167169 return Task . CompletedTask ;
168170 }
@@ -191,7 +193,7 @@ _urlsToWatch is null ||
191193 _resourcesRemaining = 0 ;
192194 var request = e . Session . HttpClient . Request ;
193195
194- _logger ? . LogRequest ( [ $ "Exceeded resource limit when calling { request . Url } .", "Request will be throttled" ] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
196+ Logger . LogRequest ( [ $ "Exceeded resource limit when calling { request . Url } .", "Request will be throttled" ] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
195197 if ( _configuration . WhenLimitExceeded == RateLimitResponseWhenLimitExceeded . Throttle )
196198 {
197199 if ( ! e . GlobalData . ContainsKey ( RetryAfterPlugin . ThrottledRequestsKey ) )
@@ -250,7 +252,7 @@ _urlsToWatch is null ||
250252 }
251253 else
252254 {
253- _logger ? . LogRequest ( [ $ "Custom behavior not set. { _configuration . CustomResponseFile } not found."] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
255+ Logger . LogRequest ( [ $ "Custom behavior not set. { _configuration . CustomResponseFile } not found."] , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
254256 }
255257 }
256258 }
0 commit comments