forked from digitalocean/godo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for the /v2/account endpoint.
- Loading branch information
1 parent
006aa44
commit ccc93aa
Showing
3 changed files
with
106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters