go-mcm/model/model.go

48 lines
818 B
Go
Raw Normal View History

2019-10-02 19:03:23 +00:00
package model
import (
"fmt"
"go.etztech.xyz/go-mcm/model/django"
"io/ioutil"
"net/http"
"strings"
)
type Query struct {
Token string
URL string
}
func NewQuery(token, baseURL string) *Query {
return &Query{
Token: token,
URL: fmt.Sprintf("%s/model", strings.TrimRight(baseURL, "/")),
}
}
func (q *Query) NewDjangoBuilder() *django.Builder {
return django.NewBuilder(q.Token)
}
func (q *Query) endpoint(model, query string) string {
if !strings.HasPrefix(query, "?") {
query = "?" + query
}
return fmt.Sprintf("%s/%s%s", q.URL, model, query)
}
func response(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
}