1414using Titanium . Web . Proxy . Models ;
1515
1616namespace Microsoft . Graph . DeveloperProxy . Plugins . RandomErrors ;
17- internal enum FailMode {
17+ internal enum GraphRandomErrorFailMode {
1818 Throttled ,
1919 Random ,
2020 PassThru
2121}
2222
23- public class RandomErrorConfiguration {
23+ public class GraphRandomErrorConfiguration {
2424 public int Rate { get ; set ; } = 0 ;
2525 public List < int > AllowedErrors { get ; set ; } = new ( ) ;
2626}
2727
28- public class RandomErrorPlugin : BaseProxyPlugin {
28+ public class GraphRandomErrorPlugin : BaseProxyPlugin {
2929 private readonly Option < int ? > _rate ;
3030 private readonly Option < IEnumerable < int > > _allowedErrors ;
31- private readonly RandomErrorConfiguration _configuration = new ( ) ;
31+ private readonly GraphRandomErrorConfiguration _configuration = new ( ) ;
3232
33- public override string Name => nameof ( RandomErrorPlugin ) ;
33+ public override string Name => nameof ( GraphRandomErrorPlugin ) ;
3434
3535 private const int retryAfterInSeconds = 5 ;
3636 private readonly Dictionary < string , HttpStatusCode [ ] > _methodStatusCode = new ( )
@@ -88,7 +88,7 @@ public class RandomErrorPlugin : BaseProxyPlugin {
8888 private readonly Dictionary < string , DateTime > _throttledRequests ;
8989 private readonly Random _random ;
9090
91- public RandomErrorPlugin ( ) {
91+ public GraphRandomErrorPlugin ( ) {
9292 _rate = new Option < int ? > ( "--failure-rate" , "The percentage of requests to graph to respond with failures" ) ;
9393 _rate . AddAlias ( "-f" ) ;
9494 _rate . ArgumentHelpName = "failure rate" ;
@@ -108,28 +108,28 @@ public RandomErrorPlugin() {
108108 }
109109
110110 // uses config to determine if a request should be failed
111- private FailMode ShouldFail ( ProxyRequestArgs e ) {
111+ private GraphRandomErrorFailMode ShouldFail ( ProxyRequestArgs e ) {
112112 var r = e . Session . HttpClient . Request ;
113113 string key = BuildThrottleKey ( r ) ;
114114 if ( _throttledRequests . TryGetValue ( key , out DateTime retryAfterDate ) ) {
115115 if ( retryAfterDate > DateTime . Now ) {
116116 _logger ? . LogRequest ( new [ ] { $ "Calling { r . Url } again before waiting for the Retry-After period.", "Request will be throttled" } , MessageType . Failed , new LoggingContext ( e . Session ) ) ;
117117 // update the retryAfterDate to extend the throttling window to ensure that brute forcing won't succeed.
118118 _throttledRequests [ key ] = retryAfterDate . AddSeconds ( retryAfterInSeconds ) ;
119- return FailMode . Throttled ;
119+ return GraphRandomErrorFailMode . Throttled ;
120120 }
121121 else {
122122 // clean up expired throttled request and ensure that this request is passed through.
123123 _throttledRequests . Remove ( key ) ;
124- return FailMode . PassThru ;
124+ return GraphRandomErrorFailMode . PassThru ;
125125 }
126126 }
127- return _random . Next ( 1 , 100 ) <= _configuration . Rate ? FailMode . Random : FailMode . PassThru ;
127+ return _random . Next ( 1 , 100 ) <= _configuration . Rate ? GraphRandomErrorFailMode . Random : GraphRandomErrorFailMode . PassThru ;
128128 }
129129
130- private void FailResponse ( ProxyRequestArgs e , FailMode failMode ) {
130+ private void FailResponse ( ProxyRequestArgs e , GraphRandomErrorFailMode failMode ) {
131131 HttpStatusCode errorStatus ;
132- if ( failMode == FailMode . Throttled ) {
132+ if ( failMode == GraphRandomErrorFailMode . Throttled ) {
133133 errorStatus = HttpStatusCode . TooManyRequests ;
134134 }
135135 else {
@@ -152,11 +152,11 @@ private void UpdateProxyResponse(ProxyRequestArgs ev, HttpStatusCode errorStatus
152152 headers . Add ( new HttpHeader ( "Retry-After" , retryAfterInSeconds . ToString ( ) ) ) ;
153153 }
154154
155- string body = JsonSerializer . Serialize ( new ErrorResponseBody (
156- new ErrorResponseError {
155+ string body = JsonSerializer . Serialize ( new GraphErrorResponseBody (
156+ new GraphErrorResponseError {
157157 Code = new Regex ( "([A-Z])" ) . Replace ( errorStatus . ToString ( ) , m => { return $ " { m . Groups [ 1 ] } "; } ) . Trim ( ) ,
158158 Message = BuildApiErrorMessage ( request ) ,
159- InnerError = new ErrorResponseInnerError {
159+ InnerError = new GraphErrorResponseInnerError {
160160 RequestId = requestId ,
161161 Date = requestDate
162162 }
@@ -213,7 +213,7 @@ private void OnRequest(object? sender, ProxyRequestArgs e) {
213213 && e . ShouldExecute ( _urlsToWatch ) ) {
214214 var failMode = ShouldFail ( e ) ;
215215
216- if ( failMode == FailMode . PassThru && _configuration . Rate != 100 ) {
216+ if ( failMode == GraphRandomErrorFailMode . PassThru && _configuration . Rate != 100 ) {
217217 return ;
218218 }
219219 FailResponse ( e , failMode ) ;
@@ -223,25 +223,25 @@ private void OnRequest(object? sender, ProxyRequestArgs e) {
223223}
224224
225225
226- internal class ErrorResponseBody {
226+ internal class GraphErrorResponseBody {
227227 [ JsonPropertyName ( "error" ) ]
228- public ErrorResponseError Error { get ; set ; }
228+ public GraphErrorResponseError Error { get ; set ; }
229229
230- public ErrorResponseBody ( ErrorResponseError error ) {
230+ public GraphErrorResponseBody ( GraphErrorResponseError error ) {
231231 Error = error ;
232232 }
233233}
234234
235- internal class ErrorResponseError {
235+ internal class GraphErrorResponseError {
236236 [ JsonPropertyName ( "code" ) ]
237237 public string Code { get ; set ; } = string . Empty ;
238238 [ JsonPropertyName ( "message" ) ]
239239 public string Message { get ; set ; } = string . Empty ;
240240 [ JsonPropertyName ( "innerError" ) ]
241- public ErrorResponseInnerError ? InnerError { get ; set ; }
241+ public GraphErrorResponseInnerError ? InnerError { get ; set ; }
242242}
243243
244- internal class ErrorResponseInnerError {
244+ internal class GraphErrorResponseInnerError {
245245 [ JsonPropertyName ( "request-id" ) ]
246246 public string RequestId { get ; set ; } = string . Empty ;
247247 [ JsonPropertyName ( "date" ) ]
0 commit comments