Skip to content

Commit

Permalink
feat: add configurable Timeout for BoxClient (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwwoda authored Dec 1, 2021
1 parent 1098f75 commit ac842ed
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Box.V2/BoxClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public BoxClient(IBoxConfig boxConfig, string asUser = null, bool? suppressNotif
_asUser = asUser;
_suppressNotifications = suppressNotifications;

_handler = new HttpRequestHandler(boxConfig.WebProxy);
_handler = new HttpRequestHandler(boxConfig.WebProxy, boxConfig.Timeout);
_converter = new BoxJsonConverter();
_service = new BoxService(_handler);
Auth = new AuthRepository(Config, _service, _converter, null);
Expand All @@ -56,7 +56,7 @@ public BoxClient(IBoxConfig boxConfig, OAuthSession authSession, string asUser =
_asUser = asUser;
_suppressNotifications = suppressNotifications;

_handler = new HttpRequestHandler(boxConfig.WebProxy);
_handler = new HttpRequestHandler(boxConfig.WebProxy, boxConfig.Timeout);
_converter = new BoxJsonConverter();
_service = new BoxService(_handler);
Auth = new AuthRepository(Config, _service, _converter, authSession);
Expand All @@ -78,7 +78,7 @@ public BoxClient(IBoxConfig boxConfig, IAuthRepository authRepository, string as
_asUser = asUser;
_suppressNotifications = suppressNotifications;

_handler = new HttpRequestHandler(boxConfig.WebProxy);
_handler = new HttpRequestHandler(boxConfig.WebProxy, boxConfig.Timeout);
_converter = new BoxJsonConverter();
_service = new BoxService(_handler);
Auth = authRepository;
Expand Down
5 changes: 5 additions & 0 deletions Box.V2/Config/BoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public BoxConfig(BoxConfigBuilder builder)
DeviceName = builder.DeviceName;
AcceptEncoding = builder.AcceptEncoding;
WebProxy = builder.WebProxy;
Timeout = builder.Timeout;
}

/// <summary>
Expand Down Expand Up @@ -272,6 +273,10 @@ public static IBoxConfig CreateFromJsonString(string jsonString)
/// The web proxy for HttpRequestHandler
/// </summary>
public IWebProxy WebProxy { get; private set; }
/// <summary>
/// Timeout for the connection
/// </summary>
public TimeSpan? Timeout { get; private set; }
}

public enum CompressionType
Expand Down
16 changes: 16 additions & 0 deletions Box.V2/Config/BoxConfigBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ public BoxConfigBuilder SetWebProxy(IWebProxy webProxy)
return this;
}

/// <summary>
/// Sets connection timeout for HttpRequestHandler.
/// </summary>
/// <param name="timeout">Connection timeout for HttpRequestHandler.</param>
/// <returns>this BoxConfigBuilder object for chaining</returns>
public BoxConfigBuilder SetTimeout(TimeSpan timeout)
{
Timeout = timeout;
return this;
}

public string ClientId { get; private set; }
public string ClientSecret { get; private set; }
public string EnterpriseId { get; private set; }
Expand Down Expand Up @@ -230,5 +241,10 @@ public BoxConfigBuilder SetWebProxy(IWebProxy webProxy)
/// The web proxy for HttpRequestHandler
/// </summary>
public IWebProxy WebProxy { get; private set; }

/// <summary>
/// Timeout for the connection
/// </summary>
public TimeSpan? Timeout { get; private set; }
}
}
4 changes: 4 additions & 0 deletions Box.V2/Config/IBoxConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,9 @@ public interface IBoxConfig
/// The web proxy for HttpRequestHandler
/// </summary>
IWebProxy WebProxy { get; }
/// <summary>
/// Timeout for the connection
/// </summary>
TimeSpan? Timeout { get; }
}
}
2 changes: 1 addition & 1 deletion Box.V2/JWTAuth/BoxJWTAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public BoxJWTAuth(IBoxConfig boxConfig, IBoxService boxService)
/// Constructor for JWT authentication with default boxService
/// </summary>
/// <param name="boxConfig">Config contains information about client id, client secret, enterprise id, private key, private key password, public key id </param>
public BoxJWTAuth(IBoxConfig boxConfig) : this(boxConfig, new BoxService(new HttpRequestHandler(boxConfig.WebProxy)))
public BoxJWTAuth(IBoxConfig boxConfig) : this(boxConfig, new BoxService(new HttpRequestHandler(boxConfig.WebProxy, boxConfig.Timeout)))
{

}
Expand Down
6 changes: 4 additions & 2 deletions Box.V2/Request/HttpRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ public class HttpRequestHandler : IRequestHandler
public const HttpStatusCode TooManyRequests = (HttpStatusCode)429;
public const int RetryLimit = 5;
private readonly TimeSpan _defaultRequestTimeout = new TimeSpan(0, 0, 100); // 100 seconds, same as default HttpClient timeout
private readonly TimeSpan _timeout;

public HttpRequestHandler(IWebProxy webProxy = null)
public HttpRequestHandler(IWebProxy webProxy = null, TimeSpan? timeout = null)
{
ClientFactory.WebProxy = webProxy;
#if NET45
System.Net.ServicePointManager.Expect100Continue = false;
#endif
_timeout = timeout ?? _defaultRequestTimeout;
}

public async Task<IBoxResponse<T>> ExecuteAsyncWithoutRetry<T>(IBoxRequest request)
Expand Down Expand Up @@ -168,7 +170,7 @@ private async Task<HttpResponseMessage> GetResponse(IBoxRequest request, bool is
}
else
{
cts.CancelAfter(_defaultRequestTimeout);
cts.CancelAfter(_timeout);
}

var timeoutToken = cts.Token;
Expand Down

0 comments on commit ac842ed

Please sign in to comment.