-
Notifications
You must be signed in to change notification settings - Fork 0
/
hkn.go
157 lines (122 loc) · 4.47 KB
/
hkn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
Package hkn is a go module for interacting with Hacker News.
To get started simply import the package, create a client and call methods on the client:
import (
"fmt"
"github.com/lukakerr/hkn"
)
func main() {
client := hkn.NewClient()
// For example, to get an item by id
item, err := client.GetItem(8869)
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", item)
}
*/
package hkn
import (
"errors"
"net/http"
)
// Client represents the hkn client
type Client struct {
BaseURL string
WebURL string
}
// NewClient creates a new hkn client
func NewClient() *Client {
c := Client{
BaseURL: "https://hacker-news.firebaseio.com/v0",
WebURL: "https://news.ycombinator.com",
}
return &c
}
const (
// A JSON suffix for URLs
jsonSuffix = ".json"
)
var (
// ErrFetching represents an error fetching a resource
ErrFetching = errors.New("fetching resource failed")
// ErrEmptyContent represents an error that content provided is empty
ErrEmptyContent = errors.New("content is empty")
// ErrEmptyTitle represents an error that a title provided is empty
ErrEmptyTitle = errors.New("title is empty")
// ErrInvalidAuth represents an error authenticating
ErrInvalidAuth = errors.New("invalid username or password")
// ErrFetchingActionURL represents an error fetching an action URL
ErrFetchingActionURL = errors.New("fetching action URL failed")
// ErrInvalidNumber represents an error that a number provided is invalid
ErrInvalidNumber = errors.New("invalid number")
)
// GetItem returns a single item given an id
func (c *Client) GetItem(id int) (Item, error) {
return getItem(id, c.BaseURL)
}
// GetItems returns a slice of items given a number of ids
func (c *Client) GetItems(ids []int) (Items, error) {
return getItems(ids, c.BaseURL)
}
// GetMaxItemID returns the most recent item id
func (c *Client) GetMaxItemID() (int, error) {
return getMaxItemID(c.BaseURL)
}
// GetUpdates returns the latest item and profile updates
func (c *Client) GetUpdates() (Updates, error) {
return getUpdates(c.BaseURL)
}
// GetUser returns a user given an id
func (c *Client) GetUser(id string) (User, error) {
return getUser(id, c.BaseURL)
}
// Login a user given a username and password and get back an authentication cookie
func (c *Client) Login(username string, password string) (*http.Cookie, error) {
return login(username, password, c.WebURL)
}
// GetTopStories returns top stories given a number
func (c *Client) GetTopStories(number int) ([]int, error) {
return getTopStories(number, c.BaseURL)
}
// GetNewStories returns new stories given a number
func (c *Client) GetNewStories(number int) ([]int, error) {
return getNewStories(number, c.BaseURL)
}
// GetBestStories returns best stories given a number
func (c *Client) GetBestStories(number int) ([]int, error) {
return getBestStories(number, c.BaseURL)
}
// GetLatestAskStories returns latest ask stories given a number
func (c *Client) GetLatestAskStories(number int) ([]int, error) {
return getLatestAskStories(number, c.BaseURL)
}
// GetLatestShowStories returns latest show stories given a number
func (c *Client) GetLatestShowStories(number int) ([]int, error) {
return getLatestShowStories(number, c.BaseURL)
}
// GetLatestJobStories returns latest job stories given a number
func (c *Client) GetLatestJobStories(number int) ([]int, error) {
return getLatestJobStories(number, c.BaseURL)
}
// Upvote an item given an id and cookie and get back whether the upvote was successful
func (c *Client) Upvote(id int, cookie *http.Cookie) (bool, error) {
return upvote(id, cookie, c.WebURL)
}
// Unvote a comment given an id and cookie and get back whether the unvote was successful
func (c *Client) Unvote(id int, cookie *http.Cookie) (bool, error) {
return unvote(id, cookie, c.WebURL)
}
// Comment creates a comment on an item given an id, content and cookie, and returns whether the comment was successful
func (c *Client) Comment(id int, content string, cookie *http.Cookie) (bool, error) {
return comment(id, content, cookie, c.WebURL)
}
// CreateStoryWithURL creates a story with a mandatory title and optional url
func (c *Client) CreateStoryWithURL(title string, url string, cookie *http.Cookie) (bool, error) {
return createStory(title, url, cookie, c.WebURL, "url")
}
// CreateStoryWithText creates a story with a mandatory title and optional text body
func (c *Client) CreateStoryWithText(title string, text string, cookie *http.Cookie) (bool, error) {
return createStory(title, text, cookie, c.WebURL, "text")
}