instance.go
changeset 120 579912e9d0ef
parent 112 467dd91bf1f9
child 130 c450bb73f59a
equal deleted inserted replaced
119:22c8c58ad61b 120:579912e9d0ef
     1 package gondole
     1 package gondole
     2 
     2 
     3 import (
     3 import (
     4 	"encoding/json"
       
     5 	"fmt"
       
     6 
       
     7 	"github.com/sendgrid/rest"
     4 	"github.com/sendgrid/rest"
     8 )
     5 )
     9 
     6 
    10 // GetCurrentInstance returns current instance information
     7 // GetCurrentInstance returns current instance information
    11 func (g *Client) GetCurrentInstance() (*Instance, error) {
     8 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
     9 	var i Instance
    29 	err = json.Unmarshal([]byte(r.Body), &i)
    10 	if err := g.apiCall("instance", rest.Get, nil, &i); err != nil {
    30 	if err != nil {
    11 		return nil, err
    31 		return nil, fmt.Errorf("instance API: %s", err.Error())
       
    32 	}
    12 	}
    33 	return &i, nil
    13 	return &i, nil
    34 }
    14 }