Exemplo n.º 1
0
//does the actual call, returning the connection and the internal request
func (this *JsonClient) doApiCall(conn *cheshireConn, req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) (*cheshireRequest, error) {
	if conn == nil {
		return nil, fmt.Errorf("Cannot do api call, conn is nil")
	}
	if req.TxnId() == "" {
		req.SetTxnId(NewTxnId())
	}
	r, err := conn.sendRequest(req, responseChan, errorChan)
	return r, err
}
Exemplo n.º 2
0
// Does a synchronous api call.  times out after the requested timeout.
// This will automatically set the txn accept to single
func (this *JsonClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	conn, err := this.connection()
	//retry if necessary
	for i := 0; i < this.Retries && err != nil; i++ {
		time.Sleep(this.RetryPause)
		conn, err = this.connection()
	}
	if err != nil {
		return nil, err
	}

	req.SetTxnAccept("single")
	response, err := this.doApiCallSync(conn, req, timeout)
	return response, err
}
Exemplo n.º 3
0
//does the actual call, returning the connection and the internal request
func (this *JsonClient) doApiCall(req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) (*cheshireRequest, error) {
	conn, err := this.connection()
	if err != nil {
		return nil, err
	}
	if req.TxnId() == "" {
		req.SetTxnId(cheshire.NewTxnId())
	}
	r, err := conn.sendRequest(req, responseChan, errorChan)
	if err == nil {
		this.pool.Return(conn)
	} else {
		this.pool.ReturnBroken(conn)
	}
	return r, err
}
Exemplo n.º 4
0
func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	uri := req.Uri()
	pms, err := req.Params().MarshalURL()
	if err != nil {
		return nil, err
	}
	reqBody := strings.NewReader("")

	if req.Method() == "GET" {
		joiner := "&"
		//add params to the uri
		if !strings.Contains(uri, "?") {
			joiner = "?"
		}
		uri = fmt.Sprintf("%s%s%s", uri, joiner, pms)
	} else {
		reqBody = strings.NewReader(pms)
	}
	url := fmt.Sprintf("http://%s%s", this.Address, uri)
	//convert to an http.Request
	request, err := http.NewRequest(req.Method(), url, reqBody)
	if err != nil {
		return nil, err
	}
	res, err := http.DefaultClient.Do(request)

	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	//convert to a strest response2
	var response = &cheshire.Response{*dynmap.NewDynMap()}
	err = response.UnmarshalJSON(body)
	if err != nil {
		return nil, err
	}
	return response, nil
}
Exemplo n.º 5
0
func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	uri := req.Uri()

	var reqBody io.Reader

	if req.Method() == "GET" {
		joiner := "&"
		//add params to the uri
		if !strings.Contains(uri, "?") {
			joiner = "?"
		}

		pms, err := req.Params().MarshalURL()
		if err != nil {
			return nil, err
		}
		uri = fmt.Sprintf("%s%s%s", uri, joiner, pms)
	} else {
		// set the request body as json
		json, err := req.Params().MarshalJSON()
		if err != nil {
			return nil, err
		}

		// log.Printf("JSON %s", string(json))
		reqBody = bytes.NewReader(json)
	}

	url := fmt.Sprintf("http://%s%s", this.Address, uri)
	//convert to an http.Request
	request, err := http.NewRequest(req.Method(), url, reqBody)

	if req.Method() != "GET" {
		//set the content type
		request.Header.Set("Content-Type", "application/json")
	}

	if err != nil {
		return nil, err
	}
	res, err := http.DefaultClient.Do(request)

	if err != nil {
		return nil, err
	}
	defer res.Body.Close()
	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}

	//convert to a strest response2
	mp := dynmap.New()
	// var response = &cheshire.Response{*dynmap.NewDynMap()}
	err = mp.UnmarshalJSON(body)
	response := cheshire.NewResponseDynMap(mp)
	if err != nil {
		return nil, err
	}
	return response, nil
}
Exemplo n.º 6
0
// Does a synchronous api call.  times out after the requested timeout.
// This will automatically set the txn accept to single
func (this *JsonClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	req.SetTxnAccept("single")
	response, err := this.doApiCallSync(req, timeout)
	return response, err
}