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

Commit

Permalink
some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jun 15, 2021
1 parent 88b87a4 commit 2159ebd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 36 deletions.
40 changes: 20 additions & 20 deletions endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const (
)

// Misc
//goland:noinspection GoUnusedExportedConstant,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetGateway = NewAPIRoute(GET, "/gateway")
GetGatewayBot = NewAPIRoute(GET, "/gateway/bot")
Expand All @@ -18,7 +18,7 @@ var (
)

// Users
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetUser = NewAPIRoute(GET, "/users/{user.id}")
GetSelfUser = NewAPIRoute(GET, "/users/@me")
Expand All @@ -30,7 +30,7 @@ var (
)

// 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
//goland:noinspection GoUnusedGlobalVariable
var (
GetGuild = NewAPIRoute(GET, "/guilds/{guild.id}")
GetGuildPreview = NewAPIRoute(GET, "/guilds/{guild.id}/preview")
Expand Down Expand Up @@ -76,19 +76,19 @@ var (
)

// Roles
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetRoles = NewAPIRoute(GET, "/guilds/{guild.id}/roles")
GetRole = NewAPIRoute(GET, "/guilds/{guild.id}/roles/{role.id}")
CreateRole = NewAPIRoute(POST, "/guilds/{guild.id}/roles")
UpdateRoles = NewAPIRoute(PATCH, "/guilds/{guild.id}/roles")
UpdateRole = NewAPIRoute(PATCH, "/guilds/{guild.id}/roles/{role.id}")
UpdateRolePositions = NewAPIRoute(PATCH, "/guilds/{guild.id}/roles")
DelteRole = NewAPIRoute(DELETE, "/guilds/{guild.id}/roles/{role.id}")
DeleteRole = NewAPIRoute(DELETE, "/guilds/{guild.id}/roles/{role.id}")
)

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

// Threads
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
CreateThreadWithMessage = NewAPIRoute(POST, "/channels/{channel.id}/messages/{message.id}/threads")
CreateThread = NewAPIRoute(POST, "/channels/{channel.id}/threads")
Expand All @@ -117,14 +117,14 @@ var (
RemoveThreadMember = NewAPIRoute(DELETE, "/channels/{channel.id}/thread-members/{user.id}")
GetThreadMembers = NewAPIRoute(GET, "/channels/{channel.id}/thread-members")

GetActiveThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/active")
GetArchivedPublicThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/archived/public")
GetArchivedPrivateThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/archived/private")
GetJoinedArchievedPrivateThreads = NewAPIRoute(GET, "/channels/{channel.id}/users/@me/threads/archived/private")
GetActiveThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/active")
GetArchivedPublicThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/archived/public")
GetArchivedPrivateThreads = NewAPIRoute(GET, "/channels/{channel.id}/threads/archived/private")
GetJoinedAchievedPrivateThreads = NewAPIRoute(GET, "/channels/{channel.id}/users/@me/threads/archived/private")
)

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

// Emotes
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetEmotes = NewAPIRoute(GET, "/guilds/{guild.id}/emojis")
Getemote = NewAPIRoute(GET, "/guilds/{guild.id}/emojis/{emoji.id}")
GetEmote = NewAPIRoute(GET, "/guilds/{guild.id}/emojis/{emoji.id}")
CreateEmote = NewAPIRoute(POST, "/guilds/{guild.id}/emojis")
UpdateEmote = NewAPIRoute(PATCH, "/guilds/{guild.id}/emojis/{emote.id}")
DeleteEmote = NewAPIRoute(DELETE, "/guilds/{guild.id}/emojis/{emote.id}")
)

// Webhooks
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetWebhook = NewAPIRoute(GET, "/webhooks/{webhook.id}")
GetWebhookWithToken = NewAPIRoute(GET, "/webhooks/{webhook.id}/{webhook.token}")
UpdateWebhook = NewAPIRoute(PATCH, "/webhooks/{webhook.id}")
UpdateWebhokWithToken = NewAPIRoute(PATCH, "/webhooks/{webhook.id}/{webhook.token}")
UpdateWebhookWithToken = NewAPIRoute(PATCH, "/webhooks/{webhook.id}/{webhook.token}")
DeleteWebhook = NewAPIRoute(DELETE, "/webhooks/{webhook.id}")
DeleteWebhookWithToken = NewAPIRoute(DELETE, "/webhooks/{webhook.id}/{webhook.token}")

Expand All @@ -175,7 +175,7 @@ var (
)

// Invites
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GetInvite = NewAPIRoute(GET, "/invites/{code}")
CreateInvite = NewAPIRoute(POST, "/channels/{channel.id}/invites")
Expand All @@ -186,7 +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
//goland:noinspection GoUnusedGlobalVariable
var (
GetGlobalCommands = NewAPIRoute(GET, "/applications/{application.id}/commands")
GetGlobalCommand = NewAPIRoute(GET, "/applications/{application.id}/command/{command.id}")
Expand Down Expand Up @@ -217,7 +217,7 @@ var (
)

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

// Other
//goland:noinspection GoUnusedGlobalVariable,GoUnusedGlobalVariable
//goland:noinspection GoUnusedGlobalVariable
var (
GatewayURL = NewRoute(API+"/gateway", "v", "encoding", "compress")
InviteURL = NewRoute("https://discord.gg/{code}")
Expand Down
6 changes: 4 additions & 2 deletions rest_error.go → error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import (
)

var _ RestError = (*restErrorImpl)(nil)
var _ error = (*restErrorImpl)(nil)

// RestError holds the http.Response & an error
type RestError interface {
error
Response() *http.Response
}

// NewRestError returns a new RestError with the given http.Response & error
func NewRestError(response *http.Response, err error) RestError {
// NewError returns a new Error with the given http.Response & error
//goland:noinspection GoUnusedExportedFunction
func NewError(response *http.Response, err error) RestError {
return &restErrorImpl{
err: err,
response: response,
Expand Down
1 change: 0 additions & 1 deletion file.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ 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,
Expand Down
18 changes: 9 additions & 9 deletions rest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (r *restClientImpl) DoWithHeaders(route *CompiledAPIRoute, rqBody interface
buffer = &bytes.Buffer{}
err := json.NewEncoder(buffer).Encode(rqBody)
if err != nil {
return NewRestError(nil, err)
return NewError(nil, err)
}
}
body, _ := ioutil.ReadAll(io.TeeReader(buffer, rqBuffer))
Expand All @@ -96,7 +96,7 @@ func (r *restClientImpl) DoWithHeaders(route *CompiledAPIRoute, rqBody interface

rq, err := http.NewRequest(route.Method().String(), route.URL(), rqBuffer)
if err != nil {
return NewRestError(nil, err)
return NewError(nil, err)
}

if customHeader != nil {
Expand All @@ -109,7 +109,7 @@ func (r *restClientImpl) DoWithHeaders(route *CompiledAPIRoute, rqBody interface

rs, err := r.httpClient.Do(rq)
if err != nil {
return NewRestError(rs, err)
return NewError(rs, err)
}

if rs.Body != nil {
Expand All @@ -124,29 +124,29 @@ func (r *restClientImpl) DoWithHeaders(route *CompiledAPIRoute, rqBody interface
if rsBody != nil && rs.Body != nil {
if err = json.NewDecoder(rs.Body).Decode(rsBody); err != nil {
r.Logger().Errorf("error unmarshalling response. error: %s", err)
return NewRestError(rs, err)
return NewError(rs, err)
}
}
return nil

case http.StatusTooManyRequests:
r.Logger().Error(ErrRatelimited)
return NewRestError(rs, ErrRatelimited)
return NewError(rs, ErrRatelimited)

case http.StatusBadGateway:
r.Logger().Error(ErrBadGateway)
return NewRestError(rs, ErrBadGateway)
return NewError(rs, ErrBadGateway)

case http.StatusBadRequest:
r.Logger().Error(ErrBadRequest)
return NewRestError(rs, ErrBadRequest)
return NewError(rs, ErrBadRequest)

case http.StatusUnauthorized:
r.Logger().Error(ErrUnauthorized)
return NewRestError(rs, ErrUnauthorized)
return NewError(rs, ErrUnauthorized)

default:
body, _ := ioutil.ReadAll(rq.Body)
return NewRestError(rs, fmt.Errorf("request to %s failed. statuscode: %d, body: %s", rq.URL, rs.StatusCode, body))
return NewError(rs, fmt.Errorf("request to %s failed. statuscode: %d, body: %s", rq.URL, rs.StatusCode, body))
}
}
6 changes: 2 additions & 4 deletions route.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"strings"
)

// BaseRoute is the standard base route used
var BaseRoute = ""

// QueryValues is used to supply query param value pairs to Route.Compile
type QueryValues map[string]interface{}

func newRoute(baseRoute string, url string, queryParams []string) *Route {
Expand Down Expand Up @@ -73,7 +71,7 @@ func NewRoute(url string, queryParams ...string) *Route {
params[param] = struct{}{}
}
return &Route{
baseRoute: BaseRoute,
baseRoute: "",
route: url,
queryParams: params,
urlParamCount: countURLParams(url),
Expand Down

0 comments on commit 2159ebd

Please sign in to comment.