Example #1
0
File: keys.go Project: ikatson/etcd
func unmarshalSuccessfulKeysResponse(header http.Header, body []byte) (*Response, error) {
	var res Response
	err := codec.NewDecoderBytes(body, new(codec.JsonHandle)).Decode(&res)
	if err != nil {
		return nil, ErrInvalidJSON
	}
	if header.Get("X-Etcd-Index") != "" {
		res.Index, err = strconv.ParseUint(header.Get("X-Etcd-Index"), 10, 64)
		if err != nil {
			return nil, err
		}
	}
	return &res, nil
}
Example #2
0
// Unmarshal parses RawResponse and stores the result in Response
func (rr *RawResponse) Unmarshal() (*Response, error) {
	if rr.StatusCode != http.StatusOK && rr.StatusCode != http.StatusCreated {
		return nil, handleError(rr.Body)
	}

	resp := new(Response)

	err := codec.NewDecoderBytes(rr.Body, new(codec.JsonHandle)).Decode(resp)

	if err != nil {
		return nil, err
	}

	// attach index and term to response
	resp.EtcdIndex, _ = strconv.ParseUint(rr.Header.Get("X-Etcd-Index"), 10, 64)
	resp.RaftIndex, _ = strconv.ParseUint(rr.Header.Get("X-Raft-Index"), 10, 64)
	resp.RaftTerm, _ = strconv.ParseUint(rr.Header.Get("X-Raft-Term"), 10, 64)

	return resp, nil
}