112
|
1 |
package gondole |
|
2 |
|
|
3 |
import ( |
|
4 |
"encoding/json" |
|
5 |
"fmt" |
|
6 |
|
|
7 |
"github.com/sendgrid/rest" |
|
8 |
) |
|
9 |
|
|
10 |
// GetCurrentInstance returns current instance information |
|
11 |
func (g *Client) GetCurrentInstance() (*Instance, error) { |
|
12 |
req := g.prepareRequest("instance") |
|
13 |
r, err := rest.API(req) |
|
14 |
if err != nil { |
|
15 |
return nil, fmt.Errorf("instance: %s", err.Error()) |
|
16 |
} |
|
17 |
|
|
18 |
// Check for error reply |
|
19 |
var errorResult Error |
|
20 |
if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { |
|
21 |
// The empty object is not an error |
|
22 |
if errorResult.Text != "" { |
|
23 |
return nil, fmt.Errorf("%s", errorResult.Text) |
|
24 |
} |
|
25 |
} |
|
26 |
|
|
27 |
// Not an error reply; let's unmarshal the data |
|
28 |
var i Instance |
|
29 |
err = json.Unmarshal([]byte(r.Body), &i) |
|
30 |
if err != nil { |
|
31 |
return nil, fmt.Errorf("instance API: %s", err.Error()) |
|
32 |
} |
|
33 |
return &i, nil |
|
34 |
} |