diff --git a/modules/hackernews/client.go b/modules/hackernews/client.go index 91034e83..6cf4af99 100644 --- a/modules/hackernews/client.go +++ b/modules/hackernews/client.go @@ -1,7 +1,9 @@ package hackernews import ( + "bytes" "fmt" + "io/ioutil" "net/http" "strconv" "strings" @@ -19,7 +21,7 @@ func GetStories(storyType string) ([]int, error) { return storyIds, err } - err = utils.ParseJSON(&storyIds, resp.Body) + err = utils.ParseJSON(&storyIds, bytes.NewReader(resp)) if err != nil { return storyIds, err } @@ -36,7 +38,7 @@ func GetStory(id int) (Story, error) { return story, err } - err = utils.ParseJSON(&story, resp.Body) + err = utils.ParseJSON(&story, bytes.NewReader(resp)) if err != nil { return story, err } @@ -50,7 +52,7 @@ var ( apiEndpoint = "https://hacker-news.firebaseio.com/v0/" ) -func apiRequest(path string) (*http.Response, error) { +func apiRequest(path string) ([]byte, error) { req, err := http.NewRequest("GET", apiEndpoint+path+".json", nil) if err != nil { return nil, err @@ -61,11 +63,16 @@ func apiRequest(path string) (*http.Response, error) { if err != nil { return nil, err } + defer func() { _ = resp.Body.Close() }() if resp.StatusCode < 200 || resp.StatusCode > 299 { return nil, fmt.Errorf(resp.Status) } - return resp, nil + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + return body, nil }