Skip to content

Commit

Permalink
Merge pull request digitalocean#52 from digitalocean/refactor/roots
Browse files Browse the repository at this point in the history
Standardize on returning objects, not the json roots.
  • Loading branch information
bryanl committed Jun 1, 2015
2 parents bbf01e0 + de59235 commit d681ba7
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 77 deletions.
10 changes: 5 additions & 5 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package godo
// endpoints of the Digital Ocean API
// See: https://developers.digitalocean.com/documentation/v2/#account
type AccountService interface {
Get() (*AccountRoot, *Response, error)
Get() (*Account, *Response, error)
}

// AccountServiceOp handles communication with the Account related methods of
Expand All @@ -23,7 +23,7 @@ type Account struct {
EmailVerified bool `json:"email_verified,omitempty"`
}

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

Expand All @@ -32,19 +32,19 @@ func (r Account) String() string {
}

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

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

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

return root, resp, err
return root.Account, resp, err
}
4 changes: 2 additions & 2 deletions account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestAccountGet(t *testing.T) {
t.Errorf("Account.Get returned error: %v", err)
}

expected := &AccountRoot{Account: &Account{DropletLimit: 25, Email: "sammy@digitalocean.com",
UUID: "b6fr89dbf6d9156cace5f3c78dc9851d957381ef", EmailVerified: true}}
expected := &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)
}
Expand Down
35 changes: 19 additions & 16 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const domainsBasePath = "v2/domains"
// https://developers.digitalocean.com/documentation/v2#domain-records
type DomainsService interface {
List(*ListOptions) ([]Domain, *Response, error)
Get(string) (*DomainRoot, *Response, error)
Create(*DomainCreateRequest) (*DomainRoot, *Response, error)
Get(string) (*Domain, *Response, error)
Create(*DomainCreateRequest) (*Domain, *Response, error)
Delete(string) (*Response, error)

Records(string, *ListOptions) ([]DomainRecord, *Response, error)
Expand All @@ -35,8 +35,8 @@ type Domain struct {
ZoneFile string `json:"zone_file"`
}

// DomainRoot represents a response from the Digital Ocean API
type DomainRoot struct {
// domainRoot represents a response from the Digital Ocean API
type domainRoot struct {
Domain *Domain `json:"domain"`
}

Expand All @@ -52,12 +52,12 @@ type DomainCreateRequest struct {
}

// DomainRecordRoot is the root of an individual Domain Record response
type DomainRecordRoot struct {
type domainRecordRoot struct {
DomainRecord *DomainRecord `json:"domain_record"`
}

// DomainRecordsRoot is the root of a group of Domain Record responses
type DomainRecordsRoot struct {
type domainRecordsRoot struct {
DomainRecords []DomainRecord `json:"domain_records"`
Links *Links `json:"links"`
}
Expand Down Expand Up @@ -113,39 +113,42 @@ func (s DomainsServiceOp) List(opt *ListOptions) ([]Domain, *Response, error) {
}

// Get individual domain
func (s *DomainsServiceOp) Get(name string) (*DomainRoot, *Response, error) {
func (s *DomainsServiceOp) Get(name string) (*Domain, *Response, error) {
path := fmt.Sprintf("%s/%s", domainsBasePath, name)

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

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

return root, resp, err
return root.Domain, resp, err
}

// Create a new domain
func (s *DomainsServiceOp) Create(createRequest *DomainCreateRequest) (*DomainRoot, *Response, error) {
func (s *DomainsServiceOp) Create(createRequest *DomainCreateRequest) (*Domain, *Response, error) {
path := domainsBasePath

req, err := s.client.NewRequest("POST", path, createRequest)
if err != nil {
fmt.Printf("1Something bad happened: %+v", err)
return nil, nil, err
}

root := new(DomainRoot)
root := new(domainRoot)
resp, err := s.client.Do(req, root)
fmt.Printf("%+v\n", resp)
if err != nil {
fmt.Printf("2Something bad happened: %+v", err)
return nil, resp, err
}

return root, resp, err
fmt.Printf("%+v\n", root)
return root.Domain, resp, err
}

// Delete domain
Expand Down Expand Up @@ -185,7 +188,7 @@ func (s *DomainsServiceOp) Records(domain string, opt *ListOptions) ([]DomainRec
return nil, nil, err
}

root := new(DomainRecordsRoot)
root := new(domainRecordsRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
Expand All @@ -206,7 +209,7 @@ func (s *DomainsServiceOp) Record(domain string, id int) (*DomainRecord, *Respon
return nil, nil, err
}

record := new(DomainRecordRoot)
record := new(domainRecordRoot)
resp, err := s.client.Do(req, record)
if err != nil {
return nil, resp, err
Expand Down Expand Up @@ -261,7 +264,7 @@ func (s *DomainsServiceOp) CreateRecord(
return nil, nil, err
}

d := new(DomainRecordRoot)
d := new(domainRecordRoot)
resp, err := s.client.Do(req, d)
if err != nil {
return nil, resp, err
Expand Down
12 changes: 3 additions & 9 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestDomains_GetDomain(t *testing.T) {
t.Errorf("domain.Get returned error: %v", err)
}

expected := &DomainRoot{Domain: &Domain{Name: "example.com"}}
expected := &Domain{Name: "example.com"}
if !reflect.DeepEqual(domains, expected) {
t.Errorf("domains.Get returned %+v, expected %+v", domains, expected)
}
Expand All @@ -117,21 +117,15 @@ func TestDomains_Create(t *testing.T) {
t.Errorf("Request body = %+v, expected %+v", v, createRequest)
}

dr := DomainRoot{&Domain{Name: v.Name}}
b, err := json.Marshal(dr)
if err != nil {
t.Fatal(err)
}

fmt.Fprint(w, string(b))
fmt.Fprint(w, `{"domain":{"name":"example.com"}}`)
})

domain, _, err := client.Domains.Create(createRequest)
if err != nil {
t.Errorf("Domains.Create returned error: %v", err)
}

expected := &DomainRoot{Domain: &Domain{Name: "example.com"}}
expected := &Domain{Name: "example.com"}
if !reflect.DeepEqual(domain, expected) {
t.Errorf("Domains.Create returned %+v, expected %+v", domain, expected)
}
Expand Down
18 changes: 9 additions & 9 deletions droplet_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (s *DropletActionsServiceOp) Rename(id int, name string) (*Action, *Respons
return s.doAction(id, request)
}

// Snapshot a Droplet
// Snapshot a Droplet.
func (s *DropletActionsServiceOp) Snapshot(id int, name string) (*Action, *Response, error) {
requestType := "snapshot"
request := &ActionRequest{
Expand All @@ -113,49 +113,49 @@ func (s *DropletActionsServiceOp) Snapshot(id int, name string) (*Action, *Respo
return s.doAction(id, request)
}

// Disable backups for a droplet
// DisableBackups disables backups for a droplet.
func (s *DropletActionsServiceOp) DisableBackups(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "disable_backups"}
return s.doAction(id, request)
}

// Reset password for a droplet
// PasswordReset resets the password for a droplet.
func (s *DropletActionsServiceOp) PasswordReset(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "password_reset"}
return s.doAction(id, request)
}

// Rebuild droplet from an image with a given id
// RebuildByImageID rebuilds a droplet droplet from an image with a given id.
func (s *DropletActionsServiceOp) RebuildByImageID(id, imageID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": imageID}
return s.doAction(id, request)
}

// Rebuild a droplet from an image with a given slug
// RebuildByImageSlug rebuilds a droplet from an image with a given slug.
func (s *DropletActionsServiceOp) RebuildByImageSlug(id int, slug string) (*Action, *Response, error) {
request := &ActionRequest{"type": "rebuild", "image": slug}
return s.doAction(id, request)
}

// Change a droplet kernel
// ChangeKernel changes the kernel for a droplet.
func (s *DropletActionsServiceOp) ChangeKernel(id, kernelID int) (*Action, *Response, error) {
request := &ActionRequest{"type": "change_kernel", "kernel": kernelID}
return s.doAction(id, request)
}

// Enable IPv6 on a droplet
// EnableIPv6 enables IPv6 for a droplet.
func (s *DropletActionsServiceOp) EnableIPv6(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_ipv6"}
return s.doAction(id, request)
}

// Enable private networking on a droplet
// EnablePrivateNetworking enables private networking for a droplet.
func (s *DropletActionsServiceOp) EnablePrivateNetworking(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "enable_private_networking"}
return s.doAction(id, request)
}

// Upgrade a droplet
// Upgrade a droplet.
func (s *DropletActionsServiceOp) Upgrade(id int) (*Action, *Response, error) {
request := &ActionRequest{"type": "upgrade"}
return s.doAction(id, request)
Expand Down
Loading

0 comments on commit d681ba7

Please sign in to comment.