-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
154 lines (127 loc) · 3.4 KB
/
client.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
package hn
import (
"context"
"fmt"
firebase "firebase.google.com/go/v4"
db "firebase.google.com/go/v4/db"
"google.golang.org/api/option"
)
const (
HackerNewsAPI = "https://hacker-news.firebaseio.com"
APIVersion = "v0"
ItemPath = APIVersion + "/item/%d"
MaxItemPath = APIVersion + "/maxitem"
NewStoriesPath = APIVersion + "/newstories"
UserPath = APIVersion + "/user/%s"
)
/*
Client provides a consistent interface to the HN API.
*/
type Client struct {
db *db.Client
disableEtags bool
lists map[string]IDList
}
/*
DefaultClient creates a new HN client with charactistics that are
appropriate for most users calling the HN API as follows:
- No authentication is required.
- ETags are used to determine whether an item or user has changed.
*/
func DefaultClient(ctx context.Context) (*Client, error) {
return NewClient(ctx, option.WithoutAuthentication()) // , DisableETags())
}
/*
NewClient creates a new HN client using the provided API options.
*/
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
cfg := firebase.Config{
DatabaseURL: HackerNewsAPI,
}
app, err := firebase.NewApp(ctx, &cfg, opts...)
if err != nil {
return nil, err
}
client, err := app.Database(ctx)
return &Client{
db: client,
lists: map[string]IDList{},
}, err
}
/*
Item retrieves the news item with the provided id from the HN API.
*/
func (c Client) Item(ctx context.Context, id int) (Item, error) {
item := Item{
remote: remote{
path: fmt.Sprintf(ItemPath, id),
},
}
_, err := c.Update(ctx, &item)
return item, err
}
/*
MaxItem returns the id of the last item created. The client never
caches the maximum item id, nor does it use ETags to avoid retrieving
the value from the server.
*/
func (c Client) MaxItem(ctx context.Context) (int, error) {
ref := c.db.NewRef(MaxItemPath)
max := 0
return max, ref.Get(ctx, &max)
}
/*
NewStories returns a list of item ids for the 500 newest stories.
*/
func (c Client) NewStories(ctx context.Context) (IDList, error) {
return c.list(ctx, NewStoriesPath)
}
/*
Update retrieves a new version of an HN Item, User or list (passed into
the method as rem) if there is one available. If ETags are disabled in
the client, the remote object is retrieved from the server again and the
boolean returned will alwasy indicated that the item was changed. The
default operation of the client is to check the ETags. In this case,
the returned boolean will indicate whether a new version of the object
has been retrieved.
*/
func (c Client) Update(ctx context.Context, rem Remote) (bool, error) {
ref := c.db.NewRef(rem.Path())
if c.disableEtags {
return true, ref.Get(ctx, rem)
}
chngd, etag, err := ref.GetIfChanged(ctx, rem.ETag(), rem)
if err == nil && chngd {
rem.SetETag(etag)
}
return chngd, err
}
/*
User retrieves the news item with the provided id from the HN API.
*/
func (c Client) User(ctx context.Context, id string) (User, error) {
// ref := c.db.NewRef(fmt.Sprintf(UserPath, id))
user := User{
remote: remote{
path: fmt.Sprintf(UserPath, id),
},
}
_, err := c.Update(ctx, &user)
return user, err
}
func (c Client) list(ctx context.Context, path string) (IDList, error) {
idList, ok := c.lists[path]
if !ok {
idList = IDList{
remote: remote{
path: path,
},
IDs: []ID{},
}
}
chngd, err := c.Update(ctx, &idList)
if chngd {
c.lists[path] = idList
}
return idList, err
}