-
Notifications
You must be signed in to change notification settings - Fork 31
/
data_api.go
81 lines (67 loc) · 2.75 KB
/
data_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
package mailjet
import "strings"
// ListData issues a GET to list the specified data resource
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
func (mj *Client) ListData(resource string, resp interface{}, options ...RequestOptions) (count, total int, err error) {
url := buildDataURL(mj.apiBase, &DataRequest{SourceType: resource})
req, err := createRequest("GET", url, nil, nil, options...)
if err != nil {
return count, total, err
}
return mj.httpClient.Send(req).Read(resp).Call()
}
// GetData issues a GET to view a resource specifying an id
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
// Without an specified SourceTypeID in MailjetDataRequest, it is the same as ListData.
func (mj *Client) GetData(mdr *DataRequest, res interface{}, options ...RequestOptions) (err error) {
url := buildDataURL(mj.apiBase, mdr)
req, err := createRequest("GET", url, nil, nil, options...)
if err != nil {
return err
}
_, _, err = mj.httpClient.Send(req).Read(res).Call()
return err
}
// PostData issues a POST to create a new data resource
// and stores the result in the value pointed to by res.
// Filters can be add via functional options.
func (mj *Client) PostData(fmdr *FullDataRequest, res interface{}, options ...RequestOptions) (err error) {
url := buildDataURL(mj.apiBase, fmdr.Info)
req, err := createRequest("POST", url, fmdr.Payload, nil, options...)
if err != nil {
return err
}
headers := map[string]string{"Content-Type": "application/json"}
if fmdr.Info.MimeType != "" {
contentType := strings.Replace(fmdr.Info.MimeType, ":", "/", 1)
headers = map[string]string{"Content-Type": contentType}
}
_, _, err = mj.httpClient.Send(req).With(headers).Read(res).Call()
return err
}
// PutData is used to update a data resource.
// Fields to be updated must be specified by the string array onlyFields.
// If onlyFields is nil, all fields except these with the tag read_only, are updated.
// Filters can be add via functional options.
func (mj *Client) PutData(fmr *FullDataRequest, onlyFields []string, options ...RequestOptions) (err error) {
url := buildDataURL(mj.apiBase, fmr.Info)
req, err := createRequest("PUT", url, fmr.Payload, onlyFields, options...)
if err != nil {
return err
}
headers := map[string]string{"Content-Type": "application/json"}
_, _, err = mj.httpClient.Send(req).With(headers).Call()
return err
}
// DeleteData is used to delete a data resource.
func (mj *Client) DeleteData(mdr *DataRequest, options ...RequestOptions) (err error) {
url := buildDataURL(mj.apiBase, mdr)
req, err := createRequest("DELETE", url, nil, nil, options...)
if err != nil {
return err
}
_, _, err = mj.httpClient.Send(req).Call()
return err
}