diff options
Diffstat (limited to 'backend/internal/ibd/search.go')
-rw-r--r-- | backend/internal/ibd/search.go | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/backend/internal/ibd/search.go b/backend/internal/ibd/search.go index 23ef08b..341b14b 100644 --- a/backend/internal/ibd/search.go +++ b/backend/internal/ibd/search.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "io" "net/http" "net/url" "time" @@ -37,17 +38,24 @@ func (c *Client) Search(ctx context.Context, symbol string) (database.Stock, err if err != nil { return database.Stock{}, err } + defer func(Body io.ReadCloser) { + _ = Body.Close() + }(resp.Body) - if resp.Result.StatusCode != http.StatusOK { + if resp.StatusCode != http.StatusOK { + content, err := io.ReadAll(resp.Body) + if err != nil { + return database.Stock{}, fmt.Errorf("failed to read response body: %w", err) + } return database.Stock{}, fmt.Errorf( "unexpected status code %d: %s", - resp.Result.StatusCode, - resp.Result.Content, + resp.StatusCode, + string(content), ) } var sr searchResponse - if err = json.Unmarshal([]byte(resp.Result.Content), &sr); err != nil { + if err = json.NewDecoder(resp.Body).Decode(&sr); err != nil { return database.Stock{}, err } |