Skip to content

Commit

Permalink
refactor updateTokenOnce to use requireOK
Browse files Browse the repository at this point in the history
Signed-off-by: FFMMM <FFMMM@users.noreply.github.com>
  • Loading branch information
FFMMM committed Sep 27, 2021
1 parent 437200d commit 119efd3
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package api

import (
"bufio"
"bytes"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -602,6 +601,7 @@ func (a *Agent) AgentHealthServiceByIDOpts(serviceID string, q *QueryOptions) (s
r.setQueryOptions(q)
r.params.Add("format", "json")
r.header.Set("Accept", "application/json")
// not a lot of value in wrapping the doRequest call in a requireHttpCodes call
_, resp, err := a.c.doRequest(r)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -641,6 +641,7 @@ func (a *Agent) AgentHealthServiceByNameOpts(service string, q *QueryOptions) (s
r.setQueryOptions(q)
r.params.Add("format", "json")
r.header.Set("Accept", "application/json")
// not a lot of value in wrapping the doRequest call in a requireHttpCodes call
_, resp, err := a.c.doRequest(r)
if err != nil {
return "", nil, err
Expand Down Expand Up @@ -1237,15 +1238,15 @@ func (a *Agent) updateTokenOnce(target, token string, q *WriteOptions) (*WriteMe
if err != nil {
return nil, 0, err
}
defer closeResponseBody(resp)

wm := &WriteMeta{RequestTime: rtt}

if resp.StatusCode != 200 {
var buf bytes.Buffer
io.Copy(&buf, resp.Body)
return wm, resp.StatusCode, fmt.Errorf("Unexpected response code: %d (%s)", resp.StatusCode, buf.Bytes())
_, _, err = requireOK(rtt, resp, err)

if err != nil {
return wm, resp.StatusCode, err
}
defer closeResponseBody(resp)

return wm, resp.StatusCode, nil
}

0 comments on commit 119efd3

Please sign in to comment.