author | Mikael Berthe <mikael@lilotux.net> |
Thu, 06 Sep 2018 11:28:42 +0200 | |
changeset 239 | ca5639b4768e |
parent 231 | 741291bb4772 |
child 240 | 80c81e9b77b4 |
permissions | -rw-r--r-- |
130 | 1 |
/* |
207 | 2 |
Copyright 2017-2018 Mikael Berthe |
130 | 3 |
Copyright 2017 Ollivier Robert |
4 |
||
5 |
Licensed under the MIT license. Please see the LICENSE file is this directory. |
|
6 |
*/ |
|
7 |
||
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
8 |
package madon |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
9 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
10 |
import ( |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
11 |
"bytes" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
12 |
"encoding/json" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
13 |
"fmt" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
14 |
"net/http" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
15 |
"net/url" |
155 | 16 |
"regexp" |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
17 |
"strconv" |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
"strings" |
239
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
19 |
"time" |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
21 |
"github.com/pkg/errors" |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
"github.com/sendgrid/rest" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
24 |
|
155 | 25 |
type apiLinks struct { |
26 |
next, prev *LimitParams |
|
27 |
} |
|
28 |
||
29 |
func parseLink(links []string) (*apiLinks, error) { |
|
30 |
if len(links) == 0 { |
|
31 |
return nil, nil |
|
32 |
} |
|
33 |
||
34 |
al := new(apiLinks) |
|
35 |
linkRegex := regexp.MustCompile(`<([^>]+)>; rel="([^"]+)`) |
|
36 |
for _, l := range links { |
|
37 |
m := linkRegex.FindAllStringSubmatch(l, -1) |
|
38 |
for _, submatch := range m { |
|
39 |
if len(submatch) != 3 { |
|
40 |
continue |
|
41 |
} |
|
42 |
// Parse URL |
|
43 |
u, err := url.Parse(submatch[1]) |
|
44 |
if err != nil { |
|
45 |
return al, err |
|
46 |
} |
|
47 |
var lp *LimitParams |
|
48 |
since := u.Query().Get("since_id") |
|
49 |
max := u.Query().Get("max_id") |
|
50 |
lim := u.Query().Get("limit") |
|
51 |
if since == "" && max == "" { |
|
52 |
continue |
|
53 |
} |
|
54 |
lp = new(LimitParams) |
|
55 |
if since != "" { |
|
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
56 |
lp.SinceID, err = strconv.ParseInt(since, 10, 64) |
155 | 57 |
if err != nil { |
58 |
return al, err |
|
59 |
} |
|
60 |
} |
|
61 |
if max != "" { |
|
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
62 |
lp.MaxID, err = strconv.ParseInt(max, 10, 64) |
155 | 63 |
if err != nil { |
64 |
return al, err |
|
65 |
} |
|
66 |
} |
|
67 |
if lim != "" { |
|
68 |
lp.Limit, err = strconv.Atoi(lim) |
|
69 |
if err != nil { |
|
70 |
return al, err |
|
71 |
} |
|
72 |
} |
|
73 |
switch submatch[2] { |
|
74 |
case "prev": |
|
75 |
al.prev = lp |
|
76 |
case "next": |
|
77 |
al.next = lp |
|
78 |
} |
|
79 |
} |
|
80 |
} |
|
81 |
return al, nil |
|
82 |
} |
|
83 |
||
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
84 |
// restAPI actually does the HTTP query |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
85 |
// It is a copy of rest.API with better handling of parameters with multiple values |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
86 |
func restAPI(request rest.Request) (*rest.Response, error) { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
87 |
c := &rest.Client{HTTPClient: http.DefaultClient} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
88 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
89 |
// Build the HTTP request object. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
if len(request.QueryParams) != 0 { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
91 |
// Add parameters to the URL |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
92 |
request.BaseURL += "?" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
93 |
urlp := url.Values{} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
94 |
for key, value := range request.QueryParams { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
// It seems Mastodon doesn't like parameters with index |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
// numbers, but it needs the brackets. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
// Let's check if the key matches '^.+\[.*\]$' |
231
741291bb4772
Fix (hopefully) handling of arrays in API calls
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
98 |
// Do not proceed if there's another bracket pair. |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
klen := len(key) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
if klen == 0 { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
continue |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
102 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
i := strings.Index(key, "[") |
231
741291bb4772
Fix (hopefully) handling of arrays in API calls
Mikael Berthe <mikael@lilotux.net>
parents:
207
diff
changeset
|
104 |
if i > 0 && key[klen-1] == ']' && strings.Index(key[i+1:], "[") < 0 { |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
// This is an array, let's remove the index number |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
106 |
key = key[:i] + "[]" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
107 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
108 |
urlp.Add(key, value) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
109 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
110 |
urlpstr := urlp.Encode() |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
111 |
request.BaseURL += urlpstr |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
112 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
113 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
114 |
req, err := http.NewRequest(string(request.Method), request.BaseURL, bytes.NewBuffer(request.Body)) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
115 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
116 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
117 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
118 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
119 |
for key, value := range request.Headers { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
120 |
req.Header.Set(key, value) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
121 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
122 |
_, exists := req.Header["Content-Type"] |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
123 |
if len(request.Body) > 0 && !exists { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
124 |
req.Header.Set("Content-Type", "application/json") |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
125 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
127 |
// Build the HTTP client and make the request. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
128 |
res, err := c.MakeRequest(req) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
129 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
130 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
} |
169
d84b2b83813d
Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
132 |
if res.StatusCode < 200 || res.StatusCode >= 300 { |
d84b2b83813d
Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
133 |
return nil, errors.Errorf("bad server status code (%d): %s", |
d84b2b83813d
Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
134 |
res.StatusCode, http.StatusText(res.StatusCode)) |
d84b2b83813d
Better handling of server error pages
Mikael Berthe <mikael@lilotux.net>
parents:
162
diff
changeset
|
135 |
} |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
136 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
137 |
// Build Response object. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 |
response, err := rest.BuildResponse(res) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
141 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
142 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
return response, nil |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
144 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
145 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
146 |
// prepareRequest inserts all pre-defined stuff |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
147 |
func (mc *Client) prepareRequest(target string, method rest.Method, params apiCallParams) (rest.Request, error) { |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
148 |
var req rest.Request |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
149 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
150 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
151 |
return req, ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
152 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
153 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
154 |
endPoint := mc.APIBase + "/" + target |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
155 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
156 |
// Request headers |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
157 |
hdrs := make(map[string]string) |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
158 |
hdrs["User-Agent"] = fmt.Sprintf("madon/%s", MadonVersion) |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
159 |
if mc.UserToken != nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
160 |
hdrs["Authorization"] = fmt.Sprintf("Bearer %s", mc.UserToken.AccessToken) |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
161 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
162 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
163 |
req = rest.Request{ |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
164 |
BaseURL: endPoint, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
165 |
Headers: hdrs, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
166 |
Method: method, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
167 |
QueryParams: params, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
168 |
} |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
169 |
return req, nil |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
170 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
171 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
172 |
// apiCall makes a call to the Mastodon API server |
155 | 173 |
// If links is not nil, the prev/next links from the API response headers |
174 |
// will be set (if they exist) in the structure. |
|
175 |
func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, links *apiLinks, data interface{}) error { |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
176 |
if mc == nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
177 |
return errors.New("use of uninitialized madon client") |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
178 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
179 |
|
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
180 |
if limitOptions != nil { |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
181 |
if params == nil { |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
182 |
params = make(apiCallParams) |
150
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
183 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
184 |
if limitOptions.Limit > 0 { |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
185 |
params["limit"] = strconv.Itoa(limitOptions.Limit) |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
186 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
187 |
if limitOptions.SinceID > 0 { |
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
188 |
params["since_id"] = strconv.FormatInt(limitOptions.SinceID, 10) |
150
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
189 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
190 |
if limitOptions.MaxID > 0 { |
159
408aa794d9bb
s/int/int64/ for IDs and time integers
Mikael Berthe <mikael@lilotux.net>
parents:
155
diff
changeset
|
191 |
params["max_id"] = strconv.FormatInt(limitOptions.MaxID, 10) |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
192 |
} |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
193 |
} |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
194 |
|
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
195 |
// Prepare query |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
196 |
req, err := mc.prepareRequest(endPoint, method, params) |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
197 |
if err != nil { |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
198 |
return err |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
199 |
} |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
200 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
201 |
// Make API call |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
202 |
r, err := restAPI(req) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
203 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
204 |
return errors.Wrapf(err, "API query (%s) failed", endPoint) |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
205 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
206 |
|
155 | 207 |
if links != nil { |
208 |
pLinks, err := parseLink(r.Headers["Link"]) |
|
209 |
if err != nil { |
|
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
210 |
return errors.Wrapf(err, "cannot decode header links (%s)", method) |
155 | 211 |
} |
212 |
if pLinks != nil { |
|
213 |
*links = *pLinks |
|
214 |
} |
|
215 |
} |
|
216 |
||
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
217 |
// Check for error reply |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
218 |
var errorResult Error |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
219 |
if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
220 |
// The empty object is not an error |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
221 |
if errorResult.Text != "" { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
222 |
return errors.New(errorResult.Text) |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
223 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
224 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
225 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
226 |
// Not an error reply; let's unmarshal the data |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
227 |
err = json.Unmarshal([]byte(r.Body), &data) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
228 |
if err != nil { |
162
68df3a01e1a7
Use github.com/pkg/errors
Mikael Berthe <mikael@lilotux.net>
parents:
159
diff
changeset
|
229 |
return errors.Wrapf(err, "cannot decode API response (%s)", method) |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
230 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
231 |
return nil |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
232 |
} |
239
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
233 |
|
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
234 |
/* Mastodon timestamp handling */ |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
235 |
|
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
236 |
// MastodonDate is a custom type for the timestamps returned by some API calls |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
237 |
// It is used, for example, by 'v1/instance/activity' and 'v2/search'. |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
238 |
// The date returned by those Mastodon API calls is a string containing a |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
239 |
// timestamp in seconds... |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
240 |
|
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
241 |
// UnmarshalJSON handles deserialization for custom MastodonDate type |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
242 |
func (act *MastodonDate) UnmarshalJSON(b []byte) error { |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
243 |
s, err := strconv.ParseInt(strings.Trim(string(b), "\""), 10, 64) |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
244 |
if err != nil { |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
245 |
return err |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
246 |
} |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
247 |
if s == 0 { |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
248 |
act.Time = time.Time{} |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
249 |
return nil |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
250 |
} |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
251 |
act.Time = time.Unix(s, 0) |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
252 |
return nil |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
253 |
} |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
254 |
|
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
255 |
// MarshalJSON handles serialization for custom MastodonDate type |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
256 |
func (act *MastodonDate) MarshalJSON() ([]byte, error) { |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
257 |
return []byte(fmt.Sprintf("\"%d\"", act.Unix())), nil |
ca5639b4768e
Introduce new type: MastodonDate
Mikael Berthe <mikael@lilotux.net>
parents:
231
diff
changeset
|
258 |
} |