-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathread.go
106 lines (91 loc) · 1.92 KB
/
read.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
package microcms
import (
"fmt"
"net/http"
"net/url"
"path"
"strings"
)
type ListParams struct {
Endpoint string
DraftKey string
Limit int
Offset int
Orders []string
Q string
Fields []string
IDs []string
Filters string
Depth int
}
type GetParams struct {
Endpoint string
ContentID string
DraftKey string
Fields []string
Depth int
}
func (c *Client) List(p ListParams, data interface{}) error {
req, err := makeRequest(c, http.MethodGet, p.Endpoint, makeListQuery(p), nil)
if err != nil {
return err
}
if err := sendRequest(c, req, data); err != nil {
return err
}
return err
}
func (c *Client) Get(p GetParams, data interface{}) error {
req, err := makeRequest(c, http.MethodGet, path.Join(p.Endpoint, p.ContentID), makeGetQuery(p), nil)
if err != nil {
return err
}
if err := sendRequest(c, req, data); err != nil {
return err
}
return err
}
func makeListQuery(p ListParams) url.Values {
urlValues := url.Values{}
if len(p.DraftKey) > 0 {
urlValues.Set("draftKey", p.DraftKey)
}
if p.Limit != 0 {
urlValues.Set("limit", fmt.Sprint(p.Limit))
}
if p.Offset != 0 {
urlValues.Set("offset", fmt.Sprint(p.Offset))
}
if len(p.Orders) > 0 {
urlValues.Set("orders", strings.Join(p.Orders, ","))
}
if len(p.Q) > 0 {
urlValues.Set("q", p.Q)
}
if len(p.Fields) > 0 {
urlValues.Set("fields", strings.Join(p.Fields, ","))
}
if len(p.IDs) > 0 {
urlValues.Set("ids", strings.Join(p.IDs, ","))
}
if len(p.Filters) > 0 {
urlValues.Set("filters", p.Filters)
}
if p.Depth != 0 {
urlValues.Set("depth", fmt.Sprint(p.Depth))
}
return urlValues
}
func makeGetQuery(p GetParams) url.Values {
urlValues := url.Values{}
if len(p.DraftKey) > 0 {
urlValues.Set("draftKey", p.DraftKey)
}
if len(p.Fields) > 0 {
urlValues.Set("fields", strings.Join(p.Fields, ","))
}
if p.Depth != 0 {
urlValues.Set("depth", fmt.Sprint(p.Depth))
}
return urlValues
}