author | Mikael Berthe <mikael@lilotux.net> |
Fri, 28 Apr 2017 16:22:39 +0200 | |
changeset 150 | cd328b30af77 |
parent 149 | 5f922977d7c7 |
child 155 | 0c581e0108da |
permissions | -rw-r--r-- |
130 | 1 |
/* |
2 |
Copyright 2017 Ollivier Robert |
|
3 |
Copyright 2017 Mikael Berthe |
|
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" |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
16 |
"strconv" |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
17 |
"strings" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
18 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
19 |
"github.com/sendgrid/rest" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
20 |
) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
21 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
22 |
// restAPI actually does the HTTP query |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
23 |
// 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
|
24 |
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
|
25 |
c := &rest.Client{HTTPClient: http.DefaultClient} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
26 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
27 |
// Build the HTTP request object. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
28 |
if len(request.QueryParams) != 0 { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
29 |
// Add parameters to the URL |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
30 |
request.BaseURL += "?" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
31 |
urlp := url.Values{} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
32 |
for key, value := range request.QueryParams { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
33 |
// 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
|
34 |
// numbers, but it needs the brackets. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
35 |
// Let's check if the key matches '^.+\[.*\]$' |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
36 |
klen := len(key) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
37 |
if klen == 0 { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
38 |
continue |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
39 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
40 |
i := strings.Index(key, "[") |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
41 |
if key[klen-1] == ']' && i > 0 { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
42 |
// 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
|
43 |
key = key[:i] + "[]" |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
44 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
45 |
urlp.Add(key, value) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
46 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
47 |
urlpstr := urlp.Encode() |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
48 |
request.BaseURL += urlpstr |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
49 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
50 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
51 |
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
|
52 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
53 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
54 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
55 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
56 |
for key, value := range request.Headers { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
57 |
req.Header.Set(key, value) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
58 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
59 |
_, exists := req.Header["Content-Type"] |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
60 |
if len(request.Body) > 0 && !exists { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
61 |
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
|
62 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
63 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
64 |
// 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
|
65 |
res, err := c.MakeRequest(req) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
66 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
67 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
68 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
69 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
70 |
// Build Response object. |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
71 |
response, err := rest.BuildResponse(res) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
72 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
73 |
return nil, err |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
74 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
75 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
76 |
return response, nil |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
77 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
78 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
79 |
// prepareRequest inserts all pre-defined stuff |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
80 |
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
|
81 |
var req rest.Request |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
82 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
83 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
84 |
return req, ErrUninitializedClient |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
85 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
86 |
|
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
87 |
endPoint := mc.APIBase + "/" + target |
125
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 |
// Request headers |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
90 |
hdrs := make(map[string]string) |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
91 |
hdrs["User-Agent"] = fmt.Sprintf("madon/%s", MadonVersion) |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
92 |
if mc.UserToken != nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
93 |
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
|
94 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
95 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
96 |
req = rest.Request{ |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
97 |
BaseURL: endPoint, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
98 |
Headers: hdrs, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
99 |
Method: method, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
100 |
QueryParams: params, |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
101 |
} |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
102 |
return req, nil |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
103 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
104 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
105 |
// apiCall makes a call to the Mastodon API server |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
106 |
func (mc *Client) apiCall(endPoint string, method rest.Method, params apiCallParams, limitOptions *LimitParams, data interface{}) error { |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
107 |
if mc == nil { |
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
108 |
return fmt.Errorf("use of uninitialized madon client") |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
109 |
} |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
110 |
|
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
111 |
if limitOptions != nil { |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
112 |
if params == nil { |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
113 |
params = make(apiCallParams) |
150
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
114 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
115 |
if limitOptions.Limit > 0 { |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
116 |
params["limit"] = strconv.Itoa(limitOptions.Limit) |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
117 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
118 |
if limitOptions.SinceID > 0 { |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
119 |
params["since_id"] = strconv.Itoa(limitOptions.SinceID) |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
120 |
} |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
121 |
if limitOptions.MaxID > 0 { |
cd328b30af77
Fix mistake in previous changeset
Mikael Berthe <mikael@lilotux.net>
parents:
149
diff
changeset
|
122 |
params["max_id"] = strconv.Itoa(limitOptions.MaxID) |
149
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
123 |
} |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
124 |
} |
5f922977d7c7
Add support for limits and paging ({since,max}_id) API parameters
Mikael Berthe <mikael@lilotux.net>
parents:
138
diff
changeset
|
125 |
|
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
126 |
// Prepare query |
138
23d3a518d0ad
Update package name in source files
Mikael Berthe <mikael@lilotux.net>
parents:
130
diff
changeset
|
127 |
req, err := mc.prepareRequest(endPoint, method, params) |
128
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
128 |
if err != nil { |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
129 |
return err |
a5a00fad7a32
Add checks for client initialization
Mikael Berthe <mikael@lilotux.net>
parents:
125
diff
changeset
|
130 |
} |
125
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
131 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
132 |
// Make API call |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
133 |
r, err := restAPI(req) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
134 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
135 |
return fmt.Errorf("API query (%s) failed: %s", endPoint, err.Error()) |
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 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
138 |
// Check for error reply |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
139 |
var errorResult Error |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
140 |
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
|
141 |
// 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
|
142 |
if errorResult.Text != "" { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
143 |
return fmt.Errorf("%s", errorResult.Text) |
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 |
|
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
147 |
// 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
|
148 |
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
|
149 |
if err != nil { |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
150 |
return fmt.Errorf("cannot decode API response (%s): %s", method, err.Error()) |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
151 |
} |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
152 |
return nil |
2bbb72b9ebf6
Rework the API wrappers to handle arrays of parameters
Mikael Berthe <mikael@lilotux.net>
parents:
diff
changeset
|
153 |
} |