// Sends a generic request that gets back a generic response, or error. This returns a ChromeResponse
// object.
7681
func SendDefaultRequest(target ChromeTargeter, sendCh chan<- *Message, paramRequest *ParamRequest) (*ChromeResponse, error) {
	req := &ChromeRequest{Id: paramRequest.Id, Method: paramRequest.Method, Params: paramRequest.Params}
	data, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}

	recvCh := make(chan *Message, 1)
	sendMsg := &Message{ReplyCh: recvCh, Id: paramRequest.Id, Data: []byte(data)}

	timeout := time.NewTimer(target.GetApiTimeout())
	defer timeout.Stop()

	select {
	case sendCh <- sendMsg:
		timeout.Stop()
	case <-timeout.C:
		return nil, &ChromeApiTimeoutErr{}
	case <-target.GetDoneCh():
		return nil, &ChromeDoneErr{}
	}

	timeout.Reset(target.GetApiTimeout())

	var resp *Message
	select {
	case <-timeout.C:
		return nil, &ChromeApiTimeoutErr{}
	case resp = <-recvCh:
		timeout.Stop()
	case <-target.GetDoneCh():
		return nil, &ChromeDoneErr{}
	}

	if resp == nil || resp.Data == nil {
		return nil, &ChromeEmptyResponseErr{}
	}

	chromeResponse := &ChromeResponse{}
	err = json.Unmarshal(resp.Data, chromeResponse)
	if err != nil {
		return nil, err
	}
	return chromeResponse, nil
}
594833
func (service *HTTPService) ServeHTTP(
	response http.ResponseWriter, request *http.Request) {
	if service.clientAccessPolicyXMLHandler(response, request) ||
		service.crossDomainXMLHandler(response, request) {
		return
	}
	context := service.acquireContext()
	context.InitHTTPContext(service, response, request)
	var resp []byte
	err := service.sendHeader(context)
	if err == nil {
		switch request.Method {
		case "GET":
			if service.GET {
				resp = service.DoFunctionList(context)
			} else {
				response.WriteHeader(403)
			}
		case "POST":
			var req []byte
			if req, err = readAllFromHTTPRequest(request); err == nil {
				resp = service.Handle(req, context)
			}
		}
	}
	if err != nil {
		resp = service.EndError(err, context)
	}
	service.releaseContext(context)
	response.Header().Set("Content-Length", util.Itoa(len(resp)))
	response.Write(resp)
}
991916
func (c *Connection) Send(req Request, wantResponse bool) (res Response, err error) {
	if c.Closed {
		return res, errors.New(`Cannot send on closed connection`)
	}
	req.JsonRPC = `2.0`
	res = Response{}

	c.writeWait.Add(1)
	if wantResponse == true {
		c.responseLock.Lock()
		id := c.requestID
		ch := make(chan *rpcResponse)
		c.responses[id] = &ch
		c.requestID++
		c.responseLock.Unlock()
		req.Id = &id

		log.WithField(`request`, req).Debug(`Sending Kodi Request (response desired)`)
		c.write <- req
		res.channel = &ch
		res.Pending = true
	} else {
		log.WithField(`request`, req).Debug(`Sending Kodi Request (response undesired)`)
		c.write <- req
		res.Pending = false
	}
	c.writeWait.Done()

	return
}
69739
func (dc *DispatcherClient) Send(funcName string, request interface{}) error {
	req := dc.getRequest(funcName, request)
	return dc.c.Send(req)
}
322733
func (_m *Stub) Send(server string, conf common.Config, req *discovery.Request) (cmd.ServiceResponse, error) {
	ret := _m.Called(server, conf, req)

	var r0 cmd.ServiceResponse
	if rf, ok := ret.Get(0).(func(string, common.Config, *discovery.Request) cmd.ServiceResponse); ok {
		r0 = rf(server, conf, req)
	} else {
		if ret.Get(0) != nil {
			r0 = ret.Get(0).(cmd.ServiceResponse)
		}
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(string, common.Config, *discovery.Request) error); ok {
		r1 = rf(server, conf, req)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
10352
func SendHttp(apiKey string, m HttpMessage) (*HttpResponse, error) {
	c := &httpGcmClient{httpAddress, &http.Client{}, "0"}
	b := newExponentialBackoff()
	return sendHttp(apiKey, m, c, b)
}
128907
func (rc *Response) Send(response *restful.Response) {
	rc.SendWithHeader(http.StatusOK, response)
}
563528
func (cerr *ChromeRequestErr) Error() string {
	return "request " + strconv.FormatInt(cerr.Resp.Id, 10) + " failed, code: " + strconv.FormatInt(cerr.Resp.Error.Code, 10) + " msg: " + cerr.Resp.Error.Message
}
449401
func NewGetErrorResponse(key string, err error) GetResponse {
	resp := &genericResponse{
		err:           err,
		allowNotFound: true,
	}
	resp.item.Key = key
	return resp
}
106886
func (c *ChromeTarget) dispatchResponse(msg []byte) {
	f := &responseHeader{}
	err := json.Unmarshal(msg, f)
	if err != nil {
		c.debugf("error reading response data from chrome target: %v\n", err)
	}

	c.replyLock.Lock()

	if r, ok := c.replyDispatcher[f.Id]; ok {
		delete(c.replyDispatcher, f.Id)
		c.replyLock.Unlock()
		c.debugf("%d dispatching\n", f.Id)
		c.dispatchWithTimeout(r, f.Id, msg)
		return
	}
	c.replyLock.Unlock()

	c.checkTargetDisconnected(f.Method)

	c.eventLock.RLock()
	if r, ok := c.eventDispatcher[f.Method]; ok {
		c.eventLock.RUnlock()
		c.debugf("dispatching %s event: %s\n", f.Method, string(msg))
		go r(c, msg)
		return

	}
	c.eventLock.RUnlock()

	if c.debugEvents == true {
		log.Printf("\n\nno event recv bound for: %s\n", f.Method)
		log.Printf("data: %s\n\n", string(msg))
	}
}
304628
func (b *BulkIndexer) Send(buf *bytes.Buffer) error {
	type responseStruct struct {
		Took   int64                    `json:"took"`
		Errors bool                     `json:"errors"`
		Items  []map[string]interface{} `json:"items"`
	}

	response := responseStruct{}

	body, err := b.conn.DoCommand("POST", fmt.Sprintf("/_bulk?refresh=%t", b.Refresh), nil, buf)

	if err != nil {
		atomic.AddUint64(&b.numErrors, 1)
		return err
	}
	// check for response errors, bulk insert will give 200 OK but then include errors in response
	jsonErr := json.Unmarshal(body, &response)
	if jsonErr == nil {
		if response.Errors {
			atomic.AddUint64(&b.numErrors, uint64(len(response.Items)))
			return fmt.Errorf("Bulk Insertion Error. Failed item count [%d]", len(response.Items))
		}
	}
	return nil
}
316101
func (c Client) SendRequest(r *MailRequest) (string, error) {
	// https://help.aliyun.com/document_detail/29440.html?spm=5176.doc29444.2.1.hYchmR
	ps := parameters.NewParameterSet()
	ps.Add("Format", "JSON")
	ps.Add("Version", "2015-11-23")
	ps.Add("AccessKeyId", c.AccessKeyID)
	ps.Add("SignatureMethod", "HMAC-SHA1")
	ps.Add("Timestamp", time.Now().UTC().Format("2006-01-02T15:04:05Z"))
	ps.Add("SignatureVersion", "1.0")
	id, _ := uuid.NewV4()
	ps.Add("SignatureNonce", id.String())

	ps.Add("Action", r.Action)
	ps.Add("AccountName", r.AccountName)
	if r.ReplyToAddress {
		ps.Add("ReplyToAddress", "true")
	} else {
		ps.Add("ReplyToAddress", "false")
	}
	ps.Add("AddressType", strconv.Itoa(r.AddressType))
	ps.Add("ToAddress", r.ToAddress)
	if len(r.FromAlias) > 0 {
		ps.Add("FromAlias", r.FromAlias)
	}
	if len(r.Subject) > 0 {
		ps.Add("Subject", r.Subject)
	}
	if len(r.HtmlBody) > 0 {
		ps.Add("HtmlBody", r.HtmlBody)
	}
	if len(r.TextBody) > 0 {
		ps.Add("TextBody", r.TextBody)
	}

	ps.Add("Signature", ps.Sign(ps.GetStringToSign(), c.AccessKeySecret))
	resp, err := http.Get(baseURL + "?" + ps.Concatenate())
	if err != nil {
		return "", err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return "failed to read response", err
	}
	if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
		return string(body), errors.New("response code isn't ok")
	}

	var v map[string]string
	dec := json.NewDecoder(bytes.NewReader(body))
	if err := dec.Decode(&v); err != nil {
		return "", err
	}
	if _, ok := v["Code"]; ok {
		return "", errors.New("response doesn't contain Code")
	}
	return string(body), nil
}
1265450
func Respond(r *http.Response, decorators ...RespondDecorator) error {
	if r == nil {
		return nil
	}
	return CreateResponder(decorators...).Respond(r)
}
844333
func (c *Client) Send(o ...Modifier) (*http.Response, error) {
	// Initialize
	r := c.createRequest()

	// Run all the modifiers
	for _, f := range o {
		err := f(r)
		if err != nil {
			return nil, err
		}
	}

	rChan := make(chan *poolResponse)
	preq := &poolRequest{r, rChan}

	c.pool <- preq

	presp := <-rChan
	close(rChan)

	return presp.resp, presp.err
}
1268158
func SendResponse(req Request, response Response) error {
	return SendResponseWithClient(http.DefaultClient, req, response)
}
951892
func (c *Client) Send(req *Request, res Response) error {
	// TODO: handle retry, timeouts, etc.
	return c.send(req, res)
}
892032
func ResponseHandler(w http.ResponseWriter, r *http.Request, data []byte) {
	// Get callback from request
	callback := r.URL.Query().Get("callback")
	// If callback is not empty then
	if callback != "" {
		// Response for JSONP
		w.Header().Set("Content-Type", "application/javascript")
		fmt.Fprintf(w, "%s(%s)", callback, data)
	} else {
		// Otherwise JSON
		w.Header().Set("Content-Type", "application/json")
		w.Write(data)
	}
}