67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
package libgm
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"reflect"
|
|
|
|
"go.mau.fi/mautrix-gmessages/libgm/util"
|
|
)
|
|
|
|
func (c *Client) PostRequest(url string, payload []byte, headers interface{}) (*http.Response, error) {
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(payload))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reqHeaders := &http.Header{}
|
|
SetHeaders(reqHeaders, headers)
|
|
req.Header = *reqHeaders
|
|
res, reqErr := c.http.Do(req)
|
|
if reqErr != nil {
|
|
return res, reqErr
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (c *Client) GetRequest(url string, headers interface{}) (*http.Response, error) {
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
reqHeaders := &http.Header{}
|
|
SetHeaders(reqHeaders, headers)
|
|
req.Header = *reqHeaders
|
|
res, reqErr := c.http.Do(req)
|
|
if reqErr != nil {
|
|
return res, reqErr
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func (c *Client) MakeRelayRequest(url string, body []byte) (*http.Response, error) {
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
util.BuildRelayHeaders(req, "application/x-protobuf", "*/*")
|
|
res, reqErr := c.http.Do(req)
|
|
if reqErr != nil {
|
|
return res, reqErr
|
|
}
|
|
return res, nil
|
|
}
|
|
|
|
func SetHeaders(h *http.Header, headers interface{}) {
|
|
if headers == nil {
|
|
return
|
|
}
|
|
v := reflect.ValueOf(headers)
|
|
for i := 0; i < v.NumField(); i++ {
|
|
field := v.Type().Field(i)
|
|
value := v.Field(i).String()
|
|
if !v.Field(i).IsZero() {
|
|
h.Set(field.Tag.Get("header"), value)
|
|
}
|
|
}
|
|
}
|