refactor: decode JSON io.Reader stream

This commit is contained in:
Mohamed Sohail 2023-01-16 08:31:46 +00:00
parent ad5ff05a32
commit f2d9154da0
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"fmt" "fmt"
"io"
"net/http" "net/http"
"time" "time"
@ -52,14 +51,9 @@ func (f *Graphql) Block(ctx context.Context, blockNumber uint64) (FetchResponse,
if resp.StatusCode >= http.StatusBadRequest { if resp.StatusCode >= http.StatusBadRequest {
return FetchResponse{}, fmt.Errorf("error fetching block %s", resp.Status) return FetchResponse{}, fmt.Errorf("error fetching block %s", resp.Status)
} }
defer resp.Body.Close()
out, err := io.ReadAll(resp.Body) if err := json.NewDecoder(resp.Body).Decode(&fetchResponse); err != nil {
_ = resp.Body.Close()
if err != nil {
return FetchResponse{}, nil
}
if err := json.Unmarshal(out, &fetchResponse); err != nil {
return FetchResponse{}, err return FetchResponse{}, err
} }