-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update-readme-with-new-client-init
- Loading branch information
Showing
7 changed files
with
216 additions
and
1 deletion.
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
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,72 @@ | ||
package godo | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
const billingHistoryBasePath = "v2/customers/my/billing_history" | ||
|
||
// BillingHistoryService is an interface for interfacing with the BillingHistory | ||
// endpoints of the DigitalOcean API | ||
// See: https://developers.digitalocean.com/documentation/v2/#billing_history | ||
type BillingHistoryService interface { | ||
List(context.Context, *ListOptions) (*BillingHistory, *Response, error) | ||
} | ||
|
||
// BillingHistoryServiceOp handles communication with the BillingHistory related methods of | ||
// the DigitalOcean API. | ||
type BillingHistoryServiceOp struct { | ||
client *Client | ||
} | ||
|
||
var _ BillingHistoryService = &BillingHistoryServiceOp{} | ||
|
||
// BillingHistory represents a DigitalOcean Billing History | ||
type BillingHistory struct { | ||
BillingHistory []BillingHistoryEntry `json:"billing_history"` | ||
Links *Links `json:"links"` | ||
Meta *Meta `json:"meta"` | ||
} | ||
|
||
// BillingHistoryEntry represents an entry in a customer's Billing History | ||
type BillingHistoryEntry struct { | ||
Description string `json:"description"` | ||
Amount string `json:"amount"` | ||
InvoiceID *string `json:"invoice_id"` | ||
InvoiceUUID *string `json:"invoice_uuid"` | ||
Date time.Time `json:"date"` | ||
Type string `json:"type"` | ||
} | ||
|
||
func (b BillingHistory) String() string { | ||
return Stringify(b) | ||
} | ||
|
||
// List the Billing History for a customer | ||
func (s *BillingHistoryServiceOp) List(ctx context.Context, opt *ListOptions) (*BillingHistory, *Response, error) { | ||
path, err := addOptions(billingHistoryBasePath, opt) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil) | ||
if err != nil { | ||
return nil, nil, err | ||
} | ||
|
||
root := new(BillingHistory) | ||
resp, err := s.client.Do(ctx, req, root) | ||
if err != nil { | ||
return nil, resp, err | ||
} | ||
if l := root.Links; l != nil { | ||
resp.Links = l | ||
} | ||
if m := root.Meta; m != nil { | ||
resp.Meta = m | ||
} | ||
|
||
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,71 @@ | ||
package godo | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBillingHistory_List(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
mux.HandleFunc("/v2/customers/my/billing_history", func(w http.ResponseWriter, r *http.Request) { | ||
testMethod(t, r, http.MethodGet) | ||
fmt.Fprint(w, `{ | ||
"billing_history": [ | ||
{ | ||
"description": "Invoice for May 2018", | ||
"amount": "12.34", | ||
"invoice_id": "123", | ||
"invoice_uuid": "example-uuid", | ||
"date": "2018-06-01T08:44:38Z", | ||
"type": "Invoice" | ||
}, | ||
{ | ||
"description": "Payment (MC 2018)", | ||
"amount": "-12.34", | ||
"date": "2018-06-02T08:44:38Z", | ||
"type": "Payment" | ||
} | ||
], | ||
"meta": { | ||
"total": 2 | ||
} | ||
}`) | ||
}) | ||
|
||
history, resp, err := client.BillingHistory.List(ctx, nil) | ||
if err != nil { | ||
t.Errorf("BillingHistory.List returned error: %v", err) | ||
} | ||
|
||
expectedBillingHistory := []BillingHistoryEntry{ | ||
{ | ||
Description: "Invoice for May 2018", | ||
Amount: "12.34", | ||
InvoiceID: String("123"), | ||
InvoiceUUID: String("example-uuid"), | ||
Date: time.Date(2018, 6, 1, 8, 44, 38, 0, time.UTC), | ||
Type: "Invoice", | ||
}, | ||
{ | ||
Description: "Payment (MC 2018)", | ||
Amount: "-12.34", | ||
InvoiceID: nil, | ||
InvoiceUUID: nil, | ||
Date: time.Date(2018, 6, 2, 8, 44, 38, 0, time.UTC), | ||
Type: "Payment", | ||
}, | ||
} | ||
entries := history.BillingHistory | ||
if !reflect.DeepEqual(entries, expectedBillingHistory) { | ||
t.Errorf("BillingHistory.List\nBillingHistory: got=%#v\nwant=%#v", entries, expectedBillingHistory) | ||
} | ||
expectedMeta := &Meta{Total: 2} | ||
if !reflect.DeepEqual(resp.Meta, expectedMeta) { | ||
t.Errorf("BillingHistory.List\nMeta: got=%#v\nwant=%#v", resp.Meta, expectedMeta) | ||
} | ||
} |
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
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
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
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