Skip to content

Commit

Permalink
Minimize diff with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaptic committed Aug 19, 2024
1 parent b8ddb2e commit 60e435a
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions clients/stellarcore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ func (c *Client) Upgrade(ctx context.Context, version int) (err error) {
}
defer drainReponse(hresp, true, &err) //nolint:errcheck

if !(hresp.StatusCode >= 200 && hresp.StatusCode < 300) {
err = errors.New("http request failed with non-200 status code")
return
if hresp.StatusCode < 200 || hresp.StatusCode >= 300 {
return errors.New("http request failed with non-200 status code")
}
return nil
}
Expand Down Expand Up @@ -182,8 +181,15 @@ func (c *Client) SubmitTransaction(ctx context.Context, envelope string) (resp *
}

var hresp *http.Response
hresp, err = c.getResponse(req)
hresp, err = c.http().Do(req)
if err != nil {
err = errors.Wrap(err, "http request errored")
return
}
defer drainReponse(hresp, true, &err) //nolint:errcheck

if hresp.StatusCode < 200 || hresp.StatusCode >= 300 {
err = errors.New("http request failed with non-200 status code")
return
}

Expand Down Expand Up @@ -222,7 +228,6 @@ func (c *Client) WaitForNetworkSync(ctx context.Context) error {

// ManualClose closes a ledger when Core is running in `MANUAL_CLOSE` mode
func (c *Client) ManualClose(ctx context.Context) (err error) {

q := url.Values{}

var req *http.Request
Expand All @@ -232,9 +237,17 @@ func (c *Client) ManualClose(ctx context.Context) (err error) {
return
}

hresp, err := c.getResponse(req)
var hresp *http.Response
hresp, err = c.http().Do(req)
if err != nil {
return errors.Wrap(err, "http request errored")
err = errors.Wrap(err, "http request errored")
return
}
defer drainReponse(hresp, true, &err) //nolint:errcheck

if hresp.StatusCode < 200 || hresp.StatusCode >= 300 {
err = errors.New("http request failed with non-200 status code")
return
}

// verify there wasn't an exception
Expand Down Expand Up @@ -327,7 +340,8 @@ func (c *Client) makeLedgerKeyRequest(
target interface{},
endpoint string,
ledgerSeq uint32,
keys ...xdr.LedgerKey) error {
keys ...xdr.LedgerKey,
) error {
q, err := buildMultiKeyRequest(keys...)
if err != nil {
return err
Expand All @@ -341,30 +355,19 @@ func (c *Client) makeLedgerKeyRequest(
return err
}

hresp, err := c.getResponse(req)
if err != nil {
return err
}

// wrap returns nil if the inner error is nil
return errors.Wrap(json.NewDecoder(hresp.Body).Decode(&target), "json decode failed")
}

// getResponse is an abstraction method to perform a request and check for a
// non-2xx status code, returning the whole response
func (c *Client) getResponse(req *http.Request) (*http.Response, error) {
var hresp *http.Response
hresp, err := c.http().Do(req)
hresp, err = c.http().Do(req)
if err != nil {
return hresp, errors.Wrap(err, "http request errored")
return errors.Wrap(err, "http request errored")
}
defer drainReponse(hresp, true, &err) //nolint:errcheck

if hresp.StatusCode < 200 || hresp.StatusCode >= 300 {
return hresp, fmt.Errorf("http request failed with non-200 status code (%d)", hresp.StatusCode)
return fmt.Errorf("http request failed with non-200 status code (%d)", hresp.StatusCode)
}

return hresp, nil
// wrap returns nil if the inner error is nil
return errors.Wrap(json.NewDecoder(hresp.Body).Decode(&target), "json decode failed")
}

// buildMultiKeyRequest is a workaround helper because, unfortunately,
Expand All @@ -388,7 +391,7 @@ func buildMultiKeyRequest(keys ...xdr.LedgerKey) (string, error) {
for _, key := range keys {
keyB64, err := key.MarshalBinaryBase64()
if err != nil {
return q.String(), errors.Wrapf(err, "failed to encode LedgerKey")
return q.String(), errors.Wrap(err, "failed to encode LedgerKey")
}
q.WriteString("key=" + url.QueryEscape(keyB64) + "&")
}
Expand Down

0 comments on commit 60e435a

Please sign in to comment.