equal
deleted
inserted
replaced
15 "net/url" |
15 "net/url" |
16 "regexp" |
16 "regexp" |
17 "strconv" |
17 "strconv" |
18 "strings" |
18 "strings" |
19 |
19 |
|
20 "github.com/pkg/errors" |
20 "github.com/sendgrid/rest" |
21 "github.com/sendgrid/rest" |
21 ) |
22 ) |
22 |
23 |
23 type apiLinks struct { |
24 type apiLinks struct { |
24 next, prev *LimitParams |
25 next, prev *LimitParams |
165 // apiCall makes a call to the Mastodon API server |
166 // apiCall makes a call to the Mastodon API server |
166 // If links is not nil, the prev/next links from the API response headers |
167 // If links is not nil, the prev/next links from the API response headers |
167 // will be set (if they exist) in the structure. |
168 // will be set (if they exist) in the structure. |
168 func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, links *apiLinks, data interface{}) error { |
169 func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, links *apiLinks, data interface{}) error { |
169 if mc == nil { |
170 if mc == nil { |
170 return fmt.Errorf("use of uninitialized madon client") |
171 return errors.New("use of uninitialized madon client") |
171 } |
172 } |
172 |
173 |
173 if limitOptions != nil { |
174 if limitOptions != nil { |
174 if params == nil { |
175 if params == nil { |
175 params = make(apiCallParams) |
176 params = make(apiCallParams) |
192 } |
193 } |
193 |
194 |
194 // Make API call |
195 // Make API call |
195 r, err := restAPI(req) |
196 r, err := restAPI(req) |
196 if err != nil { |
197 if err != nil { |
197 return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error()) |
198 return errors.Wrapf(err, "API query (%s) failed", endPoint) |
198 } |
199 } |
199 |
200 |
200 if links != nil { |
201 if links != nil { |
201 pLinks, err := parseLink(r.Headers["Link"]) |
202 pLinks, err := parseLink(r.Headers["Link"]) |
202 if err != nil { |
203 if err != nil { |
203 return fmt.Errorf("cannot decode header links (%s): %s", method, err.Error()) |
204 return errors.Wrapf(err, "cannot decode header links (%s)", method) |
204 } |
205 } |
205 if pLinks != nil { |
206 if pLinks != nil { |
206 *links = *pLinks |
207 *links = *pLinks |
207 } |
208 } |
208 } |
209 } |
210 // Check for error reply |
211 // Check for error reply |
211 var errorResult Error |
212 var errorResult Error |
212 if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
213 if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
213 // The empty object is not an error |
214 // The empty object is not an error |
214 if errorResult.Text != "" { |
215 if errorResult.Text != "" { |
215 return fmt.Errorf("%s", errorResult.Text) |
216 return errors.New(errorResult.Text) |
216 } |
217 } |
217 } |
218 } |
218 |
219 |
219 // Not an error reply; let's unmarshal the data |
220 // Not an error reply; let's unmarshal the data |
220 err = json.Unmarshal([]byte(r.Body), &data) |
221 err = json.Unmarshal([]byte(r.Body), &data) |
221 if err != nil { |
222 if err != nil { |
222 return fmt.Errorf("cannot decode API response (%s): %s", method, err.Error()) |
223 return errors.Wrapf(err, "cannot decode API response (%s)", method) |
223 } |
224 } |
224 return nil |
225 return nil |
225 } |
226 } |