go-mcm/internal/common.go

59 lines
974 B
Go
Raw Normal View History

2019-10-03 20:22:22 +00:00
package internal
import (
"encoding/json"
2019-10-03 20:22:22 +00:00
"io/ioutil"
"net/http"
"net/url"
)
type Status struct {
Success bool `json:"success"`
Message string `json:"message"`
}
2019-10-03 20:22:22 +00:00
func ResponseGet(endpoint string) ([]byte, error) {
resp, err := http.Get(endpoint)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func ResponsePost(endpoint string, form url.Values) ([]byte, error) {
resp, err := http.PostForm(endpoint, form)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, err
}
func ResponseStatus(endpoint string, form url.Values) (*Status, error) {
resp, err := ResponsePost(endpoint, form)
if err != nil {
return nil, err
}
status := &Status{}
err = json.Unmarshal(resp, &status)
if err != nil {
return nil, err
}
return status, nil
2019-10-03 20:22:22 +00:00
}