Skip to content

Commit 5ec9cbf

Browse files
author
Allison Pierson
authored
legacy pg: don't try parsing plaintext str as json (#2833)
1 parent 0a198d2 commit 5ec9cbf

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

flypg/http.go

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,51 @@ func newHttpClient(dialer agent.Dialer) *http.Client {
5858
return &http.Client{Transport: logging}
5959
}
6060

61-
func (c *Client) Do(ctx context.Context, method, path string, in, out interface{}) error {
61+
func (c *Client) doRequest(ctx context.Context, method, path string, in interface{}) (io.ReadCloser, error) {
6262
req, err := c.NewRequest(path, method, in)
6363
if err != nil {
64-
return err
64+
return nil, err
6565
}
6666

6767
req = req.WithContext(ctx)
6868

6969
res, err := c.httpClient.Do(req)
7070
if err != nil {
71-
return err
71+
return nil, err
7272
}
7373
defer res.Body.Close()
7474

7575
if res.StatusCode > 299 {
76-
return newError(res.StatusCode, res)
76+
return nil, newError(res.StatusCode, res)
7777
}
7878

79-
if out != nil {
80-
if err := json.NewDecoder(res.Body).Decode(out); err != nil {
81-
return err
82-
}
79+
return res.Body, nil
80+
}
81+
82+
func (c *Client) Do(ctx context.Context, method, path string, in, out interface{}) error {
83+
84+
body, err := c.doRequest(ctx, method, path, in)
85+
86+
if err != nil {
87+
return err
8388
}
89+
if out == nil {
90+
return nil
91+
}
92+
93+
return json.NewDecoder(body).Decode(out)
94+
}
8495

85-
return nil
96+
func (c *Client) DoPlaintext(ctx context.Context, method, path string, in interface{}) (string, error) {
97+
body, err := c.doRequest(ctx, method, path, in)
98+
if err != nil {
99+
return "", err
100+
}
101+
data, err := io.ReadAll(body)
102+
if err != nil {
103+
return "", err
104+
}
105+
return string(data), nil
86106
}
87107

88108
func (c *Client) NewRequest(path string, method string, in interface{}) (*http.Request, error) {

flypg/pg.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,7 @@ func (c *Client) NodeRole(ctx context.Context) (string, error) {
140140

141141
func (c *Client) legacyNodeRole(ctx context.Context) (string, error) {
142142
endpoint := "/flycheck/role"
143-
var out string
144-
err := c.Do(ctx, http.MethodGet, endpoint, nil, &out)
143+
out, err := c.DoPlaintext(ctx, http.MethodGet, endpoint, nil)
145144
if err != nil {
146145
return "", err
147146
}

0 commit comments

Comments
 (0)