wtf auth??
This commit is contained in:
parent
607d7e121c
commit
a3a1654030
14 changed files with 337 additions and 1 deletions
|
@ -42,6 +42,10 @@ func (c *DiscordClientMock) BotAuth(req *http.Request) {
|
|||
c.Called(req)
|
||||
}
|
||||
|
||||
func (c *DiscordClientMock) ClientAuth(req *http.Request, accessToken string) {
|
||||
c.Called(req, accessToken)
|
||||
}
|
||||
|
||||
func (c *DiscordClientMock) MockResponse(method, path string, statusCode int, data any) {
|
||||
body := bytes.Buffer{}
|
||||
json.NewEncoder(&body).Encode(data)
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package discord
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
|
@ -11,9 +12,14 @@ import (
|
|||
|
||||
const DiscordBaseUrl = "https://discord.com/api/v10"
|
||||
|
||||
var (
|
||||
ErrUnauthorized = errors.New("discord: got a 401 from discord")
|
||||
)
|
||||
|
||||
type IDiscordClient interface {
|
||||
Do(req *http.Request) (*http.Response, error)
|
||||
BotAuth(req *http.Request)
|
||||
ClientAuth(req *http.Request, accessToken string)
|
||||
GetClientID() string
|
||||
}
|
||||
|
||||
|
@ -44,6 +50,10 @@ func (d *DiscordClient) BotAuth(req *http.Request) {
|
|||
req.Header.Set("Authorization", fmt.Sprintf("Bot %s", d.BotToken))
|
||||
}
|
||||
|
||||
func (d *DiscordClient) ClientAuth(req *http.Request, accessToken string) {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", d.BotToken))
|
||||
}
|
||||
|
||||
func NewRequest(method string, path string) *http.Request {
|
||||
url, err := url.Parse(fmt.Sprintf("%s%s", DiscordBaseUrl, path))
|
||||
if err != nil {
|
||||
|
@ -62,6 +72,10 @@ func NewRequest(method string, path string) *http.Request {
|
|||
}
|
||||
|
||||
func OutputResponse(resp *http.Response, dst any) error {
|
||||
if resp.StatusCode == 401 {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
|
||||
// TODO: more checks?
|
||||
return json.NewDecoder(resp.Body).Decode(dst)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue