mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
client: return full http.Response in httpActionDo
This commit is contained in:
parent
5bdf6a4110
commit
d52d836761
@ -47,7 +47,7 @@ type httpClusterClient struct {
|
|||||||
endpoints []*httpClient
|
endpoints []*httpClient
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClusterClient) do(ctx context.Context, act httpAction) (int, []byte, error) {
|
func (c *httpClusterClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
//TODO(bcwaldon): introduce retry logic so all endpoints are attempted
|
//TODO(bcwaldon): introduce retry logic so all endpoints are attempted
|
||||||
return c.endpoints[0].do(ctx, act)
|
return c.endpoints[0].do(ctx, act)
|
||||||
}
|
}
|
||||||
|
@ -43,7 +43,7 @@ type httpAction interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type httpActionDo interface {
|
type httpActionDo interface {
|
||||||
do(context.Context, httpAction) (int, []byte, error)
|
do(context.Context, httpAction) (*http.Response, []byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type roundTripResponse struct {
|
type roundTripResponse struct {
|
||||||
@ -57,7 +57,7 @@ type httpClient struct {
|
|||||||
timeout time.Duration
|
timeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *httpClient) do(ctx context.Context, act httpAction) (int, []byte, error) {
|
func (c *httpClient) do(ctx context.Context, act httpAction) (*http.Response, []byte, error) {
|
||||||
req := act.httpRequest(c.endpoint)
|
req := act.httpRequest(c.endpoint)
|
||||||
|
|
||||||
rtchan := make(chan roundTripResponse, 1)
|
rtchan := make(chan roundTripResponse, 1)
|
||||||
@ -89,9 +89,9 @@ func (c *httpClient) do(ctx context.Context, act httpAction) (int, []byte, error
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
return resp.StatusCode, body, err
|
return resp, body, err
|
||||||
}
|
}
|
||||||
|
@ -78,14 +78,14 @@ func TestHTTPClientDoSuccess(t *testing.T) {
|
|||||||
Body: ioutil.NopCloser(strings.NewReader("foo")),
|
Body: ioutil.NopCloser(strings.NewReader("foo")),
|
||||||
}
|
}
|
||||||
|
|
||||||
code, body, err := c.do(context.Background(), &fakeAction{})
|
resp, body, err := c.do(context.Background(), &fakeAction{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("incorrect error value: want=nil got=%v", err)
|
t.Fatalf("incorrect error value: want=nil got=%v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
wantCode := http.StatusTeapot
|
wantCode := http.StatusTeapot
|
||||||
if wantCode != code {
|
if wantCode != resp.StatusCode {
|
||||||
t.Fatalf("invalid response code: want=%d got=%d", wantCode, code)
|
t.Fatalf("invalid response code: want=%d got=%d", wantCode, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
wantBody := []byte("foo")
|
wantBody := []byte("foo")
|
||||||
|
@ -112,13 +112,13 @@ func (k *httpKeysAPI) Create(key, val string, ttl time.Duration) (*Response, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
|
||||||
code, body, err := k.client.do(ctx, create)
|
resp, body, err := k.client.do(ctx, create)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshalHTTPResponse(code, body)
|
return unmarshalHTTPResponse(resp.StatusCode, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *httpKeysAPI) Get(key string) (*Response, error) {
|
func (k *httpKeysAPI) Get(key string) (*Response, error) {
|
||||||
@ -129,13 +129,13 @@ func (k *httpKeysAPI) Get(key string) (*Response, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), k.timeout)
|
||||||
code, body, err := k.client.do(ctx, get)
|
resp, body, err := k.client.do(ctx, get)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return unmarshalHTTPResponse(code, body)
|
return unmarshalHTTPResponse(resp.StatusCode, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
|
func (k *httpKeysAPI) Watch(key string, idx uint64) Watcher {
|
||||||
@ -169,12 +169,12 @@ type httpWatcher struct {
|
|||||||
|
|
||||||
func (hw *httpWatcher) Next() (*Response, error) {
|
func (hw *httpWatcher) Next() (*Response, error) {
|
||||||
//TODO(bcwaldon): This needs to be cancellable by the calling user
|
//TODO(bcwaldon): This needs to be cancellable by the calling user
|
||||||
code, body, err := hw.client.do(context.Background(), &hw.nextWait)
|
httpresp, body, err := hw.client.do(context.Background(), &hw.nextWait)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
resp, err := unmarshalHTTPResponse(code, body)
|
resp, err := unmarshalHTTPResponse(httpresp.StatusCode, body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -62,13 +62,13 @@ type httpMembersAPI struct {
|
|||||||
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
|
func (m *httpMembersAPI) List() ([]httptypes.Member, error) {
|
||||||
req := &membersAPIActionList{}
|
req := &membersAPIActionList{}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
||||||
code, body, err := m.client.do(ctx, req)
|
resp, body, err := m.client.do(ctx, req)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := assertStatusCode(http.StatusOK, code); err != nil {
|
if err := assertStatusCode(http.StatusOK, resp.StatusCode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,13 +88,13 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
|
|||||||
|
|
||||||
req := &membersAPIActionAdd{peerURLs: urls}
|
req := &membersAPIActionAdd{peerURLs: urls}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
||||||
code, body, err := m.client.do(ctx, req)
|
resp, body, err := m.client.do(ctx, req)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := assertStatusCode(http.StatusCreated, code); err != nil {
|
if err := assertStatusCode(http.StatusCreated, resp.StatusCode); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -109,13 +109,13 @@ func (m *httpMembersAPI) Add(peerURL string) (*httptypes.Member, error) {
|
|||||||
func (m *httpMembersAPI) Remove(memberID string) error {
|
func (m *httpMembersAPI) Remove(memberID string) error {
|
||||||
req := &membersAPIActionRemove{memberID: memberID}
|
req := &membersAPIActionRemove{memberID: memberID}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
|
||||||
code, _, err := m.client.do(ctx, req)
|
resp, _, err := m.client.do(ctx, req)
|
||||||
cancel()
|
cancel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return assertStatusCode(http.StatusNoContent, code)
|
return assertStatusCode(http.StatusNoContent, resp.StatusCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
type membersAPIActionList struct{}
|
type membersAPIActionList struct{}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user