aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/ibd/auth.go
diff options
context:
space:
mode:
authorGravatar Anshul Gupta <ansg191@anshulg.com> 2024-08-06 17:16:35 -0700
committerGravatar Anshul Gupta <ansg191@anshulg.com> 2024-08-06 17:16:35 -0700
commit961f9e0a76c3cfe9ae92ca8da0531790e0610b69 (patch)
treef6de4ed36c3f48ee94ecd524dedeb0d7c84b72e5 /backend/internal/ibd/auth.go
parent641c81198d7fed7138bb482f226e54bd703094ab (diff)
downloadibd-trader-961f9e0a76c3cfe9ae92ca8da0531790e0610b69.tar.gz
ibd-trader-961f9e0a76c3cfe9ae92ca8da0531790e0610b69.tar.zst
ibd-trader-961f9e0a76c3cfe9ae92ca8da0531790e0610b69.zip
Modify IBD to accept various transport backends
This allows IBD to try using faster and cheaper transports first with fallback to more reliable and expensive transports later.
Diffstat (limited to 'backend/internal/ibd/auth.go')
-rw-r--r--backend/internal/ibd/auth.go54
1 files changed, 38 insertions, 16 deletions
diff --git a/backend/internal/ibd/auth.go b/backend/internal/ibd/auth.go
index 7dff3a7..f09f3f7 100644
--- a/backend/internal/ibd/auth.go
+++ b/backend/internal/ibd/auth.go
@@ -7,6 +7,7 @@ import (
"encoding/json"
"errors"
"fmt"
+ "io"
"net/http"
"strings"
@@ -51,16 +52,23 @@ func (c *Client) getLoginPage(ctx context.Context) (*authConfig, error) {
if err != nil {
return nil, err
}
-
- if resp.Result.StatusCode != http.StatusOK {
+ defer func(Body io.ReadCloser) {
+ _ = Body.Close()
+ }(resp.Body)
+
+ if resp.StatusCode != http.StatusOK {
+ content, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response body: %w", err)
+ }
return nil, fmt.Errorf(
"unexpected status code %d: %s",
- resp.Result.StatusCode,
- resp.Result.Content,
+ resp.StatusCode,
+ string(content),
)
}
- node, err := html.Parse(strings.NewReader(resp.Result.Content))
+ node, err := html.Parse(resp.Body)
if err != nil {
return nil, err
}
@@ -113,18 +121,25 @@ func (c *Client) sendAuthRequest(ctx context.Context, cfg *authConfig, username,
if err != nil {
return "", "", err
}
+ defer func(Body io.ReadCloser) {
+ _ = Body.Close()
+ }(resp.Body)
- if resp.Result.StatusCode == http.StatusUnauthorized {
+ if resp.StatusCode == http.StatusUnauthorized {
return "", "", ErrBadCredentials
- } else if resp.Result.StatusCode != http.StatusOK {
+ } else if resp.StatusCode != http.StatusOK {
+ content, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return "", "", fmt.Errorf("failed to read response body: %w", err)
+ }
return "", "", fmt.Errorf(
"unexpected status code %d: %s",
- resp.Result.StatusCode,
- resp.Result.Content,
+ resp.StatusCode,
+ string(content),
)
}
- node, err := html.Parse(strings.NewReader(resp.Result.Content))
+ node, err := html.Parse(resp.Body)
if err != nil {
return "", "", err
}
@@ -145,19 +160,26 @@ func (c *Client) sendPostAuth(ctx context.Context, token, params string) (*http.
if err != nil {
return nil, err
}
-
- if resp.Result.StatusCode != http.StatusOK {
+ defer func(Body io.ReadCloser) {
+ _ = Body.Close()
+ }(resp.Body)
+
+ if resp.StatusCode != http.StatusOK {
+ content, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read response body: %w", err)
+ }
return nil, fmt.Errorf(
"unexpected status code %d: %s",
- resp.Result.StatusCode,
- resp.Result.Content,
+ resp.StatusCode,
+ string(content),
)
}
// Extract cookie
- for _, cookie := range resp.Result.Cookies {
+ for _, cookie := range resp.Cookies() {
if cookie.Name == cookieName {
- return cookie.ToHTTPCookie()
+ return cookie, nil
}
}