Skip to content

Commit

Permalink
Standardize on returning objects, not the json roots.
Browse files Browse the repository at this point in the history
Let's be nice to our consumers - they don't care about the root, they care about the object. Even when the user needs access to the Links, that's accessible via the resp variable returned.
  • Loading branch information
phillbaker authored and bryanl committed Jun 1, 2015
1 parent bbf01e0 commit 1df42b9
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 44 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 droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ const dropletBasePath = "v2/droplets"
// See: https://developers.digitalocean.com/documentation/v2#droplets
type DropletsService interface {
List(*ListOptions) ([]Droplet, *Response, error)
Get(int) (*DropletRoot, *Response, error)
Create(*DropletCreateRequest) (*DropletRoot, *Response, error)
Get(int) (*Droplet, *Response, error)
Create(*DropletCreateRequest) (*Droplet, *Response, error)
Delete(int) (*Response, error)
Kernels(int, *ListOptions) ([]Kernel, *Response, error)
Snapshots(int, *ListOptions) ([]Image, *Response, error)
Expand Down Expand Up @@ -64,7 +64,7 @@ func (d Droplet) String() string {
}

// DropletRoot represents a Droplet root
type DropletRoot struct {
type dropletRoot struct {
Droplet *Droplet `json:"droplet"`
Links *Links `json:"links,omitempty"`
}
Expand Down Expand Up @@ -190,33 +190,33 @@ func (s *DropletsServiceOp) List(opt *ListOptions) ([]Droplet, *Response, error)
}

// Get individual droplet
func (s *DropletsServiceOp) Get(dropletID int) (*DropletRoot, *Response, error) {
func (s *DropletsServiceOp) Get(dropletID int) (*Droplet, *Response, error) {
path := fmt.Sprintf("%s/%d", dropletBasePath, dropletID)

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

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

return root, resp, err
return root.Droplet, resp, err
}

// Create droplet
func (s *DropletsServiceOp) Create(createRequest *DropletCreateRequest) (*DropletRoot, *Response, error) {
func (s *DropletsServiceOp) Create(createRequest *DropletCreateRequest) (*Droplet, *Response, error) {
path := dropletBasePath

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

root := new(DropletRoot)
root := new(dropletRoot)
resp, err := s.client.Do(req, root)
if err != nil {
return nil, resp, err
Expand All @@ -225,7 +225,7 @@ func (s *DropletsServiceOp) Create(createRequest *DropletCreateRequest) (*Drople
resp.Links = l
}

return root, resp, err
return root.Droplet, resp, err
}

// Delete droplet
Expand Down
6 changes: 3 additions & 3 deletions droplets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func TestDroplets_GetDroplet(t *testing.T) {
t.Errorf("Droplet.Get returned error: %v", err)
}

expected := &DropletRoot{Droplet: &Droplet{ID: 12345}}
expected := &Droplet{ID: 12345}
if !reflect.DeepEqual(droplets, expected) {
t.Errorf("Droplets.Get returned %+v, expected %+v", droplets, expected)
}
Expand Down Expand Up @@ -150,12 +150,12 @@ func TestDroplets_Create(t *testing.T) {
fmt.Fprintf(w, `{"droplet":{"id":1}, "links":{"actions": [{"id": 1, "href": "http://example.com", "rel": "create"}]}}`)
})

root, resp, err := client.Droplets.Create(createRequest)
droplet, resp, err := client.Droplets.Create(createRequest)
if err != nil {
t.Errorf("Droplets.Create returned error: %v", err)
}

if id := root.Droplet.ID; id != 1 {
if id := droplet.ID; id != 1 {
t.Errorf("expected id '%d', received '%d'", 1, id)
}

Expand Down

0 comments on commit 1df42b9

Please sign in to comment.