62
62
import java .net .URL ;
63
63
import java .util .Map ;
64
64
import java .util .Optional ;
65
+ import java .util .UUID ;
65
66
66
67
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" ;
67
70
68
71
private final Optional <DriverService > serviceOptional ;
69
72
@@ -150,18 +153,23 @@ protected void setResponseCodec(ResponseCodec<HttpResponse> codec) {
150
153
}
151
154
152
155
protected HttpClient getClient () {
153
- //noinspection unchecked
154
156
return getPrivateFieldValue ("client" , HttpClient .class );
155
157
}
156
158
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
+
157
166
private Response createSession (Command command ) throws IOException {
158
167
if (getCommandCodec () != null ) {
159
168
throw new SessionNotCreatedException ("Session already exists" );
160
169
}
161
170
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 {
165
173
Capabilities desiredCapabilities = (Capabilities ) command .getParameters ().get ("desiredCapabilities" );
166
174
Capabilities desired = desiredCapabilities == null ? new ImmutableCapabilities () : desiredCapabilities ;
167
175
@@ -182,8 +190,8 @@ public Result createSession(HttpClient client, Command command)
182
190
.getDeclaredMethod ("createSession" , HttpClient .class , InputStream .class , long .class );
183
191
createSessionMethod .setAccessible (true );
184
192
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 ());
187
195
188
196
return result .map (result1 -> {
189
197
Result toReturn = result .get ();
0 commit comments