-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathitems.go
More file actions
110 lines (100 loc) · 3.01 KB
/
items.go
File metadata and controls
110 lines (100 loc) · 3.01 KB
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
package hackernews
import (
"context"
"encoding/json"
"fmt"
"strconv"
)
type Item struct {
ID int `json:"id"`
Deleted bool `json:"deleted"`
Type string `json:"type"`
By string `json:"by"`
Time int `json:"time"`
Text string `json:"text"`
Dead bool `json:"dead"`
Parent int `json:"parent"`
Kids []int `json:"kids"`
URL string `json:"url"`
Score int `json:"score"`
Title string `json:"title"`
Parts []int `json:"parts"`
Descendants int `json:"descendants"`
}
func (c *Client) GetItem(ctx context.Context, id int) (Item, error) {
path := fmt.Sprintf("/v0/item/%d.json", id)
b, err := c.get(ctx, path)
if err != nil {
return Item{}, fmt.Errorf("while getting item: %w", err)
}
item := Item{}
err = json.Unmarshal(b, &item)
return item, err
}
func (c *Client) MaxItemID(ctx context.Context) (int, error) {
b, err := c.get(ctx, "/v0/maxitem.json")
if err != nil {
return 0, fmt.Errorf("while getting max item: %w", err)
}
return strconv.Atoi(string(b))
}
// TopStories returns up to 500 of the latest top stories.
func (c *Client) TopStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/topstories.json")
if err != nil {
return nil, fmt.Errorf("while getting top stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}
// NewStories returns up to 500 of the newest stories.
func (c *Client) NewStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/newstories.json")
if err != nil {
return nil, fmt.Errorf("while getting new stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}
// BestStories returns up to 500 of the best stories.
func (c *Client) BestStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/beststories.json")
if err != nil {
return nil, fmt.Errorf("while getting best stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}
// AskStories returns up to 200 of the latest ask stories.
func (c *Client) AskStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/askstories.json")
if err != nil {
return nil, fmt.Errorf("while getting ask stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}
// ShowStories returns up to 200 of the latest show stories.
func (c *Client) ShowStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/showstories.json")
if err != nil {
return nil, fmt.Errorf("while getting show stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}
// JobStories returns up to 200 of the latest job stories.
func (c *Client) JobStories(ctx context.Context) ([]int, error) {
b, err := c.get(ctx, "/v0/jobstories.json")
if err != nil {
return nil, fmt.Errorf("while getting job stories: %w", err)
}
var stories []int
err = json.Unmarshal(b, &stories)
return stories, err
}