Ejemplo n.º 1
0
func (wa *webAuth) callURL(url, method string, body interface{}, statusCode int, resJSON interface{}) (res *goreq.Response, err error) {
	req := goreq.Request{
		Method:      method,
		Uri:         url,
		Body:        body,
		CookieJar:   wa.jar,
		Accept:      "application/json",
		Host:        "hub.docker.com",
		ContentType: "application/json",
	}.WithHeader("Referer", "https://hub.docker.com/login/")
	if wa.token != "" {
		req = req.WithHeader("Authorization", fmt.Sprintf("JWT %v", wa.token))

	}
	res, err = req.Do()
	if err != nil {
		return res, wrapError(err, fmt.Sprintf("%v to %v", method, url))
	}
	if statusCode != 0 && res.StatusCode != statusCode {
		return res, wrongResponseError(res,
			fmt.Sprintf("%v to %v should have returned a %v", method, url, statusCode))
	}
	if resJSON == nil {
		return
	}
	err = res.Body.FromJsonTo(resJSON)
	if err != nil {
		return res, wrapError(err, fmt.Sprintf("extracting JSON from %v to %v", method, url))
	}
	return
}