1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/hackernews/client.go
Chris Cummer 1bfca29d17
WTF-657 Add spec coverage for cfg/common_settings.go (#728)
* WTF-657 Add spec coverage for cfg/common_settings.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/position_validation.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for cfg/validations.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for checklist/checklist_item.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/conversions.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Get rid of utils.Home() function

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/homedir.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Add spec coverage for utils/text.go

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-657 Clean up utils/utils.go

Signed-off-by: Chris Cummer <chriscummer@me.com>
2019-10-30 17:35:00 -07:00

68 lines
1.2 KiB
Go

package hackernews
import (
"fmt"
"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, resp.Body)
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, resp.Body)
if err != nil {
return story, err
}
return story, nil
}
/* -------------------- Unexported Functions -------------------- */
var (
apiEndpoint = "https://hacker-news.firebaseio.com/v0/"
)
func apiRequest(path string) (*http.Response, error) {
req, err := http.NewRequest("GET", apiEndpoint+path+".json", nil)
httpClient := &http.Client{}
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
if resp.StatusCode < 200 || resp.StatusCode > 299 {
return nil, fmt.Errorf(resp.Status)
}
return resp, nil
}