-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmanga.go
134 lines (114 loc) · 4.36 KB
/
manga.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
package mangodex
import (
"context"
"fmt"
"net/http"
"net/url"
"strings"
)
const (
MangaListPath = "manga"
CheckIfMangaFollowedPath = "user/follows/manga/%s"
ToggleMangaFollowPath = "manga/%s/follow"
)
// MangaService : Provides Manga services provided by the API.
type MangaService service
// MangaList : A response for getting a list of manga.
type MangaList struct {
Result string `json:"result"`
Response string `json:"response"`
Data []Manga `json:"data"`
Limit int `json:"limit"`
Offset int `json:"offset"`
Total int `json:"total"`
}
func (ml *MangaList) GetResult() string {
return ml.Result
}
// Manga : Struct containing information on a Manga.
type Manga struct {
ID string `json:"id"`
Type string `json:"type"`
Attributes MangaAttributes `json:"attributes"`
Relationships []Relationship `json:"relationships"`
}
// GetTitle : Get title of the Manga.
func (m *Manga) GetTitle(langCode string) string {
if title := m.Attributes.Title.GetLocalString(langCode); title != "" {
return title
}
return m.Attributes.AltTitles.GetLocalString(langCode)
}
// GetDescription : Get description of the Manga.
func (m *Manga) GetDescription(langCode string) string {
return m.Attributes.Description.GetLocalString(langCode)
}
// MangaAttributes : Attributes for a Manga.
type MangaAttributes struct {
Title LocalisedStrings `json:"title"`
AltTitles LocalisedStrings `json:"altTitles"`
Description LocalisedStrings `json:"description"`
IsLocked bool `json:"isLocked"`
Links LocalisedStrings `json:"links"`
OriginalLanguage string `json:"originalLanguage"`
LastVolume *string `json:"lastVolume"`
LastChapter *string `json:"lastChapter"`
PublicationDemographic *string `json:"publicationDemographic"`
Status *string `json:"status"`
Year *int `json:"year"`
ContentRating *string `json:"contentRating"`
Tags []Tag `json:"tags"`
State string `json:"state"`
Version int `json:"version"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
}
// GetMangaList : Get a list of Manga.
// https://api.mangadex.org/docs.html#operation/get-search-manga
func (s *MangaService) GetMangaList(params url.Values) (*MangaList, error) {
return s.GetMangaListContext(context.Background(), params)
}
// GetMangaListContext : GetMangaList with custom context.
func (s *MangaService) GetMangaListContext(ctx context.Context, params url.Values) (*MangaList, error) {
u, _ := url.Parse(BaseAPI)
u.Path = MangaListPath
// Set query parameters
u.RawQuery = params.Encode()
var l MangaList
err := s.client.RequestAndDecode(ctx, http.MethodGet, u.String(), nil, &l)
return &l, err
}
// CheckIfMangaFollowed : Check if a user follows a manga.
func (s *MangaService) CheckIfMangaFollowed(id string) (bool, error) {
return s.CheckIfMangaFollowedContext(context.Background(), id)
}
// CheckIfMangaFollowedContext : CheckIfMangaFollowed with custom context.
func (s *MangaService) CheckIfMangaFollowedContext(ctx context.Context, id string) (bool, error) {
u, _ := url.Parse(BaseAPI)
u.Path = fmt.Sprintf(CheckIfMangaFollowedPath, id)
var r Response
err := s.client.RequestAndDecode(ctx, http.MethodGet, u.String(), nil, &r)
if err != nil {
if strings.Contains(err.Error(), "404") {
return false, nil
}
return false, err
}
return true, nil
}
// ToggleMangaFollowStatus :Toggle follow status for a manga.
func (s *MangaService) ToggleMangaFollowStatus(id string, toFollow bool) (*Response, error) {
return s.ToggleMangaFollowStatusContext(context.Background(), id, toFollow)
}
// ToggleMangaFollowStatusContext ToggleMangaFollowStatus with custom context.
func (s *MangaService) ToggleMangaFollowStatusContext(ctx context.Context, id string, toFollow bool) (*Response, error) {
u, _ := url.Parse(BaseAPI)
u.Path = fmt.Sprintf(ToggleMangaFollowPath, id)
method := http.MethodPost // To follow
if !toFollow {
method = http.MethodDelete // To unfollow
}
var r Response
err := s.client.RequestAndDecode(ctx, method, u.String(), nil, &r)
return &r, err
}