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

Options #1299

Merged
merged 11 commits into from
Jan 15, 2023
Merged

Options #1299

merged 11 commits into from
Jan 15, 2023

Conversation

FedorLap2006
Copy link
Collaborator

@FedorLap2006 FedorLap2006 commented Dec 15, 2022

Since the beginning, DiscordGo did not have ability for adding per-call customization:

  • What if you want to disable automatic rate limit handling when you call ChannelMessageDelete, instead handling the error by yourself?
  • Or what if you want to set additional headers on GuildBanCreate, like X-Audit-Log-Reason, to comment your action in audit log?

We’ve been thinking on how to combat this problem, and came up with the following solutions:

  • Builders pattern

    s.WithAuditLogReason("my reason").GuildBanCreate(...)
    s.WithRetryOnRateLimit(false).ChannelMessageDelete(...)
  • Functional options

    s.GuildBanCreate(..., discordgo.WithAuditLogReason("reason"))
    s.ChannelMessageDelete(..., discordgo.WithRetryOnRateLimit(false))

This PR implements the latter. It provides a few pre-built options. However, you can always supply your custom option.

Options have access to a RequestConfig struct, which is internally used by RequestX functions. This config allows you to modify a few global settings, like Client, ShouldRetryOnRateLimit and MaxRestRetries, but it also allows you to modify the request directly.

A typical functional option would look like this:

func WithQuery(key string, value string) discordgo.RequestOption {
	return func(cfg *discordgo.RequestConfig) {
		q := cfg.Request.Query()
		q.Add(key, value)
		cfg.Request.RawQuery = q.Encode()
	}
}

These are built-in options discordgo will provide:

  • WithClient - Control http.Client used for the request (defaults to s.Client)
  • WithHeader - Change a certain header in the request
    • WithAuditLogReason - Set X-Audit-Log-Reason header
    • WithLocale - Set X-Discord-Locale header
  • WithRetryOnRateLimit - Control Session.ShouldRetryOnRateLimit field for the request
  • WithMaxRestRetries - Control Session.MaxRestRetries field for the request
  • WithContext - Provide a context.Context to the http.Request

This change will be affecting all of the REST endpoints. However it should be non-breaking.

If you have used any function signatures though, you would need to change them accordingly:

- func (s *Session) XXX(...) (...) {}
+ func (s *Session) XXX(..., options ...RequestOption) (...) {}

Special thanks to @jalavosus for bringing up the issue with context in REST functions. His PR (#1119) was one of the factors that inspired me to implement this functionality.

Add functional options to request, RequestWithBucketID, RequestWithLockedBucket.
These options allow for request modification and overriding REST related Session fields,
such as Client, MaxRestRetries and ShouldRetryOnRateLimit.
Implement WithContext and WithMaxRestRetries options.
Add documentation to the options
@FedorLap2006 FedorLap2006 added feature Feature implementation feedback Additional feedback is required high priority Issue or PR with high priority of merge rest REST API related issues and pull requests labels Dec 15, 2022
@FedorLap2006 FedorLap2006 added this to the v0.27.0 milestone Dec 15, 2022
@FedorLap2006 FedorLap2006 marked this pull request as ready for review December 25, 2022 23:34
Add option arguments to all Session methods.
Rename requestConfig to newRequestConfig to match other
constructor functions naming.
Manually revert the formatting of documentation in certain functions.
Add description for fetchOptions parameter of Session.UserChannelPermissions.
restapi.go Outdated Show resolved Hide resolved
restapi.go Show resolved Hide resolved
Pass options to ChannelMessageSendEmbedsReply call in ChannelMessageSendEmbedReply
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature implementation feedback Additional feedback is required high priority Issue or PR with high priority of merge rest REST API related issues and pull requests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support for the X-Audit-Log-Reason header Add support for context.Context
1 participant