Skip to content

Commit 72eb86c

Browse files
committed
feat: Add idempotency header to request for new sessions
closes #403
1 parent e8d3701 commit 72eb86c

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

src/Appium.Net/Appium/Service/AppiumCommandExecutor.cs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ internal class AppiumCommandExecutor : ICommandExecutor
2222
{
2323
private readonly AppiumLocalService Service;
2424
private readonly Uri URL;
25-
private readonly ICommandExecutor RealExecutor;
25+
private ICommandExecutor RealExecutor;
2626
private bool isDisposed;
27+
private const string IdempotencyHeader = "X-Idempotency-Key";
28+
2729

2830
private static ICommandExecutor CreateRealExecutor(Uri remoteAddress, TimeSpan commandTimeout)
2931
{
@@ -37,6 +39,7 @@ private static ICommandExecutor CreateRealExecutor(Uri remoteAddress, TimeSpan c
3739
Activator.CreateInstance(commandType, new object[] {remoteAddress, commandTimeout}) as
3840
ICommandExecutor;
3941
}
42+
4043

4144
return commandExecutor;
4245
}
@@ -64,23 +67,29 @@ internal AppiumCommandExecutor(AppiumLocalService service, TimeSpan timeForTheSe
6467
public Response Execute(Command commandToExecute)
6568
{
6669
Response result = null;
67-
if (commandToExecute.Name == DriverCommand.NewSession && this.Service != null)
68-
{
69-
Service.Start();
70-
}
70+
7171

7272
try
7373
{
74+
if (commandToExecute.Name == DriverCommand.NewSession && Service != null)
75+
{
76+
Service.Start();
77+
}
78+
else
79+
{
80+
RealExecutor = ModifyHttpRequestHeader(RealExecutor);
81+
}
82+
7483
result = RealExecutor.Execute(commandToExecute);
7584
return result;
7685
}
7786
catch (Exception e)
7887
{
79-
if ((commandToExecute.Name == DriverCommand.NewSession) && (Service != null))
88+
if ((commandToExecute.Name == DriverCommand.NewSession))
8089
{
81-
Service.Dispose();
90+
Service?.Dispose();
8291
}
83-
throw e;
92+
throw;
8493
}
8594
finally
8695
{
@@ -97,21 +106,29 @@ public Response Execute(Command commandToExecute)
97106
}
98107
}
99108

100-
public void Dispose()
109+
private ICommandExecutor ModifyHttpRequestHeader(ICommandExecutor commandExecutor)
110+
{
111+
var modifiedCommandExecutor = commandExecutor as HttpCommandExecutor;
112+
if (modifiedCommandExecutor != null)
113+
modifiedCommandExecutor.SendingRemoteHttpRequest += SendingRemoteHttpRequestHandler;
114+
return modifiedCommandExecutor;
115+
}
116+
117+
private void SendingRemoteHttpRequestHandler(
118+
object sender, SendingRemoteHttpRequestEventArgs e)
101119
{
102-
this.Dispose(true);
120+
e.Request.Headers.Add(IdempotencyHeader, Guid.NewGuid().ToString());
103121
}
104122

123+
public void Dispose() => Dispose(true);
124+
105125
protected void Dispose(bool disposing)
106126
{
107127
if (!isDisposed)
108128
{
109129
if (disposing)
110130
{
111-
if (Service != null)
112-
{
113-
Service.Dispose();
114-
}
131+
Service?.Dispose();
115132
}
116133

117134
isDisposed = true;

0 commit comments

Comments
 (0)