Skip to content

Commit 5d6cf9d

Browse files
feat: Add idempotency key to session creation requests (#1327)
1 parent 988692f commit 5d6cf9d

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@
6262
import java.net.URL;
6363
import java.util.Map;
6464
import java.util.Optional;
65+
import java.util.UUID;
6566

6667
public class AppiumCommandExecutor extends HttpCommandExecutor {
68+
// https://github.com/appium/appium-base-driver/pull/400
69+
private static final String IDEMPOTENCY_KEY_HEADER = "X-Idempotency-Key";
6770

6871
private final Optional<DriverService> serviceOptional;
6972

@@ -150,18 +153,23 @@ protected void setResponseCodec(ResponseCodec<HttpResponse> codec) {
150153
}
151154

152155
protected HttpClient getClient() {
153-
//noinspection unchecked
154156
return getPrivateFieldValue("client", HttpClient.class);
155157
}
156158

159+
protected HttpClient withRequestsPatchedByIdempotencyKey(HttpClient httpClient) {
160+
return (request) -> {
161+
request.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase());
162+
return httpClient.execute(request);
163+
};
164+
}
165+
157166
private Response createSession(Command command) throws IOException {
158167
if (getCommandCodec() != null) {
159168
throw new SessionNotCreatedException("Session already exists");
160169
}
161170
ProtocolHandshake handshake = new ProtocolHandshake() {
162-
@SuppressWarnings("unchecked")
163-
public Result createSession(HttpClient client, Command command)
164-
throws IOException {
171+
@SuppressWarnings({"unchecked", "UnstableApiUsage"})
172+
public Result createSession(HttpClient client, Command command) throws IOException {
165173
Capabilities desiredCapabilities = (Capabilities) command.getParameters().get("desiredCapabilities");
166174
Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities() : desiredCapabilities;
167175

@@ -182,8 +190,8 @@ public Result createSession(HttpClient client, Command command)
182190
.getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class);
183191
createSessionMethod.setAccessible(true);
184192

185-
Optional<Result> result = (Optional<Result>) createSessionMethod
186-
.invoke(this, client, contentStream, counter.getCount());
193+
Optional<Result> result = (Optional<Result>) createSessionMethod.invoke(this,
194+
withRequestsPatchedByIdempotencyKey(client), contentStream, counter.getCount());
187195

188196
return result.map(result1 -> {
189197
Result toReturn = result.get();

0 commit comments

Comments
 (0)