forked from peterhellberg/hn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
users.go
64 lines (52 loc) · 1.32 KB
/
users.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
package hn
import (
"context"
"fmt"
"time"
)
var errMissingID = fmt.Errorf("missing id")
// UsersService communicates with the news
// related endpoints in the Hacker News API
type UsersService interface {
Get(ctx context.Context, id string) (*User, error)
}
// usersService implements LiveService.
type usersService struct {
client *Client
}
// User represents a Hacker News user
type User struct {
About string `json:"about"`
Created int `json:"created"`
Delay int `json:"delay"`
ID string `json:"id"`
Karma int `json:"karma"`
Submitted []int `json:"submitted"`
}
// CreatedTime return the time of the created
func (u *User) CreatedTime() time.Time {
return time.Unix(int64(u.Created), 0)
}
// User is a convenience method proxying Users.Get
func (c *Client) User(ctx context.Context, id string) (*User, error) {
return c.Users.Get(ctx, id)
}
// Get retrieves a user with the given id
func (s *usersService) Get(ctx context.Context, id string) (*User, error) {
if id == "" {
return nil, errMissingID
}
req, err := s.client.NewRequest(ctx, s.getPath(id))
if err != nil {
return nil, err
}
var user User
_, err = s.client.Do(req, &user)
if err != nil {
return nil, err
}
return &user, nil
}
func (s *usersService) getPath(id string) string {
return fmt.Sprintf("user/%v.json", id)
}