This repository has been archived by the owner on Jan 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmangarock.go
116 lines (93 loc) · 2.82 KB
/
mangarock.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
package mangarock
import (
"encoding/json"
"fmt"
"net/http"
)
// APIEndpoint is the mangarock api endpoint :)
const APIEndpoint = "https://api.mangarockhd.com/query/web401/"
// MangaRockInfo is used to parse a response from the `info` endpoint
// Contains the information related to a Manga
type MangaRockInfo struct {
Code int `json:"code"`
Data Manga `json:"data"`
}
// MangaRockPages is used to parse a response from `pages` endpoint
// Contains the link to the images related to a manga
type MangaRockPages struct {
Code int `json:"code"`
Data []string `json:"data"`
}
// Client contains only the `client` by now
// Maybe in future it can contains an ApiKey
type Client struct {
Client *http.Client
Options map[string]string
}
// NewClient returns a Client instance
func NewClient() *Client {
return &Client{
Client: &http.Client{},
Options: make(map[string]string),
}
}
// NewClientWithOptions returns a Client instance with the given options
func NewClientWithOptions(options map[string]string) *Client {
return &Client{
Client: &http.Client{},
Options: options,
}
}
// getJson decode the response body to given struct
func getJSON(response *http.Response, target interface{}) error {
return json.NewDecoder(response.Body).Decode(target)
}
// Set Additional options to the client instance
func (c *Client) SetOptions(options map[string]string) {
c.Options = options
}
// prepareRequest will setup a request by using method and endpoint
// it can be used to set a future API key
func (c *Client) prepareRequest(method, endpoint string) (*http.Request, error) {
req, err := http.NewRequest(method, fmt.Sprintf("%s%s", APIEndpoint, endpoint), nil)
// add country if in options
if country, ok := c.Options["country"]; ok {
q := req.URL.Query()
q.Add("country", country)
req.URL.RawQuery = q.Encode()
}
return req, err
}
// Get is Client Get method
func (c *Client) Get(endpoint string) (*http.Response, error) {
request, reqErr := c.prepareRequest("GET", endpoint)
if reqErr != nil {
return nil, reqErr
}
response, err := c.Client.Do(request)
return response, err
}
// Info returns the info related to a manga
func (c *Client) Info(comicName string) (*MangaRockInfo, error) {
endpoint := fmt.Sprintf("info?oid=%s", comicName)
mangaInfo := new(MangaRockInfo)
response, err := c.Get(endpoint)
if err != nil {
return nil, err
}
defer response.Body.Close()
jsonErr := getJSON(response, &mangaInfo)
return mangaInfo, jsonErr
}
// Pages returns the pages related to a manga
func (c *Client) Pages(comicName string) (*MangaRockPages, error) {
endpoint := fmt.Sprintf("pages?oid=%s", comicName)
mangaPages := new(MangaRockPages)
response, err := c.Get(endpoint)
if err != nil {
return mangaPages, err
}
defer response.Body.Close()
jsonErr := getJSON(response, &mangaPages)
return mangaPages, jsonErr
}