instance.go
author Mikael Berthe <mikael@lilotux.net>
Thu, 13 Apr 2017 20:54:41 +0200
changeset 112 467dd91bf1f9
child 120 579912e9d0ef
permissions -rw-r--r--
Add GetCurrentInstance()
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
112
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
package gondole
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
import (
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
	"encoding/json"
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
	"fmt"
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
	"github.com/sendgrid/rest"
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
)
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
// GetCurrentInstance returns current instance information
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
func (g *Client) GetCurrentInstance() (*Instance, error) {
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
	req := g.prepareRequest("instance")
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
	r, err := rest.API(req)
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
	if err != nil {
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
		return nil, fmt.Errorf("instance: %s", err.Error())
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
	}
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
	// Check for error reply
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
	var errorResult Error
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
	if err := json.Unmarshal([]byte(r.Body), &errorResult); err == nil {
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
		// The empty object is not an error
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
		if errorResult.Text != "" {
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
			return nil, fmt.Errorf("%s", errorResult.Text)
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
		}
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	}
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
	// Not an error reply; let's unmarshal the data
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	var i Instance
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	err = json.Unmarshal([]byte(r.Body), &i)
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	if err != nil {
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
		return nil, fmt.Errorf("instance API: %s", err.Error())
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	}
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	return &i, nil
467dd91bf1f9 Add GetCurrentInstance()
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}