Skip to content

Commit

Permalink
move params into a struct
Browse files Browse the repository at this point in the history
  • Loading branch information
sebm253 committed Dec 1, 2024
1 parent 10047f6 commit 76c6342
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 24 deletions.
27 changes: 3 additions & 24 deletions rest/applications.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rest

import (
"github.com/disgoorg/disgo/internal/slicehelper"
"github.com/disgoorg/snowflake/v2"

"github.com/disgoorg/disgo/discord"
Expand Down Expand Up @@ -37,7 +36,7 @@ type Applications interface {
GetApplicationRoleConnectionMetadata(applicationID snowflake.ID, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)
UpdateApplicationRoleConnectionMetadata(applicationID snowflake.ID, newRecords []discord.ApplicationRoleConnectionMetadata, opts ...RequestOpt) ([]discord.ApplicationRoleConnectionMetadata, error)

GetEntitlements(applicationID snowflake.ID, userID snowflake.ID, guildID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, excludeEnded bool, excludeDeleted bool, skuIDs []snowflake.ID, opts ...RequestOpt) ([]discord.Entitlement, error)
GetEntitlements(applicationID snowflake.ID, params QueryParamsGetEntitlements, opts ...RequestOpt) ([]discord.Entitlement, error)
GetEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) (*discord.Entitlement, error)
CreateTestEntitlement(applicationID snowflake.ID, entitlementCreate discord.TestEntitlementCreate, opts ...RequestOpt) (*discord.Entitlement, error)
DeleteTestEntitlement(applicationID snowflake.ID, entitlementID snowflake.ID, opts ...RequestOpt) error
Expand Down Expand Up @@ -184,28 +183,8 @@ func (s *applicationsImpl) UpdateApplicationRoleConnectionMetadata(applicationID
return
}

func (s *applicationsImpl) GetEntitlements(applicationID snowflake.ID, userID snowflake.ID, guildID snowflake.ID, before snowflake.ID, after snowflake.ID, limit int, excludeEnded bool, excludeDeleted bool, skuIDs []snowflake.ID, opts ...RequestOpt) (entitlements []discord.Entitlement, err error) {
queryValues := discord.QueryValues{
"exclude_ended": excludeEnded,
"exclude_deleted": excludeDeleted,
"sku_ids": slicehelper.JoinSnowflakes(skuIDs),
}
if userID != 0 {
queryValues["user_id"] = userID
}
if guildID != 0 {
queryValues["guild_id"] = guildID
}
if before != 0 {
queryValues["before"] = before
}
if after != 0 {
queryValues["after"] = after
}
if limit != 0 {
queryValues["limit"] = limit
}
err = s.client.Do(GetEntitlements.Compile(queryValues, applicationID), nil, &entitlements, opts...)
func (s *applicationsImpl) GetEntitlements(applicationID snowflake.ID, params QueryParamsGetEntitlements, opts ...RequestOpt) (entitlements []discord.Entitlement, err error) {
err = s.client.Do(GetEntitlements.Compile(params.ToQueryValues(), applicationID), nil, &entitlements, opts...)
return
}

Expand Down
38 changes: 38 additions & 0 deletions rest/query_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,48 @@ package rest

import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/internal/slicehelper"
"github.com/disgoorg/snowflake/v2"
)

// QueryParams serves as a generic interface for implementations of rest endpoint query parameters.
type QueryParams interface {
// ToQueryValues transforms fields from the QueryParams interface implementations into discord.QueryValues.
ToQueryValues() discord.QueryValues
}

// QueryParamsGetEntitlements holds query parameters for Applications.GetEntitlements (https://discord.com/developers/docs/resources/entitlement#list-entitlements)
type QueryParamsGetEntitlements struct {
UserID snowflake.ID
SkuIDs []snowflake.ID
Before int
After int
Limit int
GuildID snowflake.ID
ExcludeEnded bool
ExcludeDeleted bool
}

func (p QueryParamsGetEntitlements) ToQueryValues() discord.QueryValues {
queryValues := discord.QueryValues{
"exclude_ended": p.ExcludeEnded,
"exclude_deleted": p.ExcludeDeleted,
"sku_ids": slicehelper.JoinSnowflakes(p.SkuIDs),
}
if p.UserID != 0 {
queryValues["user_id"] = p.UserID
}
if p.Before != 0 {
queryValues["before"] = p.Before
}
if p.After != 0 {
queryValues["after"] = p.After
}
if p.Limit != 0 {
queryValues["limit"] = p.Limit
}
if p.GuildID != 0 {
queryValues["guild_id"] = p.GuildID
}
return queryValues
}

0 comments on commit 76c6342

Please sign in to comment.