コード例 #1
0
ファイル: client.go プロジェクト: sourabhg/goshire
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
}
コード例 #2
0
ファイル: client.go プロジェクト: trendrr/goshire
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
}