-
Notifications
You must be signed in to change notification settings - Fork 8
/
api.go
107 lines (90 loc) · 2.46 KB
/
api.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
package mangodex
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
const (
BaseAPI = "https://api.mangadex.org"
)
// DexClient : The MangaDex client.
type DexClient struct {
client *http.Client
header http.Header
common service
refreshToken string
// Services for MangaDex API
Auth *AuthService
Manga *MangaService
Chapter *ChapterService
User *UserService
AtHome *AtHomeService
}
// service : Wrapper for DexClient.
type service struct {
client *DexClient
}
// NewDexClient : New anonymous client. To login as an authenticated user, use DexClient.Login.
func NewDexClient() *DexClient {
// Create client
client := http.Client{}
// Create header
header := http.Header{}
header.Set("Content-Type", "application/json") // Set default content type.
// Create the new client
dex := &DexClient{
client: &client,
header: header,
}
// Set the common client
dex.common.client = dex
// Reuse the common client for the other services
dex.Auth = (*AuthService)(&dex.common)
dex.Manga = (*MangaService)(&dex.common)
dex.Chapter = (*ChapterService)(&dex.common)
dex.User = (*UserService)(&dex.common)
dex.AtHome = (*AtHomeService)(&dex.common)
return dex
}
// Request : Sends a request to the MangaDex API.
func (c *DexClient) Request(ctx context.Context, method, url string, body io.Reader) (*http.Response, error) {
// Create the request
req, err := http.NewRequestWithContext(ctx, method, url, body)
if err != nil {
return nil, err
}
// Set header for request.
req.Header = c.header
// Send request.
resp, err := c.client.Do(req)
if err != nil {
return nil, err
} else if resp.StatusCode != 200 {
// Decode to an ErrorResponse struct.
var er ErrorResponse
if err = json.NewDecoder(resp.Body).Decode(&er); err != nil {
return nil, err
}
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
return nil, fmt.Errorf("non-200 status code -> (%d) %s", resp.StatusCode, er.GetErrors())
}
return resp, nil
}
// RequestAndDecode : Convenience wrapper to also decode response to required data type
func (c *DexClient) RequestAndDecode(ctx context.Context, method, url string, body io.Reader, rt ResponseType) error {
// Get the response of the request.
resp, err := c.Request(ctx, method, url, body)
if err != nil {
return err
}
// Decode the request into the specified ResponseType.
err = json.NewDecoder(resp.Body).Decode(rt)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(resp.Body)
return err
}