Skip to content

Commit

Permalink
Merge pull request #289 from timoreimann/deserialize-meta-the-return
Browse files Browse the repository at this point in the history
Deserialize meta field for remaining APIs
  • Loading branch information
zachgersh authored Dec 12, 2019
2 parents 1fb315c + 38b71c3 commit a3a5160
Show file tree
Hide file tree
Showing 35 changed files with 582 additions and 129 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## unreleased

- #288 Add Balance Get method - @rbutler
- #286 Deserialize meta - @timoreimann
- #286,#289 Deserialize meta field - @timoreimann

## [v1.28.0] - 2019-12-04

Expand Down
4 changes: 4 additions & 0 deletions action.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var _ ActionsService = &ActionsServiceOp{}
type actionsRoot struct {
Actions []Action `json:"actions"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

type actionRoot struct {
Expand Down Expand Up @@ -74,6 +75,9 @@ func (s *ActionsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Action
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Actions, resp, err
}
Expand Down
28 changes: 23 additions & 5 deletions action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,36 @@ func TestAction_List(t *testing.T) {
defer teardown()

mux.HandleFunc("/v2/actions", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, `{"actions": [{"id":1},{"id":2}]}`)
fmt.Fprint(w, `{
"actions": [
{
"id": 1
},
{
"id": 2
}
],
"meta": {
"total": 2
}
},
`)
testMethod(t, r, http.MethodGet)
})

actions, _, err := client.Actions.List(ctx, nil)
actions, resp, err := client.Actions.List(ctx, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

expected := []Action{{ID: 1}, {ID: 2}}
if len(actions) != len(expected) || actions[0].ID != expected[0].ID || actions[1].ID != expected[1].ID {
t.Fatalf("unexpected response")
expectedActions := []Action{{ID: 1}, {ID: 2}}
if !reflect.DeepEqual(actions, expectedActions) {
t.Errorf("Actions.List returned actions %+v, expected %+v", actions, expectedActions)
}

expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Actions.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down
4 changes: 4 additions & 0 deletions cdn.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type cdnRoot struct {
type cdnsRoot struct {
Endpoints []CDN `json:"endpoints"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// CDNCreateRequest represents a request to create a CDN.
Expand Down Expand Up @@ -93,6 +94,9 @@ func (c CDNServiceOp) List(ctx context.Context, opt *ListOptions) ([]CDN, *Respo
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Endpoints, resp, err
}
Expand Down
20 changes: 15 additions & 5 deletions cdn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@ func TestCDN_ListCDN(t *testing.T) {
"ttl": 3600,
"created_at": "2012-10-03T15:00:01.05Z"
}
]
],
"meta": {
"total": 2
}
}`,
)
})

cdns, _, err := client.CDNs.List(ctx, nil)
cdns, resp, err := client.CDNs.List(ctx, nil)
if err != nil {
t.Errorf("CDNs.List returned error: %v", err)
}

expected := []CDN{
expectedCDNs := []CDN{
{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Origin: "my-space.nyc3.digitaloceanspaces.com",
Expand All @@ -59,8 +62,15 @@ func TestCDN_ListCDN(t *testing.T) {
},
}

if !reflect.DeepEqual(cdns, expected) {
t.Errorf("CDNs.List returned %+v, expected %+v", cdns, expected)
if !reflect.DeepEqual(cdns, expectedCDNs) {
t.Errorf("CDNs.List returned CDNs %+v, expected %+v", cdns, expectedCDNs)
}

expectedMeta := &Meta{
Total: 2,
}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("CDNs.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down
4 changes: 4 additions & 0 deletions certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type certificateRoot struct {
type certificatesRoot struct {
Certificates []Certificate `json:"certificates"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// CertificatesServiceOp handles communication with certificates methods of the DigitalOcean API.
Expand Down Expand Up @@ -93,6 +94,9 @@ func (c *CertificatesServiceOp) List(ctx context.Context, opt *ListOptions) ([]C
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Certificates, resp, nil
}
Expand Down
13 changes: 9 additions & 4 deletions certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var certsJSONResponse = `
],
"links": {},
"meta": {
"total": 1
"total": 2
}
}
`
Expand Down Expand Up @@ -103,13 +103,13 @@ func TestCertificates_List(t *testing.T) {
fmt.Fprint(w, certsJSONResponse)
})

certificates, _, err := client.Certificates.List(ctx, nil)
certificates, resp, err := client.Certificates.List(ctx, nil)

