Ejemplo n.º 1
0
func (p *THTTPClient) Read(buf []byte) (int, error) {
	if p.response == nil {
		return 0, thrift.NewTTransportException(
			thrift.NOT_OPEN,
			"Response buffer is empty, no request.",
		)
	}

	n, err := p.response.Body.Read(buf)
	if n > 0 && (err == nil || err == io.EOF) {
		return n, nil
	}

	return n, thrift.NewTTransportExceptionFromError(err)
}
Ejemplo n.º 2
0
func (p *THTTPClient) doRequest() (*http.Response, error) {
	client := &http.Client{Timeout: p.timeout}
	req, err := http.NewRequest("POST", p.url.String(), p.requestBuffer)
	if err != nil {
		return nil, thrift.NewTTransportExceptionFromError(err)
	}
	p.header.Add("Content-Type", "application/x-thrift")
	req.Header = p.header

	response, err := client.Do(req)

	if err != nil {
		return nil, thrift.NewTTransportExceptionFromError(err)
	}

	if response.StatusCode != http.StatusOK {
		return nil, thrift.NewTTransportException(
			thrift.UNKNOWN_TRANSPORT_EXCEPTION,
			"HTTP Response code: "+strconv.Itoa(response.StatusCode),
		)
	}

	return response, nil
}