Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSUrlSessionHandler: TimeoutInterval to TotalSeconds rather than Seconds #259

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/ModernHttpClient/Android/OkHttpNetworkHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class NativeMessageHandler : HttpClientHandler
};

public bool DisableCaching { get; set; }
public TimeSpan? Timeout { get; set; }

public NativeMessageHandler() : this(false, false) {}

Expand Down Expand Up @@ -79,6 +80,14 @@ string getHeaderSeparator(string name)

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (Timeout != null)
{
var timeout = (long)TimeOut.Value.TotalMilliseconds;
client.SetConnectTimeout(timeout, TimeUnit.Milliseconds);
client.SetWriteTimeout(timeout, TimeUnit.Milliseconds);
client.SetReadTimeout(timeout, TimeUnit.Milliseconds);
}

var java_uri = request.RequestUri.GetComponents(UriComponents.AbsoluteUri, UriFormat.UriEscaped);
var url = new Java.Net.URL(java_uri);

Expand Down
6 changes: 6 additions & 0 deletions src/ModernHttpClient/Facades.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerificati
{
}

public TimeSpan? Timeout
{
get {throw new Exception(wrongVersion);}
set {throw new Exception(wrongVersion);}
}

public void RegisterForProgress(HttpRequestMessage request, ProgressDelegate callback)
{
throw new Exception(wrongVersion);
Expand Down
6 changes: 5 additions & 1 deletion src/ModernHttpClient/iOS/NSUrlSessionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class InflightOperation
public CancellationToken CancellationToken { get; set; }
public bool IsCompleted { get; set; }
}

public class NativeMessageHandler : HttpClientHandler
{
readonly NSUrlSession session;
Expand All @@ -52,6 +52,7 @@ public class NativeMessageHandler : HttpClientHandler
readonly bool customSSLVerification;

public bool DisableCaching { get; set; }
public TimeSpan? Timeout { get; set; }

public NativeMessageHandler(): this(false, false) { }
public NativeMessageHandler(bool throwOnCaptiveNetwork, bool customSSLVerification, NativeCookieHandler cookieHandler = null, SslProtocol? minimumSSLProtocol = null)
Expand Down Expand Up @@ -129,6 +130,9 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
Url = NSUrl.FromString(request.RequestUri.AbsoluteUri),
};

if (Timeout != null)
rq.TimeoutInterval = Timeout.Value.TotalSeconds;

var op = session.CreateDataTask(rq);

cancellationToken.ThrowIfCancellationRequested();
Expand Down