mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
After commit 49109c77f719cedc5c7f15bcf255f952d2c3b90d the HackerNews widget did not work as expected. It failed with a error message "http: read on closed response body". This commit fixes the underlying reader issue by reading the whole request body within the apiRequest function and returning the []bytes of the response instead of *http.Response. If we want to continue to return *http.Response this would lead to the problem that the caller (GetStory() and GetStories()) always need to close the Request Body. The ways before this MR is a bit problematic because the defer gets executed when the function is "done", so it closes the Body already before the utils.ParseJSON can read it. Signed-off-by: Lukas Kämmerling <lukas.kaemmerling@hetzner-cloud.de>
79 lines
1.4 KiB
Go
79 lines
1.4 KiB
Go
package hackernews
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/wtfutil/wtf/utils"
|
|
)
|
|
|
|
func GetStories(storyType string) ([]int, error) {
|
|
var storyIds []int
|
|
|
|
switch strings.ToLower(storyType) {
|
|
case "new", "top", "job", "ask":
|
|
resp, err := apiRequest(storyType + "stories")
|
|
if err != nil {
|
|
return storyIds, err
|
|
}
|
|
|
|
err = utils.ParseJSON(&storyIds, bytes.NewReader(resp))
|
|
if err != nil {
|
|
return storyIds, err
|
|
}
|
|
}
|
|
|
|
return storyIds, nil
|
|
}
|
|
|
|
func GetStory(id int) (Story, error) {
|
|
var story Story
|
|
|
|
resp, err := apiRequest("item/" + strconv.Itoa(id))
|
|
if err != nil {
|
|
return story, err
|
|
}
|
|
|
|
err = utils.ParseJSON(&story, bytes.NewReader(resp))
|
|
if err != nil {
|
|
return story, err
|
|
}
|
|
|
|
return story, nil
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
var (
|
|
apiEndpoint = "https://hacker-news.firebaseio.com/v0/"
|
|
)
|
|
|
|
func apiRequest(path string) ([]byte, error) {
|
|
req, err := http.NewRequest("GET", apiEndpoint+path+".json", nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
httpClient := &http.Client{}
|
|
resp, err := httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return nil, fmt.Errorf(resp.Status)
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return body, nil
|
|
}
|