// NewBytesResponse creates an *http.Response with a body based on the given bytes.  Also accepts
// an http status code.
769844
func NewBytesResponse(status int, body []byte) *http.Response {
	return &http.Response{
		Status:        strconv.Itoa(status),
		StatusCode:    status,
		Body:          NewRespBodyFromBytes(body),
		Header:        http.Header{},
		ContentLength: -1,
	}
}
1061681
func NewResponse() *Response {
	r := &Response{
		Code:        200,
		HeaderMap:   http.Header{},
		body:        nil,
		closeNotify: make(chan bool, 1),
		bodyLock:    new(sync.RWMutex),
		closedLock:  new(sync.RWMutex),
		headersLock: new(sync.RWMutex),
	}

	r.bodyLock.Lock()
	r.closedLock.Lock()
	r.headersLock.Lock()
	return r
}
1001614
func GetResponseAndBytes(url string) (*http.Response, []byte, error) {
	resp, err := http.Get(url)
	if err != nil {
		return resp, []byte{}, err
	}
	bytes, err := ResponseBody(resp)
	return resp, bytes, err
}
1250370
func WriteTextResponse(w http.ResponseWriter, status int, body string) {
	w.Header().Set("Content-Type", "text/plain; charset=utf-8")
	w.Header().Set("X-Content-Type-Options", "nosniff")
	w.WriteHeader(status)
	w.Write([]byte(body))
}
486575
func NewBytes(X *xgbutil.XUtil, bs []byte) (*Image, error) {
	img, _, err := image.Decode(bytes.NewReader(bs))
	if err != nil {
		return nil, err
	}
	return NewConvert(X, img), nil
}
728865
func validateResponse(r *http.Response) error {
	if c := r.StatusCode; 200 <= c && c <= 299 {
		return nil
	}

	bodyBytes, _ := ioutil.ReadAll(r.Body)
	bodyString := string(bodyBytes)
	m := &errorJSONResponse{}
	err := json.Unmarshal([]byte(bodyString), &m)
	if err != nil {
		return err
	}

	return m.Error
}
1019864
func MemResponseBodyBytes(m int64) optSetter {
	return func(b *Buffer) error {
		if m < 0 {
			return fmt.Errorf("mem bytes should be >= 0 got %d", m)
		}
		b.memResponseBodyBytes = m
		return nil
	}
}
955137
func newResponse(r *http.Response) *Response {
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		body = []byte(`Error reading body:` + err.Error())
	}

	return &Response{r, body}
}
506158
func JSONResponse(w http.ResponseWriter, code int, data interface{}) {
	w.Header()["Content-Type"] = []string{"application/json"}
	w.WriteHeader(code)
	switch code {
	case 204, 304:
		/* no body */
	default:
		if data == nil {
			data = ResourceError{code, "Server Error"}
		}
		b, e := json.MarshalIndent(data, "", "  ")
		if e != nil {
			code = http.StatusInternalServerError
			b, _ = json.MarshalIndent(ResourceError{500, "Server Error"}, "", "  ")
		}
		fmt.Fprintf(w, "%s\n", string(b))
	}
}
361153
func NewErrorResponse(code int, err interface{}, context string) *ErrorResponse {
	var msg string

	switch err := err.(type) {
	case error:
		msg = err.Error()
	case string:
		msg = err
	default:
		return nil
	}

	return &ErrorResponse{
		Err: []APIError{
			{
				Code:          code,
				InternalError: code == 500,
				Msg:           msg,
				Context:       context,
			},
		},
	}
}
915257
func newResponse(r *http.Response) *Response {
	body, _ := ioutil.ReadAll(r.Body)
	response := &Response{
		Status: r.Status,
		StatusCode: r.StatusCode,
		Body: string(body),
		ContentLength: r.ContentLength,
		}
	return response
}
478686
func NewResponse(success bool, message string, data interface{}) *Response {
	return &Response{
		Success: success,
		Message: message,
		Data:    data,
	}
}
1213391
func (r *Response) Bytes() []byte {
	if r.Error != nil {
		return nil
	}

	r.populateResponseByteBuffer()

	// Are we still empty?
	if r.buffer.Len() == 0 {
		return nil
	}
	return r.buffer.Bytes()
}
116372
func NewResponse(body []byte, headers http.Header) *Response {
	return &Response{body: body, Headers: headers}
}
370158
func newResponse(r *http.Response) *Response {
	response := &Response{Response: r}
	response.populatePageValues()
	response.Rate = parseRate(r)
	return response
}
1200949
func NewErrorResponse(message string, code int) Response {
	return Response{
		Success:  false,
		Result:   nil,
		Errors:   []ResponseMessage{{code, message}},
		Messages: []ResponseMessage{},
	}
}
220308
func EncodeResponse(w http.ResponseWriter, res interface{}, err bool) {
	w.Header().Set("Content-Type", DefaultContentTypeV1_1)
	if err {
		w.WriteHeader(http.StatusInternalServerError)
	}
	json.NewEncoder(w).Encode(res)
}