# HG changeset patch # User Mikael Berthe # Date 1492109681 -7200 # Node ID 467dd91bf1f90b026d2f0bd62578a7652fc27baa # Parent fc7cd6c90b2e4c7b558632efb4a04f416c93e5f4 Add GetCurrentInstance() diff -r fc7cd6c90b2e -r 467dd91bf1f9 instance.go --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/instance.go Thu Apr 13 20:54:41 2017 +0200 @@ -0,0 +1,34 @@ +package gondole + +import ( + "encoding/json" + "fmt" + + "github.com/sendgrid/rest" +) + +// GetCurrentInstance returns current instance information +func (g *Client) GetCurrentInstance() (*Instance, error) { + req := g.prepareRequest("instance") + r, err := rest.API(req) + if err != nil { + return nil, fmt.Errorf("instance: %s", err.Error()) + } + + // Check for error reply + var errorResult Error + if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil { + // The empty object is not an error + if errorResult.Text != "" { + return nil, fmt.Errorf("%s", errorResult.Text) + } + } + + // Not an error reply; let's unmarshal the data + var i Instance + err = json.Unmarshal([]byte(r.Body), &i) + if err != nil { + return nil, fmt.Errorf("instance API: %s", err.Error()) + } + return &i, nil +}