revoltgo/http.go
2021-09-13 17:49:16 +03:00

49 lines
938 B
Go

package revoltgo
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)
// Send http request
func (c Client) Request(method, path string, data []byte) ([]byte, error) {
reqBody := bytes.NewBuffer(data)
// Prepare request
req, err := http.NewRequest(method, API_URL+path, reqBody)
if err != nil {
return []byte{}, err
}
req.Header.Set("content-type", "application/json")
// Set auth headers
if c.SelfBot == nil {
req.Header.Set("x-bot-token", c.Token)
} else if c.SelfBot.SessionToken != "" && c.SelfBot.UserId != "" {
req.Header.Set("x-session-token", c.SelfBot.SessionToken)
}
// Send request
resp, err := c.HTTP.Do(req)
if err != nil {
return []byte{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte{}, err
}
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
return []byte{}, fmt.Errorf("%s: %s", resp.Status, body)
}
return body, nil
}