From 2159ebdba2b54c1526698c023a43fe81d6b69906 Mon Sep 17 00:00:00 2001 From: TopiSenpai <tobi@strasserhof.com> Date: Wed, 16 Jun 2021 00:28:37 +0200 Subject: [PATCH] some more cleanup --- endpoints.go | 40 +++++++++++++++++++-------------------- rest_error.go => error.go | 6 ++++-- file.go | 1 - rest_client.go | 18 +++++++++--------- route.go | 6 ++---- 5 files changed, 35 insertions(+), 36 deletions(-) rename rest_error.go => error.go (77%) diff --git a/endpoints.go b/endpoints.go index f7a6e1f..d8bd6d3 100644 --- a/endpoints.go +++ b/endpoints.go @@ -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") @@ -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") @@ -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") @@ -76,7 +76,7 @@ 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}") @@ -84,11 +84,11 @@ var ( 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}") @@ -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") @@ -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}") @@ -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}") @@ -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") @@ -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}") @@ -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}) @@ -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}") diff --git a/rest_error.go b/error.go similarity index 77% rename from rest_error.go rename to error.go index 8f30100..b843e09 100644 --- a/rest_error.go +++ b/error.go @@ -5,6 +5,7 @@ import ( ) var _ RestError = (*restErrorImpl)(nil) +var _ error = (*restErrorImpl)(nil) // RestError holds the http.Response & an error type RestError interface { @@ -12,8 +13,9 @@ type RestError interface { 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, diff --git a/file.go b/file.go index 5defb28..3e6f791 100644 --- a/file.go +++ b/file.go @@ -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, diff --git a/rest_client.go b/rest_client.go index 9c8f30a..e9cc148 100644 --- a/rest_client.go +++ b/rest_client.go @@ -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)) @@ -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 { @@ -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 { @@ -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)) } } diff --git a/route.go b/route.go index 9adc19b..cd591bc 100644 --- a/route.go +++ b/route.go @@ -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 { @@ -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),