-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathuser.go
83 lines (69 loc) · 2.38 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package mangodex
import (
"context"
"net/http"
"net/url"
"strconv"
)
const (
GetUserFollowedMangaListPath = "user/follows/manga"
GetLoggedUserPath = "user/me"
)
// UserService : Provides User services provided by the API.
type UserService service
// GetUserFollowedMangaList : Return list of followed Manga.
// https://api.mangadex.org/docs.html#operation/get-user-follows-manga
func (s *UserService) GetUserFollowedMangaList(limit, offset int, includes []string) (*MangaList, error) {
return s.GetUserFollowedMangaListContext(context.Background(), limit, offset, includes)
}
// GetUserFollowedMangaListContext : GetUserFollowedMangaListPath with custom context.
func (s *UserService) GetUserFollowedMangaListContext(ctx context.Context, limit, offset int, includes []string) (*MangaList, error) {
u, _ := url.Parse(BaseAPI)
u.Path = GetUserFollowedMangaListPath
// Set required query parameters
q := u.Query()
q.Add("limit", strconv.Itoa(limit))
q.Add("offset", strconv.Itoa(offset))
for _, i := range includes {
q.Add("includes[]", i)
}
u.RawQuery = q.Encode()
var l MangaList
err := s.client.RequestAndDecode(ctx, http.MethodGet, u.String(), nil, &l)
return &l, err
}
// UserResponse : Typical User response.
type UserResponse struct {
Result string `json:"result"`
Response string `json:"response"`
Data User `json:"data"`
}
func (ur *UserResponse) GetResult() string {
return ur.Result
}
// User : Info on a MangaDex user.
type User struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes UserAttributes `json:"attributes"`
Relationships []Relationship `json:"relationships"`
}
// UserAttributes : Attributes of a User.
type UserAttributes struct {
Username string `json:"username"`
Roles []string `json:"roles"`
Version int `json:"version"`
}
// GetLoggedUser : Return logged UserResponse.
// https://api.mangadex.org/docs.html#operation/get-user-follows-group
func (s *UserService) GetLoggedUser() (*UserResponse, error) {
return s.GetLoggedUserContext(context.Background())
}
// GetLoggedUserContext : GetLoggedUser with custom context.
func (s *UserService) GetLoggedUserContext(ctx context.Context) (*UserResponse, error) {
u, _ := url.Parse(BaseAPI)
u.Path = GetLoggedUserPath
var r UserResponse
err := s.client.RequestAndDecode(ctx, http.MethodGet, u.String(), nil, &r)
return &r, err
}