Golang binding of Sakura Cloud API client.
- Automatic authorization / authentication handling
- Automatic retry handling
- Client side rate limitter
httptest.Serverintegration for better testing experience
The library provides Client struct as its sole public API. All operations are against it. You have to first allocate it:
import "github.com/sacloud/saclient-go"
var theClient saclient.ClientYou can pass various flags to the allocated client using environment variables, command line flags, and others. Let's say we are going to accept those two here:
import "os"
func main() {
fs := theClient.FlagSet()
// Load settings from the command line arguments
err := fs.Parse(os.Args[1:])
if err != nil {
os.Exit(1)
}
// Load settings from the environment variables
err = theClient.SetEnviron(os.Environ())
if err != nil {
os.Exit(1)
}
// This is optional (done automatically)
// but it is a bit polite to do so explicitly
err = theClient.Popuate()
if err != nil {
os.Exit(1)
}
}After these set up you can use the client like other HTTP clients, by using its Do method.
import (
"strings"
"http"
)
func yourLogic() error {
// Say you want to post something...
api := "https://secure.sakura.ad.jp/cloud/zone/is1a/api/cloud/1.1/..."
io := strings.NewReader(`{
"parameter": [
"values", "values", "..."
]
}`)
// Create a request
req, err := http.NewRequest("POST", api, io);
if err != nil {
return err
}
// then call it
res, err := theClient.Do(req)
if err != nil {
return err
}
// ...
}