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

Allow request timeout to be set #78

Open
wants to merge 4 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
70 changes: 59 additions & 11 deletions src/AvaTaxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
using System.Runtime.ExceptionServices;
#endif
using System.Net;
Expand All @@ -23,10 +24,15 @@ namespace Avalara.AvaTax.RestClient
/// </remarks>
public partial class AvaTaxClient
{
private AvaTaxClientOptions _options = new AvaTaxClientOptions();
private Dictionary<string, string> _clientHeaders = new Dictionary<string, string>();
private Uri _envUri;
#if PORTABLE
private static HttpClient _client = new HttpClient() { Timeout = TimeSpan.FromMinutes(20) };
private static HttpClient _client = new HttpClient()
{
// This timeout will be the default timeout used for all requests if if a timeout is not provided with the AvaTaxClientOptions
Timeout = TimeSpan.FromMinutes(20)
};
#endif

/// <summary>
Expand Down Expand Up @@ -81,9 +87,9 @@ public AvaTaxClient(string appName, string appVersion, string machineName, Uri c
WithClientIdentifier(appName, appVersion, machineName);
_envUri = customEnvironment;
}
#endregion
#endregion

#region Security
#region Security
/// <summary>
/// Sets the default security header string
/// </summary>
Expand Down Expand Up @@ -165,9 +171,20 @@ public AvaTaxClient WithClientIdentifier(string appName, string appVersion, stri
_clientHeaders.Add(Constants.AVALARA_CLIENT_HEADER, String.Format("{0}; {1}; {2}; {3}; {4}", appName, appVersion, "CSharpRestClient", API_VERSION, machineName));
return this;
}
#endregion
#endregion

/// <summary>
/// Sets options for the AvaTaxClient and how it behaves.
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public AvaTaxClient WithOptions(AvaTaxClientOptions options)
{
_options = options ?? new AvaTaxClientOptions();
return this;
}

#region REST Call Interface
#region REST Call Interface
#if PORTABLE
/// <summary>
/// Implementation of asynchronous client APIs
Expand Down Expand Up @@ -236,9 +253,9 @@ public FileResult RestCallFile(string verb, AvaTaxPath relativePath, object payl
}
}
#endif
#endregion
#endregion

#region Implementation
#region Implementation
private JsonSerializerSettings _serializer_settings = null;
private JsonSerializerSettings SerializerSettings
{
Expand Down Expand Up @@ -354,7 +371,27 @@ private async Task<HttpResponseMessage> InternalRestCallAsync(CallDuration cd, s
// Send
cd.FinishSetup();

return await _client.SendAsync(request).ConfigureAwait(false);
CancellationTokenSource timeoutTokenSource = null;

try
{
CancellationToken token = default(CancellationToken);
if (_options != null && _options.Timeout.HasValue)
{
timeoutTokenSource = new CancellationTokenSource(_options.Timeout.Value);
token = timeoutTokenSource.Token;
}

return await _client.SendAsync(request, token).ConfigureAwait(false);
}
finally
{
if (timeoutTokenSource != null)
{
timeoutTokenSource.Dispose();
timeoutTokenSource = null;
}
}
}
}

Expand Down Expand Up @@ -482,7 +519,13 @@ private FileResult RestCallFile(string verb, AvaTaxPath relativePath, object con
// Use HttpWebRequest so we can get a decent response
var wr = (HttpWebRequest)WebRequest.Create(path);
wr.Proxy = null;

// Default to 20 minutes if a timeout has not been provided in the AvaTaxClientOptions.
wr.Timeout = 1200000;
if (_options != null && _options.Timeout.HasValue)
{
wr.Timeout = Convert.ToInt32(Math.Floor(_options.Timeout.Value.TotalMilliseconds));
}

// Add headers
foreach (var key in _clientHeaders.Keys)
Expand Down Expand Up @@ -515,7 +558,7 @@ private FileResult RestCallFile(string verb, AvaTaxPath relativePath, object con
using (var inStream = response.GetResponseStream()) {
const int BUFFER_SIZE = 1024;
var chunks = new List<byte>();
var totalBytes = 0;
var totalBytes = 0;
var bytesRead = 0;

do
Expand All @@ -531,7 +574,7 @@ private FileResult RestCallFile(string verb, AvaTaxPath relativePath, object con
}
totalBytes += bytesRead;
} while (bytesRead > 0);

if(totalBytes <= 0) {
throw new IOException("Response contained no data");
}
Expand Down Expand Up @@ -593,6 +636,11 @@ private string RestCallString(string verb, AvaTaxPath relativePath, object conte
wr.Proxy = null;
wr.Timeout = 1200000;

if (_options != null && _options.Timeout.HasValue)
{
wr.Timeout = Convert.ToInt32(Math.Floor(_options.Timeout.Value.TotalMilliseconds));
}

// Add headers
foreach (var key in _clientHeaders.Keys)
{
Expand Down Expand Up @@ -655,7 +703,7 @@ private string RestCallString(string verb, AvaTaxPath relativePath, object conte
}
}

// Catch a web exception
// Catch a web exception
} catch (WebException webex) {
HttpWebResponse httpWebResponse = webex.Response as HttpWebResponse;
if (httpWebResponse != null) {
Expand Down
21 changes: 21 additions & 0 deletions src/AvaTaxClientOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
#if PORTABLE
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Threading;
#endif

namespace Avalara.AvaTax.RestClient
{
/// <summary>
/// Configuration options for the <see cref="AvaTaxClient"/>
/// </summary>
public class AvaTaxClientOptions
{
/// <summary>
/// The request timeout.
/// </summary>
public TimeSpan? Timeout { get; set; }
}
}
1 change: 1 addition & 0 deletions src/Avalara.AvaTax.net20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<Compile Include="AvaTaxApi.cs" />
<Compile Include="AvaTaxCallEventArgs.cs" />
<Compile Include="AvaTaxClient.cs" />
<Compile Include="AvaTaxClientOptions.cs" />
<Compile Include="AvaTaxEnvironment.cs" />
<Compile Include="AvaTaxError.cs" />
<Compile Include="AvaTaxOfflineHelper.cs" />
Expand Down
1 change: 1 addition & 0 deletions src/Avalara.AvaTax.net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="AvaTaxApi.cs" />
<Compile Include="AvaTaxCallEventArgs.cs" />
<Compile Include="AvaTaxClient.cs" />
<Compile Include="AvaTaxClientOptions.cs" />
<Compile Include="AvaTaxEnvironment.cs" />
<Compile Include="AvaTaxError.cs" />
<Compile Include="AvaTaxOfflineHelper.cs" />
Expand Down