feat: Add idempotency key to session creation requests by mykola-mokhnach · Pull Request #1327 · appium/java-client
Expand Up
@@ -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<DriverService> serviceOptional;
Expand Down Expand Up @@ -150,18 +153,23 @@ protected void setResponseCodec(ResponseCodec<HttpResponse> 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;
Expand All @@ -182,8 +190,8 @@ public Result createSession(HttpClient client, Command command) .getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class); createSessionMethod.setAccessible(true);
Optional<Result> result = (Optional<Result>) createSessionMethod .invoke(this, client, contentStream, counter.getCount()); Optional<Result> result = (Optional<Result>) createSessionMethod.invoke(this, withRequestsPatchedByIdempotencyKey(client), contentStream, counter.getCount());
return result.map(result1 -> { Result toReturn = result.get(); Expand Down
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<DriverService> serviceOptional;
Expand Down Expand Up @@ -150,18 +153,23 @@ protected void setResponseCodec(ResponseCodec<HttpResponse> 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;
Expand All @@ -182,8 +190,8 @@ public Result createSession(HttpClient client, Command command) .getDeclaredMethod("createSession", HttpClient.class, InputStream.class, long.class); createSessionMethod.setAccessible(true);
Optional<Result> result = (Optional<Result>) createSessionMethod .invoke(this, client, contentStream, counter.getCount()); Optional<Result> result = (Optional<Result>) createSessionMethod.invoke(this, withRequestsPatchedByIdempotencyKey(client), contentStream, counter.getCount());
return result.map(result1 -> { Result toReturn = result.get(); Expand Down