From 1517dba431de65bc7dcd9a0c3e39162c693f1f98 Mon Sep 17 00:00:00 2001 From: Luke Samuel Date: Thu, 30 May 2019 09:10:04 -0400 Subject: [PATCH] Allow for custom default RequestOptions This change would allow the consumer of the library to set the default `RequestOptions` if they desire. Invoking a method that takes `RequestOptions` as a parameter without specifying the argument will use a copy of the default. Behavior will be unchanged without modifying the `RequestOptions.Default` property. Additionally, this change adds 4 `With...` methods that allow for fluent construction of new `RequestOptions` objects. --- src/Discord.Net.Core/RequestOptions.cs | 50 ++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/src/Discord.Net.Core/RequestOptions.cs b/src/Discord.Net.Core/RequestOptions.cs index 3af3ded6f..ff5bf6125 100644 --- a/src/Discord.Net.Core/RequestOptions.cs +++ b/src/Discord.Net.Core/RequestOptions.cs @@ -8,9 +8,9 @@ namespace Discord public class RequestOptions { /// - /// Creates a new class with its default settings. + /// Gets or sets the default used when not specified with a request. /// - public static RequestOptions Default => new RequestOptions(); + public static RequestOptions Default { get; set; } = new RequestOptions(); /// /// Gets or sets the maximum time to wait for for this request to complete. @@ -53,7 +53,7 @@ namespace Discord internal static RequestOptions CreateOrClone(RequestOptions options) { if (options == null) - return new RequestOptions(); + return Default.Clone(); else return options.Clone(); } @@ -68,5 +68,49 @@ namespace Discord } public RequestOptions Clone() => MemberwiseClone() as RequestOptions; + + /// + /// Creates a copy of the current instance and sets the + /// property. + /// + public RequestOptions WithAuditLogReason (string reason) + { + var clone = Clone(); + clone.AuditLogReason = reason; + return clone; + } + + /// + /// Creates a copy of the current instance and sets the + /// property. + /// + public RequestOptions WithRetryMode (RetryMode retryMode) + { + var clone = Clone(); + clone.RetryMode = retryMode; + return clone; + } + + /// + /// Creates a copy of the current instance and sets the + /// property. + /// + public RequestOptions WithCancellationToken (CancellationToken cancelToken) + { + var clone = Clone(); + clone.CancelToken = cancelToken; + return clone; + } + + /// + /// Creates a copy of the current instance and sets the + /// property. + /// + public RequestOptions WithTimeout (int? timeout) + { + var clone = Clone(); + clone.Timeout = timeout; + return clone; + } } }