added http handler.

This commit is contained in:
5elenay
2021-08-20 21:12:41 +03:00
parent a3cf0d8e4e
commit 14ee6319cc
4 changed files with 63 additions and 11 deletions

47
http.go Normal file
View File

@@ -0,0 +1,47 @@
package revoltgo
import (
"bytes"
"io/ioutil"
"net/http"
"strings"
)
// Send http request
func (c *Client) Request(method, path, data string) ([]byte, error) {
var reqBody *bytes.Buffer
// Check method
if strings.EqualFold(method, "get") {
reqBody = bytes.NewBuffer([]byte(data))
} else {
reqBody = nil
}
// Prepare request
req, err := http.NewRequest(method, API_URL+path, reqBody)
if err != nil {
return []byte(""), err
}
req.Header.Set("X-Bot-Token", c.Token)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
// Send request
resp, err := client.Do(req)
if err != nil {
return []byte(""), err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return []byte(""), err
}
return body, nil
}