Beispiel #1
0
func (r *JSON) perform(req *http.Request) *JSONResponse {
	if r.Willy.HmaxSecret != "" {
		hmax.SignRequest(req, []byte(r.Willy.HmaxSecret))
	}
	res := &JSONResponse{&Response{httptest.NewRecorder()}}
	for key, value := range r.Headers {
		req.Header.Set(key, value)
	}
	req.Header.Set("Cookie", r.Willy.Cookies)
	r.Willy.ServeHTTP(res, req)
	r.Willy.Cookies = res.Header().Get("Set-Cookie")
	return res
}
Beispiel #2
0
func (r *Request) perform(req *http.Request) *Response {
	if r.Willy.HmaxSecret != "" {
		hmax.SignRequest(req, []byte(r.Willy.HmaxSecret))
	}
	if r.Username != "" || r.Password != "" {
		req.SetBasicAuth(r.Username, r.Password)
	}
	res := &Response{httptest.NewRecorder()}
	for key, value := range r.Headers {
		req.Header.Set(key, value)
	}
	req.Header.Set("Cookie", r.Willy.Cookies)
	r.Willy.ServeHTTP(res, req)
	c := res.HeaderMap["Set-Cookie"]
	r.Willy.Cookies = strings.Join(c, ";")
	return res
}
Beispiel #3
0
func (h *Host) makeRequest(model interface{}, req *http.Request) (*http.Response, error) {
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("X-Authorization", h.Token)

	err := hmax.SignRequest(req, []byte(h.SecretKey))
	if err != nil {
		return nil, err
	}

	cl := newHTTPClient()

	res, err := cl.Do(req)
	if err != nil {
		return res, err
	}

	d := json.NewDecoder(res.Body)
	code := res.StatusCode
	switch code {
	case 401, 404, 500:
		se := SimpleError{}
		err = d.Decode(&se)
		if err != nil {
			return res, err
		}
		return res, se
	case 422:
		ve := ValidationErrors{}
		err = d.Decode(&ve)
		if err != nil {
			return res, err
		}
		return res, ve
	default:
		if h.Primary {
			d.Decode(model)
			if err != nil {
				return res, err
			}
		}
	}

	return res, err
}