Skip to content

Commit 02572c7

Browse files
committed
Make timeout configurable by implementer
1 parent cb5b421 commit 02572c7

File tree

2 files changed

+27
-18
lines changed

2 files changed

+27
-18
lines changed

SabreTools.RedumpLib/Web/CookieWebClient.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace SabreTools.RedumpLib.Web
66
{
77
internal class CookieWebClient : WebClient
88
{
9+
/// <summary>
10+
/// The timespan to wait before the request times out.
11+
/// </summary>
12+
public TimeSpan Timeout { get; set; }
13+
914
// https://stackoverflow.com/questions/1777221/using-cookiecontainer-with-webclient-class
1015
private readonly CookieContainer _container = new();
1116

@@ -33,7 +38,7 @@ protected override WebRequest GetWebRequest(Uri address)
3338
WebRequest request = base.GetWebRequest(address);
3439
if (request is HttpWebRequest webRequest)
3540
{
36-
webRequest.Timeout = 30 * 1000; // 30 seconds
41+
webRequest.Timeout = (int)Timeout.TotalMilliseconds;
3742
webRequest.CookieContainer = _container;
3843
}
3944

SabreTools.RedumpLib/Web/RedumpClient.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,41 +31,45 @@ public class RedumpClient
3131
/// <summary>
3232
/// Maximum retry count for any operation
3333
/// </summary>
34-
public int RetryCount { get; private set; } = 3;
34+
public int RetryCount { get; }
3535

3636
/// <summary>
37-
/// Internal client for interaction
37+
/// Maximum number of seconds for a retry
3838
/// </summary>
39-
#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER
40-
private CookieWebClient _internalClient;
41-
#else
42-
private HttpClient _internalClient;
43-
#endif
44-
45-
#endregion
39+
public int TimeoutSeconds { get; }
4640

4741
/// <summary>
48-
/// Constructor
42+
/// Internal client for interaction
4943
/// </summary>
50-
public RedumpClient()
51-
{
5244
#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER
53-
_internalClient = new CookieWebClient();
45+
private readonly CookieWebClient _internalClient;
5446
#else
55-
_internalClient = new HttpClient(new HttpClientHandler { UseCookies = true }) { Timeout = TimeSpan.FromSeconds(30) };
47+
private readonly HttpClient _internalClient;
5648
#endif
57-
}
49+
50+
#endregion
5851

5952
/// <summary>
6053
/// Constructor
6154
/// </summary>
62-
public RedumpClient(int retryCount) : this()
55+
public RedumpClient(int retryCount = 3, int timeoutSeconds = 30)
6356
{
6457
// Ensure there are a positive number of retries
6558
if (retryCount <= 0)
6659
retryCount = 3;
6760

61+
// Ensure a positive timespan
62+
if (timeoutSeconds <= 0)
63+
timeoutSeconds = 30;
64+
6865
RetryCount = retryCount;
66+
TimeoutSeconds = timeoutSeconds;
67+
68+
#if NETFRAMEWORK || NETSTANDARD2_0_OR_GREATER
69+
_internalClient = new CookieWebClient() { Timeout = TimeSpan.FromSeconds(TimeoutSeconds) };
70+
#else
71+
_internalClient = new HttpClient(new HttpClientHandler { UseCookies = true }) { Timeout = TimeSpan.FromSeconds(TimeoutSeconds) };
72+
#endif
6973
}
7074

7175
#region Credentials
@@ -861,7 +865,7 @@ public async Task<Dictionary<RedumpSystem, byte[]>> DownloadPacks(string url, Re
861865

862866
/// <summary>
863867
/// Download a set of packs
864-
/// </summary>
868+
/// </summary>
865869
/// <param name="url">Base URL to download using</param>
866870
/// <param name="systems">Systems to download packs for</param>
867871
/// <param name="title">Name of the pack that is downloading</param>

0 commit comments

Comments
 (0)