// NewJsonResponder creates a Responder from a given body (as an interface{} that is encoded to
// json) and status code.
320645
func NewJsonResponder(status int, body interface{}) (Responder, error) { // nolint: golint
	resp, err := NewJsonResponse(status, body)
	if err != nil {
		return nil, err
	}
	return ResponderFromResponse(resp), nil
}
1307837
func EncodeJSONResponse(_ context.Context, reply string, nc *nats.Conn, response interface{}) error {
	b, err := json.Marshal(response)
	if err != nil {
		return err
	}

	return nc.Publish(reply, b)
}
1081257
func newResponse(data *textproto.MIMEHeader) (*AMIResponse, error) {
	if data.Get("Response") == "" {
		return nil, errors.New("Not Response")
	}
	
	response := &AMIResponse{data.Get("Actionid"),
		data.Get("Response"),
		make(map[string]string)}

	for k, v := range *data {
		if k == "Response" {
			continue
		}
		response.Params[k] = v[0]
	}
	return response, nil
}
775658
func NewResponse(w ServerResponse) (r *Response) {
	r = &Response{Out: OutResponse{Server: w, internalHeader: &RevelHeader{}}}
	r.Out.response = r
	return r
}
690043
func NewResponseError(resp *http.Response) error {
	if resp.StatusCode >= 200 && resp.StatusCode < 400 {
		return nil
	}

	body, err := ReadResponse(resp)
	if err != nil {
		return err
	}

	return errutil.New(body)
}
478686
func NewResponse(success bool, message string, data interface{}) *Response {
	return &Response{
		Success: success,
		Message: message,
		Data:    data,
	}
}
250252
func NewResponseWriter(rw http.ResponseWriter) ResponseWriter {
	newRw := responseWriter{rw, 0, 0, nil}
	if cn, ok := rw.(http.CloseNotifier); ok {
		return &closeNotifyResponseWriter{newRw, cn}
	}
	return &newRw
}
1281567
func newStreamResponseBodyReader(body io.Reader) *streamResponseBodyReader {
	return &streamResponseBodyReader{reader: bufio.NewReader(body)}
}
114517
func newResponse(msgData []byte, endpoint string, rawResponse *[]byte) (*vstResponse, error) {
	// Decode header
	hdr := velocypack.Slice(msgData)
	if err := hdr.AssertType(velocypack.Array); err != nil {
		return nil, driver.WithStack(err)
	}
	//panic("hdr: " + hex.EncodeToString(hdr))
	var hdrLen velocypack.ValueLength
	if l, err := hdr.Length(); err != nil {
		return nil, driver.WithStack(err)
	} else if l < 3 {
		return nil, driver.WithStack(fmt.Errorf("Expected a header of 3 elements, got %d", l))
	} else {
		hdrLen = l
	}

	resp := &vstResponse{
		endpoint: endpoint,
	}
	// Decode version
	if elem, err := hdr.At(0); err != nil {
		return nil, driver.WithStack(err)
	} else if version, err := elem.GetInt(); err != nil {
		return nil, driver.WithStack(err)
	} else {
		resp.Version = int(version)
	}
	// Decode type
	if elem, err := hdr.At(1); err != nil {
		return nil, driver.WithStack(err)
	} else if tp, err := elem.GetInt(); err != nil {
		return nil, driver.WithStack(err)
	} else {
		resp.Type = int(tp)
	}
	// Decode responseCode
	if elem, err := hdr.At(2); err != nil {
		return nil, driver.WithStack(err)
	} else if code, err := elem.GetInt(); err != nil {
		return nil, driver.WithStack(err)
	} else {
		resp.ResponseCode = int(code)
	}
	// Decode meta
	if hdrLen >= 4 {
		if elem, err := hdr.At(3); err != nil {
			return nil, driver.WithStack(err)
		} else if !elem.IsObject() {
			return nil, driver.WithStack(fmt.Errorf("Expected meta field to be of type Object, got %s", elem.Type()))
		} else {
			resp.meta = elem
		}
	}

	// Fetch body directly after hdr
	if body, err := hdr.Next(); err != nil {
		return nil, driver.WithStack(err)
	} else {
		resp.slice = body
		if rawResponse != nil {
			*rawResponse = body
		}
	}
	//fmt.Printf("got response: code=%d, body=%s\n", resp.ResponseCode, hex.EncodeToString(resp.slice))
	return resp, nil
}
1212325
func RestHandleResponse(context *gin.Context, body interface{}, err error) {
	if err == nil {
		context.JSON(200, body)
	} else {
		context.String(400, err.Error())
	}
}
667148
func Respond(status int, body interface{}) *NormalResponse {
	var b []byte
	var err error
	switch t := body.(type) {
	case []byte:
		b = t
	case string:
		b = []byte(t)
	default:
		if b, err = json.Marshal(body); err != nil {
			return Error(500, "body json marshal", err)
		}
	}
	return &NormalResponse{
		body:   b,
		status: status,
		header: make(http.Header),
	}
}
630071
func (c *Context) JSONParser() (*simplejson.Json, error) {
	res := c.Response()
	defer res.Body.Close()
	return simplejson.NewFromReader(res.Body)
}
842421
func NewJiraError(resp *Response, httpError error) error {
	if resp == nil {
		return errors.Wrap(httpError, "No response returned")
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return errors.Wrap(err, httpError.Error())
	}
	jerr := Error{HTTPError: httpError}
	contentType := resp.Header.Get("Content-Type")
	if strings.HasPrefix(contentType, "application/json") {
		err = json.Unmarshal(body, &jerr)
		if err != nil {
			httpError = errors.Wrap(errors.New("Could not parse JSON"), httpError.Error())
			return errors.Wrap(err, httpError.Error())
		}
	} else {
        if httpError == nil {
            return fmt.Errorf("Got Response Status %s:%s", resp.Status, string(body))
        }
		return errors.Wrap(httpError, fmt.Sprintf("%s: %s", resp.Status, string(body)))
	}

	return &jerr
}
542998
func NewAPIError(response *http.Response) APIError {
	// TODO: handle this error
	body, _ := ioutil.ReadAll(response.Body)

	return NewAPIErrorFromBody(response, body)
}
561450
func (c *Client) newResponse(res *http.Response) (*Response, error) {
	r := &Response{
		StatusCode: res.StatusCode,
		Header:     res.Header,
	}
	if res.Body != nil {
		slurp, err := ioutil.ReadAll(res.Body)
		if err != nil {
			return nil, err
		}
		// HEAD requests return a body but no content
		if len(slurp) > 0 {
			if err := c.decoder.Decode(slurp, &r.Body); err != nil {
				return nil, err
			}
		}
	}
	return r, nil
}
72188
func NewCliError(reqType string, hr *gocd.APIResponse, err error) (jerr JSONCliError) {
	data := dataJSONCliError{
		"error": err.Error(),
	}
	if hr != nil {
		data["status"] = hr.HTTP.StatusCode
		data["response-body"] = hr.Body
		data["request-endpoint"] = hr.Request.HTTP.URL.String()

		if hr.HTTP.StatusCode == 404 {
			data["error"] = "Resource not found"
		} else {
			b1, _ := json.Marshal(hr.HTTP.Header)
			b2, _ := json.Marshal(hr.Request.HTTP.Header)
			data["error"] = "An error occurred while retrieving the resource."
			data["response-header"] = string(b1)
			data["request-body"] = hr.Request.Body
			data["request-header"] = string(b2)
		}
	} else {
		data["request"] = reqType
	}
	return JSONCliError{
		data: data,
		resp: hr,
	}
}
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,
			},
		},
	}
}