Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
added more routes and added QueryValue custom type
Browse files Browse the repository at this point in the history
topi314 committed Jun 15, 2021
1 parent 7c652ca commit 88b87a4
Showing 7 changed files with 34 additions and 14 deletions.
4 changes: 2 additions & 2 deletions api_route.go
Original file line number Diff line number Diff line change
@@ -7,8 +7,8 @@ type APIRoute struct {
}

// Compile returns a CompiledAPIRoute
func (r *APIRoute) Compile(queryParams map[string]interface{}, args ...interface{}) (*CompiledAPIRoute, error) {
compiledRoute, err := r.Route.Compile(queryParams, args...)
func (r *APIRoute) Compile(queryValues QueryValues, args ...interface{}) (*CompiledAPIRoute, error) {
compiledRoute, err := r.Route.Compile(queryValues, args...)
if err != nil {
return nil, err
}
12 changes: 6 additions & 6 deletions cdn_route.go
Original file line number Diff line number Diff line change
@@ -29,13 +29,13 @@ type CDNRoute struct {
// NewCDNRoute generates a new discord cdn route struct
func NewCDNRoute(url string, supportedFileExtensions []FileExtension, queryParams ...string) *CDNRoute {
return &CDNRoute{
Route: newRoute(CDN, url, append(queryParams, "size")),
Route: newRoute(CDN, url, append(queryParams, "size", "v")),
supportedFileExtensions: supportedFileExtensions,
}
}

// Compile builds a full request URL based on provided arguments
func (r *CDNRoute) Compile(queryParams map[string]interface{}, fileExtension FileExtension, size int, args ...interface{}) (*CompiledCDNRoute, error) {
func (r *CDNRoute) Compile(queryValues QueryValues, fileExtension FileExtension, size int, args ...interface{}) (*CompiledCDNRoute, error) {
supported := false
for _, supportedFileExtension := range r.supportedFileExtensions {
if supportedFileExtension == fileExtension {
@@ -45,11 +45,11 @@ func (r *CDNRoute) Compile(queryParams map[string]interface{}, fileExtension Fil
if !supported {
return nil, errors.New("provided file extension: " + fileExtension.String() + " is not supported by discord on this endpoint!")
}
if queryParams == nil {
queryParams = map[string]interface{}{}
if queryValues == nil {
queryValues = QueryValues{}
}
queryParams["size"] = size
compiledRoute, err := r.Route.Compile(queryParams, args...)
queryValues["size"] = size
compiledRoute, err := r.Route.Compile(queryValues, args...)
if err != nil {
return nil, err
}
4 changes: 2 additions & 2 deletions custom_route.go
Original file line number Diff line number Diff line change
@@ -6,8 +6,8 @@ type CustomRoute struct {
}

// Compile returns a CompiledAPIRoute
func (r CustomRoute) Compile(queryParams map[string]interface{}, args ...interface{}) (*CompiledAPIRoute, error) {
compiledRoute, err := r.Route.Compile(queryParams, args...)
func (r CustomRoute) Compile(queryValues QueryValues, args ...interface{}) (*CompiledAPIRoute, error) {
compiledRoute, err := r.Route.Compile(queryValues, args...)
if err != nil {
return nil, err
}
17 changes: 16 additions & 1 deletion endpoints.go
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const (
)

// Misc
//goland:noinspection GoUnusedExportedConstant,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetGateway = NewAPIRoute(GET, "/gateway")
GetGatewayBot = NewAPIRoute(GET, "/gateway/bot")
@@ -17,19 +18,22 @@ var (
)

// Users
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetUser = NewAPIRoute(GET, "/users/{user.id}")
GetSelfUser = NewAPIRoute(GET, "/users/@me")
UpdateSelfUser = NewAPIRoute(PATCH, "/users/@me")
GetGuilds = NewAPIRoute(GET, "/users/@me/guilds")
GetGuilds = NewAPIRoute(GET, "/users/@me/guilds", "before", "after", "limit")
LeaveGuild = NewAPIRoute(DELETE, "/users/@me/guilds/{guild.id}")
GetDMChannels = NewAPIRoute(GET, "/users/@me/channels")
CreateDMChannel = NewAPIRoute(POST, "/users/@me/channels")
)

// Guilds
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetGuild = NewAPIRoute(GET, "/guilds/{guild.id}")
GetGuildPreview = NewAPIRoute(GET, "/guilds/{guild.id}/preview")
CreateGuild = NewAPIRoute(POST, "/guilds")
UpdateGuild = NewAPIRoute(PATCH, "/guilds/{guild.id}")
DeleteGuild = NewAPIRoute(DELETE, "/guilds/{guild.id}")
@@ -46,6 +50,7 @@ var (

GetMember = NewAPIRoute(GET, "/guilds/{guild.id}/members/{user.id}")
GetMembers = NewAPIRoute(GET, "/guilds/{guild.id}/members")
SearchMembers = NewAPIRoute(GET, "/guilds/{guild.id}/members/search", "query", "limit")
AddMember = NewAPIRoute(PUT, "/guilds/{guild.id}/members/{user.id}")
UpdateMember = NewAPIRoute(PATCH, "/guilds/{guild.id}/members/{user.id}")
RemoveMember = NewAPIRoute(DELETE, "/guilds/{guild.id}/members/{user.id}", "reason")
@@ -71,6 +76,7 @@ var (
)

// Roles
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetRoles = NewAPIRoute(GET, "/guilds/{guild.id}/roles")
GetRole = NewAPIRoute(GET, "/guilds/{guild.id}/roles/{role.id}")
@@ -82,6 +88,7 @@ var (
)

// Channels
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetChannel = NewAPIRoute(GET, "/channels/{channel.id}")
UpdateChannel = NewAPIRoute(PATCH, "/channels/{channel.id}")
@@ -100,6 +107,7 @@ var (
)

// Threads
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
CreateThreadWithMessage = NewAPIRoute(POST, "/channels/{channel.id}/messages/{message.id}/threads")
CreateThread = NewAPIRoute(POST, "/channels/{channel.id}/threads")
@@ -116,6 +124,7 @@ var (
)

// Messages
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetMessages = NewAPIRoute(GET, "/channels/{channel.id}/messages")
GetMessage = NewAPIRoute(GET, "/channels/{channel.id}/messages/{message.id}")
@@ -139,6 +148,7 @@ var (
)

// Emotes
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetEmotes = NewAPIRoute(GET, "/guilds/{guild.id}/emojis")
Getemote = NewAPIRoute(GET, "/guilds/{guild.id}/emojis/{emoji.id}")
@@ -148,6 +158,7 @@ var (
)

// Webhooks
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetWebhook = NewAPIRoute(GET, "/webhooks/{webhook.id}")
GetWebhookWithToken = NewAPIRoute(GET, "/webhooks/{webhook.id}/{webhook.token}")
@@ -164,6 +175,7 @@ var (
)

// Invites
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetInvite = NewAPIRoute(GET, "/invites/{code}")
CreateInvite = NewAPIRoute(POST, "/channels/{channel.id}/invites")
@@ -174,6 +186,7 @@ var (
)

// Interactions
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GetGlobalCommands = NewAPIRoute(GET, "/applications/{application.id}/commands")
GetGlobalCommand = NewAPIRoute(GET, "/applications/{application.id}/command/{command.id}")
@@ -204,6 +217,7 @@ var (
)

// CDN
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
Emote = NewCDNRoute("/emojis/{emote.id}.", []FileExtension{PNG, GIF})
GuildIcon = NewCDNRoute("/icons/{guild.id}/{icon.hash}.", []FileExtension{PNG, JPEG, WEBP, GIF})
@@ -220,6 +234,7 @@ var (
)

// Other
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable
var (
GatewayURL = NewRoute(API+"/gateway", "v", "encoding", "compress")
InviteURL = NewRoute("https://discord.gg/{code}")
2 changes: 2 additions & 0 deletions file.go
Original file line number Diff line number Diff line change
@@ -16,6 +16,7 @@ type MultipartBuffer struct {
}

// PayloadWithFiles returns the given payload as multipart body with all files in it
//goland:noinspection GoUnusedExportedFunction
func PayloadWithFiles(v interface{}, files ...File) (buffer *MultipartBuffer, err error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
@@ -71,6 +72,7 @@ func partHeader(contentDisposition string, contentType string) textproto.MIMEHea
}

// NewFile returns a new File struct with the given name, io.Reader & FileFlags
//goland:noinspection ALL
func NewFile(name string, reader io.Reader, flags ...FileFlags) File {
return File{
Name: name,
1 change: 1 addition & 0 deletions rest_client.go
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ var (
)

// NewRestClient constructs a new RestClient with the given http.Client, log.Logger & useragent
//goland:noinspection GoUnusedExportedFunction
func NewRestClient(httpClient *http.Client, logger log.Logger, userAgent string, customHeader http.Header) RestClient {
if httpClient == nil {
httpClient = http.DefaultClient
8 changes: 5 additions & 3 deletions route.go
Original file line number Diff line number Diff line change
@@ -11,6 +11,8 @@ import (
// BaseRoute is the standard base route used
var BaseRoute = ""

type QueryValues map[string]interface{}

func newRoute(baseRoute string, url string, queryParams []string) *Route {
params := map[string]struct{}{}
for _, param := range queryParams {
@@ -33,7 +35,7 @@ type Route struct {
}

// Compile builds a full request URL based on provided arguments
func (r *Route) Compile(queryParams map[string]interface{}, args ...interface{}) (*CompiledRoute, error) {
func (r *Route) Compile(queryValues QueryValues, args ...interface{}) (*CompiledRoute, error) {
if len(args) != r.urlParamCount {
return nil, errors.New("invalid amount of arguments received. expected: " + strconv.Itoa(len(args)) + ", received: " + strconv.Itoa(r.urlParamCount))
}
@@ -48,9 +50,9 @@ func (r *Route) Compile(queryParams map[string]interface{}, args ...interface{})

compiledRoute := r.baseRoute + route
queryParamsStr := ""
if queryParams != nil {
if queryValues != nil {
query := url.Values{}
for param, value := range queryParams {
for param, value := range queryValues {
if _, ok := r.queryParams[param]; !ok {
return nil, errors.New("unexpected query param '" + param + "' received")
}

0 comments on commit 88b87a4

Please sign in to comment.