|
1 | 1 | package microcms
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bytes" |
4 | 5 | "encoding/json"
|
5 | 6 | "fmt"
|
| 7 | + "io/ioutil" |
6 | 8 | "net/http"
|
7 | 9 | "net/url"
|
8 | 10 | "strings"
|
9 | 11 | )
|
10 | 12 |
|
| 13 | +type httpClient interface { |
| 14 | + Do(req *http.Request) (*http.Response, error) |
| 15 | +} |
| 16 | + |
11 | 17 | type Client struct {
|
12 | 18 | serviceDomain string
|
13 | 19 | apiKey string
|
| 20 | + httpClient httpClient |
14 | 21 | }
|
15 | 22 |
|
16 |
| -type Params struct { |
17 |
| - contentID string |
18 |
| - draftKey string |
19 |
| - limit int |
20 |
| - offset int |
21 |
| - orders []string |
22 |
| - q string |
23 |
| - fields []string |
24 |
| - ids []string |
25 |
| - filters string |
26 |
| - depth int |
27 |
| -} |
28 |
| - |
29 |
| -type RequestParams func(*Params) |
30 |
| - |
31 |
| -func CreateClient(serviceDomain, apiKey string) *Client { |
| 23 | +func New(serviceDomain, apiKey string) *Client { |
32 | 24 | c := &Client{
|
33 | 25 | serviceDomain: serviceDomain,
|
34 | 26 | apiKey: apiKey,
|
| 27 | + httpClient: http.DefaultClient, |
35 | 28 | }
|
36 | 29 | return c
|
37 | 30 | }
|
38 | 31 |
|
39 |
| -func (c *Client) makeRequest(method, endpoint string, p *Params) (*http.Request, error) { |
40 |
| - url := createURL(c.serviceDomain, endpoint, p) |
41 |
| - |
42 |
| - req, err := http.NewRequest(http.MethodGet, url, nil) |
43 |
| - if err != nil { |
44 |
| - return nil, err |
45 |
| - } |
46 |
| - |
47 |
| - req.Header.Set("X-MICROCMS-API-KEY", c.apiKey) |
48 |
| - |
49 |
| - return req, nil |
| 32 | +func (c *Client) SetHTTPClient(client *http.Client) { |
| 33 | + c.httpClient = client |
50 | 34 | }
|
51 | 35 |
|
52 |
| -func (c *Client) Get(endpoint string, data interface{}, params ...RequestParams) error { |
53 |
| - p := &Params{} |
54 |
| - for _, params := range params { |
55 |
| - params(p) |
| 36 | +func makeRequest(c *Client, method, endpoint string, query url.Values, data interface{}) (*http.Request, error) { |
| 37 | + url := fmt.Sprintf("https://%s.%s/api/%s/%s", c.serviceDomain, BaseDomain, APIVersion, endpoint) |
| 38 | + if len(query) > 0 { |
| 39 | + url = fmt.Sprintf("%s?%s", url, query.Encode()) |
56 | 40 | }
|
57 | 41 |
|
58 |
| - req, err := c.makeRequest(http.MethodGet, endpoint, p) |
59 |
| - if err != nil { |
60 |
| - return err |
| 42 | + buf := new(bytes.Buffer) |
| 43 | + if data != nil { |
| 44 | + if err := json.NewEncoder(buf).Encode(data); err != nil { |
| 45 | + return nil, err |
| 46 | + } |
61 | 47 | }
|
62 | 48 |
|
63 |
| - res, err := http.DefaultClient.Do(req) |
| 49 | + req, err := http.NewRequest(method, url, buf) |
64 | 50 | if err != nil {
|
65 |
| - return err |
66 |
| - } |
67 |
| - defer res.Body.Close() |
68 |
| - |
69 |
| - if err := json.NewDecoder(res.Body).Decode(&data); err != nil { |
70 |
| - return err |
71 |
| - } |
72 |
| - |
73 |
| - return err |
74 |
| -} |
75 |
| - |
76 |
| -func createURL(serviceDomain, endpoint string, p *Params) string { |
77 |
| - base := fmt.Sprintf("https://%s.%s/api/%s/%s", serviceDomain, BaseDomain, APIVersion, endpoint) |
78 |
| - |
79 |
| - if p.contentID != "" { |
80 |
| - base = fmt.Sprintf("%s/%s", base, p.contentID) |
81 |
| - } |
82 |
| - |
83 |
| - urlValues := url.Values{} |
84 |
| - if len(p.draftKey) > 0 { |
85 |
| - urlValues.Set("draftKey", p.draftKey) |
86 |
| - } |
87 |
| - if p.limit != 0 { |
88 |
| - urlValues.Set("limit", fmt.Sprint(p.limit)) |
89 |
| - } |
90 |
| - if p.offset != 0 { |
91 |
| - urlValues.Set("offset", fmt.Sprint(p.offset)) |
92 |
| - } |
93 |
| - if len(p.orders) > 0 { |
94 |
| - urlValues.Set("orders", strings.Join(p.orders, ",")) |
95 |
| - } |
96 |
| - if len(p.q) > 0 { |
97 |
| - urlValues.Set("q", p.q) |
98 |
| - } |
99 |
| - if len(p.fields) > 0 { |
100 |
| - urlValues.Set("fields", strings.Join(p.fields, ",")) |
101 |
| - } |
102 |
| - if len(p.ids) > 0 { |
103 |
| - urlValues.Set("ids", strings.Join(p.ids, ",")) |
104 |
| - } |
105 |
| - if len(p.filters) > 0 { |
106 |
| - urlValues.Set("filters", p.filters) |
107 |
| - } |
108 |
| - if p.depth != 0 { |
109 |
| - urlValues.Set("depth", fmt.Sprint(p.depth)) |
110 |
| - } |
111 |
| - |
112 |
| - if len(urlValues) > 0 { |
113 |
| - base = fmt.Sprintf("%s?%s", base, urlValues.Encode()) |
114 |
| - } |
115 |
| - |
116 |
| - return base |
117 |
| -} |
118 |
| - |
119 |
| -func ContentID(v string) RequestParams { |
120 |
| - return func(p *Params) { |
121 |
| - p.contentID = v |
122 |
| - } |
123 |
| -} |
124 |
| - |
125 |
| -func DraftKey(v string) RequestParams { |
126 |
| - return func(p *Params) { |
127 |
| - p.draftKey = v |
128 |
| - } |
129 |
| -} |
130 |
| - |
131 |
| -func Limit(v int) RequestParams { |
132 |
| - return func(p *Params) { |
133 |
| - p.limit = v |
| 51 | + return nil, err |
134 | 52 | }
|
135 |
| -} |
136 | 53 |
|
137 |
| -func Offset(v int) RequestParams { |
138 |
| - return func(p *Params) { |
139 |
| - p.offset = v |
140 |
| - } |
141 |
| -} |
| 54 | + req.Header.Set("X-MICROCMS-API-KEY", c.apiKey) |
142 | 55 |
|
143 |
| -func Orders(v []string) RequestParams { |
144 |
| - return func(p *Params) { |
145 |
| - p.orders = v |
| 56 | + if data != nil { |
| 57 | + req.Header.Set("Content-Type", "application/json; charset=utf-8") |
146 | 58 | }
|
147 |
| -} |
148 | 59 |
|
149 |
| -func Q(v string) RequestParams { |
150 |
| - return func(p *Params) { |
151 |
| - p.q = v |
152 |
| - } |
| 60 | + return req, nil |
153 | 61 | }
|
154 | 62 |
|
155 |
| -func Fields(v []string) RequestParams { |
156 |
| - return func(p *Params) { |
157 |
| - p.fields = v |
| 63 | +func sendRequest(c *Client, req *http.Request, data interface{}) error { |
| 64 | + res, err := c.httpClient.Do(req) |
| 65 | + if err != nil { |
| 66 | + return err |
158 | 67 | }
|
159 |
| -} |
| 68 | + defer res.Body.Close() |
160 | 69 |
|
161 |
| -func IDs(ids []string) RequestParams { |
162 |
| - return func(p *Params) { |
163 |
| - p.ids = ids |
| 70 | + if res.StatusCode >= 400 { |
| 71 | + errorMessage, err := ioutil.ReadAll(res.Body) |
| 72 | + if err != nil { |
| 73 | + return fmt.Errorf("microCMS connection error: %w", err) |
| 74 | + } |
| 75 | + return fmt.Errorf("microCMS response error: %s", errorMessage) |
164 | 76 | }
|
165 |
| -} |
166 | 77 |
|
167 |
| -func Filters(v string) RequestParams { |
168 |
| - return func(p *Params) { |
169 |
| - p.filters = v |
| 78 | + if strings.Contains(res.Header.Get("Content-Type"), "application/json") { |
| 79 | + if err := json.NewDecoder(res.Body).Decode(data); err != nil { |
| 80 | + return err |
| 81 | + } |
170 | 82 | }
|
171 |
| -} |
172 | 83 |
|
173 |
| -func Depth(v int) RequestParams { |
174 |
| - return func(p *Params) { |
175 |
| - p.depth = v |
176 |
| - } |
| 84 | + return nil |
177 | 85 | }
|
0 commit comments