git-lfs/lfsapi/lfsapi.go

44 lines
904 B
Go
Raw Normal View History

package lfsapi
2016-12-16 22:43:05 +00:00
import (
"encoding/json"
"net/http"
"regexp"
2016-12-16 22:47:02 +00:00
"github.com/git-lfs/git-lfs/errors"
2016-12-16 22:43:05 +00:00
)
var (
lfsMediaTypeRE = regexp.MustCompile(`\Aapplication/vnd\.git\-lfs\+json(;|\z)`)
jsonMediaTypeRE = regexp.MustCompile(`\Aapplication/json(;|\z)`)
)
type Client struct {
2016-12-19 18:45:22 +00:00
Endpoints EndpointFinder
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
2016-12-16 22:43:05 +00:00
res, err := http.DefaultClient.Do(req)
if err != nil {
return res, err
}
return res, c.handleResponse(res)
}
func decodeResponse(res *http.Response, obj interface{}) error {
ctype := res.Header.Get("Content-Type")
if !(lfsMediaTypeRE.MatchString(ctype) || jsonMediaTypeRE.MatchString(ctype)) {
return nil
}
err := json.NewDecoder(res.Body).Decode(obj)
res.Body.Close()
if err != nil {
return errors.Wrapf(err, "Unable to parse HTTP response for %s %s", res.Request.Method, res.Request.URL)
}
return nil
}