-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
61 lines (56 loc) · 1.86 KB
/
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package wallhaven
import (
"fmt"
"net/http"
"net/url"
)
// GetUserSettings returns user preferences for the current logged in user. This method requires an API key
func GetUserSettings(apiKey string) (*User, error) {
u, _ := url.Parse(getWithBase("/settings"))
q := u.Query()
q.Add("apiKey", apiKey)
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
info := &UserResult{}
processResponse(resp, info)
return &info.Data, nil
}
// GetUserCollections returns the user's collections. This method requires an API key
// If NO username is provided (i.e: username == "") then this method will returns the authenticated user collections (public and private).
// If one username is provided (i.e: username != "") then this method will only returns user PUBLIC collections
func GetUserCollections(apiKey string, username string) (*[]Collection, error) {
var u *url.URL
if username == "" {
u, _ = url.Parse(getWithBase("/collections"))
} else {
u, _ = url.Parse(getWithBase("/collections/" + username))
}
q := u.Query()
q.Add("apiKey", apiKey)
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
collections := &CollectionsResult{}
processResponse(resp, collections)
return &collections.Data, nil
}
// GetUserCollectionWallpapers returns the Wallpapers as array from the user's collections. This method requires an API key.
// Username is required.
func GetUserCollectionWallpapers(apiKey string, username string, id CollectionID) (*[]Wallpaper, error) {
u, _ := url.Parse(getWithBase(fmt.Sprintf("%s/%s/%s", "/collections", username, id)))
q := u.Query()
q.Add("apiKey", apiKey)
u.RawQuery = q.Encode()
resp, err := http.Get(u.String())
if err != nil {
return nil, err
}
wallpapers := &CollectionWallpapersResult{}
processResponse(resp, wallpapers)
return &wallpapers.Data, nil
}