if err != nil {
t.Errorf("Certificates.List returned error: %v", err)
}

expected := []Certificate{
expectedCertificates := []Certificate{
{
ID: "892071a0-bb95-49bc-8021-3afd67a210bf",
Name: "web-cert-01",
Expand All @@ -132,7 +132,12 @@ func TestCertificates_List(t *testing.T) {
},
}

assert.Equal(t, expected, certificates)
assert.Equal(t, expectedCertificates, certificates)

expectedMeta := &Meta{
Total: 2,
}
assert.Equal(t, expectedMeta, resp.Meta)
}

func TestCertificates_Create(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type domainRoot struct {
type domainsRoot struct {
Domains []Domain `json:"domains"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// DomainCreateRequest respresents a request to create a domain.
Expand Down Expand Up @@ -122,6 +123,9 @@ func (s DomainsServiceOp) List(ctx context.Context, opt *ListOptions) ([]Domain,
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Domains, resp, err
}
Expand Down
30 changes: 25 additions & 5 deletions domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,34 @@ func TestDomains_ListDomains(t *testing.T) {

mux.HandleFunc("/v2/domains", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"domains": [{"name":"foo.com"},{"name":"bar.com"}]}`)
fmt.Fprint(w, `{
"domains": [
{
"name":"foo.com"
},
{
"name":"bar.com"
}
],
"meta": {
"total": 2
}
}`)
})

domains, _, err := client.Domains.List(ctx, nil)
domains, resp, err := client.Domains.List(ctx, nil)
if err != nil {
t.Errorf("Domains.List returned error: %v", err)
}

expected := []Domain{{Name: "foo.com"}, {Name: "bar.com"}}
if !reflect.DeepEqual(domains, expected) {
t.Errorf("Domains.List returned %+v, expected %+v", domains, expected)
expectedDomains := []Domain{{Name: "foo.com"}, {Name: "bar.com"}}
if !reflect.DeepEqual(domains, expectedDomains) {
t.Errorf("Domains.List returned domains %+v, expected %+v", domains, expectedDomains)
}

expectedMeta := &Meta{Total: 2}
if !reflect.DeepEqual(resp.Meta, expectedMeta) {
t.Errorf("Domains.List returned meta %+v, expected %+v", resp.Meta, expectedMeta)
}
}

Expand Down Expand Up @@ -59,6 +76,9 @@ func TestDomains_RetrievePageByNumber(t *testing.T) {
"last":"http://example.com/v2/domains/?page=3",
"first":"http://example.com/v2/domains/?page=1"
}
},
"meta":{
"total":2
}
}`

Expand Down
19 changes: 19 additions & 0 deletions droplets.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,25 @@ type dropletRoot struct {
type dropletsRoot struct {
Droplets []Droplet `json:"droplets"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

type kernelsRoot struct {
Kernels []Kernel `json:"kernels,omitempty"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

type dropletSnapshotsRoot struct {
Snapshots []Image `json:"snapshots,omitempty"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

type backupsRoot struct {
Backups []Image `json:"backups,omitempty"`
Links *Links `json:"links"`
Meta *Meta `json:"meta"`
}

// DropletCreateImage identifies an image for the create request. It prefers slug over ID.
Expand Down Expand Up @@ -295,6 +299,9 @@ func (s *DropletsServiceOp) list(ctx context.Context, path string) ([]Droplet, *
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Droplets, resp, err
}
Expand Down Expand Up @@ -449,6 +456,9 @@ func (s *DropletsServiceOp) Kernels(ctx context.Context, dropletID int, opt *Lis
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Kernels, resp, err
}
Expand Down Expand Up @@ -478,6 +488,9 @@ func (s *DropletsServiceOp) Actions(ctx context.Context, dropletID int, opt *Lis
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Actions, resp, err
}
Expand Down Expand Up @@ -507,6 +520,9 @@ func (s *DropletsServiceOp) Backups(ctx context.Context, dropletID int, opt *Lis
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Backups, resp, err
}
Expand Down Expand Up @@ -536,6 +552,9 @@ func (s *DropletsServiceOp) Snapshots(ctx context.Context, dropletID int, opt *L
if l := root.Links; l != nil {
resp.Links = l
}
if m := root.Meta; m != nil {
resp.Meta = m
}

return root.Snapshots, resp, err
}
Expand Down
Loading

0 comments on commit a3a5160

Please sign in to comment.