-
Notifications
You must be signed in to change notification settings - Fork 5
/
user.go
43 lines (38 loc) · 923 Bytes
/
user.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
package user
import (
"context"
"errors"
"net/url"
"github.com/NateScarlet/pixiv/pkg/client"
"github.com/NateScarlet/pixiv/pkg/image"
)
// User data.
type User struct {
ID string
Name string
Avatar image.URLs
}
// Fetch additional data from pixiv single user api,
func (i *User) Fetch(ctx context.Context) (err error) {
if i.ID == "" {
return errors.New("pixiv: user: no id specified")
}
var c = client.For(ctx)
resp, err := c.GetWithContext(ctx, c.EndpointURL("/ajax/user/"+i.ID, nil).String())
if err != nil {
return
}
defer resp.Body.Close()
body, err := client.ParseAPIResult(resp.Body)
if err != nil {
return
}
i.Name = body.Get("name").String()
i.Avatar.Mini = body.Get("image").String()
i.Avatar.Thumb = body.Get("imageBig").String()
return
}
// URL to view web page.
func (i User) URL(ctx context.Context) *url.URL {
return client.For(ctx).EndpointURL("/users/"+i.ID, nil)
}