From e7e184339e2f533435d4585bf84c8445f4604aba Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Tue, 31 Mar 2020 13:04:34 +0200 Subject: [PATCH 1/2] feat: Add idempotency key to session creation requests --- .../remote/AppiumCommandExecutor.java | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index 3f094ff53..a21d85fdd 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -62,8 +62,11 @@ import java.net.URL; import java.util.Map; import java.util.Optional; +import java.util.UUID; public class AppiumCommandExecutor extends HttpCommandExecutor { + // https://github.com/appium/appium-base-driver/pull/400 + private static final String IDEMPOTENCY_KEY_HEADER = "X-Idempotency-Key"; private final Optional serviceOptional; @@ -150,18 +153,23 @@ protected void setResponseCodec(ResponseCodec codec) { } protected HttpClient getClient() { - //noinspection unchecked return getPrivateFieldValue("client", HttpClient.class); } + protected HttpClient withRequestsPatchedByIdempotencyKey(HttpClient httpClient) { + return (request) -> { + request.setHeader(IDEMPOTENCY_KEY_HEADER, UUID.randomUUID().toString().toLowerCase()); + return httpClient.execute(request); + }; + } + private Response createSession(Command command) throws IOException { if (getCommandCodec() != null) { throw new SessionNotCreatedException("Session already exists"); } ProtocolHandshake handshake = new ProtocolHandshake() { - @SuppressWarnings("unchecked") - public Result createSession(HttpClient client, Command command) - throws IOException { + @SuppressWarnings({"unchecked", "UnstableApiUsage"}) + public Result createSession(HttpClient client, Command command) throws IOException { Capabilities desiredCapabilities = (Capabilities) command.getParameters().get("desiredCapabilities"); Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities() : desiredCapabilities; @@ -183,7 +191,8 @@ public Result createSession(HttpClient client, Command command) createSessionMethod.setAccessible(true); Optional result = (Optional) createSessionMethod - .invoke(this, client, contentStream, counter.getCount()); + .invoke(this, withRequestsPatchedByIdempotencyKey(client), + contentStream, counter.getCount()); return result.map(result1 -> { Result toReturn = result.get(); From e3ca704a15415aac010698a928b6382828abd1af Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 1 Apr 2020 10:28:27 +0200 Subject: [PATCH 2/2] Try with patched beta --- .../io/appium/java_client/remote/AppiumCommandExecutor.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java index a21d85fdd..061a291f2 100644 --- a/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java +++ b/src/main/java/io/appium/java_client/remote/AppiumCommandExecutor.java @@ -190,9 +190,8 @@ public Result createSession(HttpClient client, Command command) throws IOExcepti .getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class); createSessionMethod.setAccessible(true); - Optional result = (Optional) createSessionMethod - .invoke(this, withRequestsPatchedByIdempotencyKey(client), - contentStream, counter.getCount()); + Optional result = (Optional) createSessionMethod.invoke(this, + withRequestsPatchedByIdempotencyKey(client), contentStream, counter.getCount()); return result.map(result1 -> { Result toReturn = result.get();