Skip to content

Commit

Permalink
Add support for the /v2/account endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewsomething committed Apr 6, 2015
1 parent 006aa44 commit ccc93aa
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
50 changes: 50 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package godo

// AccountService is an interface for interfacing with the Account
// endpoints of the Digital Ocean API
// See: https://developers.digitalocean.com/documentation/v2/#account
type AccountService interface {
Get() (*AccountRoot, *Response, error)
}

// AccountServiceOp handles communication with the Account related methods of
// the DigitalOcean API.
type AccountServiceOp struct {
client *Client
}

var _ AccountService = &AccountServiceOp{}

// Account represents a DigitalOcean Account
type Account struct {
DropletLimit int `json:"droplet_limit,omitempty"`
Email string `json:"email,omitempty"`
UUID string `json:"uuid,omitempty"`
EmailVerified bool `json:"email_verified,omitempty"`
}

type AccountRoot struct {
Account *Account `json:"account"`
}

func (r Account) String() string {
return Stringify(r)
}

// Get DigitalOcean account info
func (s *AccountServiceOp) Get() (*AccountRoot, *Response, error) {
path := "v2/account"

req, err := s.client.NewRequest("GET", path, nil)
if err != nil {
return nil, nil, err
}

root := new(AccountRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}
54 changes: 54 additions & 0 deletions account_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package godo

import (
"fmt"
"net/http"
"reflect"
"testing"
)

func TestAccountGet(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/v2/account", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")

response := `
{ "account": {
"droplet_limit": 25,
"email": "sammy@digitalocean.com",
"uuid": "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
"email_verified": true
}
}`

fmt.Fprint(w, response)
})

acct, _, err := client.Account.Get()
if err != nil {
t.Errorf("Account.Get returned error: %v", err)
}

expected := &AccountRoot{Account: &Account{DropletLimit: 25, Email: "sammy@digitalocean.com", UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified: true}}
if !reflect.DeepEqual(acct, expected) {
t.Errorf("Account.Get returned %+v, expected %+v", acct, expected)
}
}

func TestAccountString(t *testing.T) {
acct := &Account{
DropletLimit: 25,
Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef",
EmailVerified: true,
}

stringified := acct.String()
expected := `godo.Account{DropletLimit:25, Email:"sammy@digitalocean.com", UUID:"b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified:true}`
if expected != stringified {
t.Errorf("Account.String returned %+v, expected %+v", stringified, expected)
}

}
2 changes: 2 additions & 0 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Client struct {
Rate Rate

// Services used for communicating with the API
Account AccountService
Actions ActionsService
Domains DomainsService
Droplets DropletsService
Expand Down Expand Up @@ -129,6 +130,7 @@ func NewClient(httpClient *http.Client) *Client {
baseURL, _ := url.Parse(defaultBaseURL)

c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.Account = &AccountServiceOp{client: c}
c.Actions = &ActionsServiceOp{client: c}
c.Domains = &DomainsServiceOp{client: c}
c.Droplets = &DropletsServiceOp{client: c}
Expand Down

0 comments on commit ccc93aa

Please sign in to comment.