JSON API » Sample clients » Go

package main

// This is a sample client for the Enswitch JSON API.
// It is released into the public domain.
// It comes with absolutely no warranty, and is not supported.

import "encoding/json"
import "fmt"
import "log"
import "net/http"
import "os"
import "strconv"

var base string = "http://enswitch.example.com/api/json/"
var function string = "access/list"
var username string = "testuser"
var password string = "testpass"

type JSON struct {
	Responses []Response
	Data []Data
}

type Response struct {
	Code string
	Key string
	Message string
}

type Data struct {
	Id string
	Name string
}

func perror( e error ) {
	if e != nil {
		panic( e )
	}
}

func main() {
	reply, error := http.Get( base + function + "/?auth_username=" + username + ";auth_password=" + password )
	perror( error )
	defer reply.Body.Close()

	var output JSON
	error = json.NewDecoder( reply.Body ).Decode( &output )
	perror( error )
	fatal := false
	for _, r := range output.Responses {
		code, error := strconv.Atoi( r.Code )
		perror( error )
		if code >= 400 {
			log.Print( r.Message )
			fatal = true
		}
	}
	if fatal {
		os.Exit( 1 )
	}
	for _, d := range output.Data {
		fmt.Printf( "Remote access account id=%s, name=%s.\n", d.Id, d.Name );
	}
}