-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from microsoftgraph/dev
release 1.0.4
- Loading branch information
Showing
7 changed files
with
108 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/microsoft/graph/httpcore/ChaosHttpHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.microsoft.graph.httpcore; | ||
|
||
import java.io.IOException; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
import com.microsoft.graph.httpcore.middlewareoption.MiddlewareType; | ||
import com.microsoft.graph.httpcore.middlewareoption.TelemetryOptions; | ||
|
||
import okhttp3.Interceptor; | ||
import okhttp3.MediaType; | ||
import okhttp3.Protocol; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import okhttp3.ResponseBody; | ||
|
||
/** | ||
* DO NOT USE IN PRODUCTION | ||
* interceptor that randomly fails the responses for unit testing purposes | ||
*/ | ||
public class ChaosHttpHandler implements Interceptor { | ||
public final MiddlewareType MIDDLEWARE_TYPE = MiddlewareType.RETRY; | ||
/* | ||
* constant string being used | ||
*/ | ||
private final String RETRY_AFTER = "Retry-After"; | ||
/** | ||
* Denominator for the failure rate (i.e. 1/X) | ||
*/ | ||
private final Integer failureRate = 3; | ||
/** | ||
* default value to return on retry after | ||
*/ | ||
private final String retryAfterValue = "10"; | ||
/** | ||
* body to respond on failed requests | ||
*/ | ||
private final String responseBody = "{\"error\": {\"code\": \"TooManyRequests\",\"innerError\": {\"code\": \"429\",\"date\": \"2020-08-18T12:51:51\",\"message\": \"Please retry after\",\"request-id\": \"94fb3b52-452a-4535-a601-69e0a90e3aa2\",\"status\": \"429\"},\"message\": \"Please retry again later.\"}}"; | ||
public static final int MSClientErrorCodeTooManyRequests = 429; | ||
|
||
@Override | ||
public Response intercept(Chain chain) throws IOException { | ||
Request request = chain.request(); | ||
|
||
if(request.tag(TelemetryOptions.class) == null) | ||
request = request.newBuilder().tag(TelemetryOptions.class, new TelemetryOptions()).build(); | ||
request.tag(TelemetryOptions.class).setFeatureUsage(TelemetryOptions.RETRY_HANDLER_ENABLED_FLAG); | ||
|
||
final Integer dice = ThreadLocalRandom.current().nextInt(1, Integer.MAX_VALUE); | ||
|
||
if(dice % failureRate == 0) { | ||
return new Response | ||
.Builder() | ||
.request(request) | ||
.protocol(Protocol.HTTP_1_1) | ||
.code(MSClientErrorCodeTooManyRequests) | ||
.message("Too Many Requests") | ||
.addHeader(RETRY_AFTER, retryAfterValue) | ||
.body(ResponseBody.create(MediaType.get("application/json"), responseBody)) | ||
.build(); | ||
} else { | ||
return chain.proceed(request); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters