Merge branch 'master' into rbutler/billing-history
This commit is contained in:
commit
33604ba0d1
|
@ -2,6 +2,8 @@
|
|||
|
||||
## unreleased
|
||||
|
||||
- #305 invoices: GetPDF and GetCSV methods - @rbutler
|
||||
- #304 Add NewFromToken convenience method to init client - @bentranter
|
||||
- #301 invoices: Get, Summary, and List methods - @rbutler
|
||||
|
||||
## [v1.30.0] - 2020-02-03
|
||||
|
|
14
godo.go
14
godo.go
|
@ -14,6 +14,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/google/go-querystring/query"
|
||||
"golang.org/x/oauth2"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -157,6 +158,17 @@ func addOptions(s string, opt interface{}) (string, error) {
|
|||
return origURL.String(), nil
|
||||
}
|
||||
|
||||
// NewFromToken returns a new DigitalOcean API client with the given API
|
||||
// token.
|
||||
func NewFromToken(token string) *Client {
|
||||
ctx := context.Background()
|
||||
|
||||
config := &oauth2.Config{}
|
||||
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: token})
|
||||
|
||||
return NewClient(oauth2.NewClient(ctx, ts))
|
||||
}
|
||||
|
||||
// NewClient returns a new DigitalOcean API client.
|
||||
func NewClient(httpClient *http.Client) *Client {
|
||||
if httpClient == nil {
|
||||
|
@ -201,7 +213,7 @@ func NewClient(httpClient *http.Client) *Client {
|
|||
// ClientOpt are options for New.
|
||||
type ClientOpt func(*Client) error
|
||||
|
||||
// New returns a new DIgitalOcean API client instance.
|
||||
// New returns a new DigitalOcean API client instance.
|
||||
func New(httpClient *http.Client, opts ...ClientOpt) (*Client, error) {
|
||||
c := NewClient(httpClient)
|
||||
for _, opt := range opts {
|
||||
|
|
|
@ -124,6 +124,11 @@ func TestNewClient(t *testing.T) {
|
|||
testClientDefaults(t, c)
|
||||
}
|
||||
|
||||
func TestNewFromToken(t *testing.T) {
|
||||
c := NewFromToken("my-token")
|
||||
testClientDefaults(t, c)
|
||||
}
|
||||
|
||||
func TestNew(t *testing.T) {
|
||||
c, err := New(nil)
|
||||
|
||||
|
|
39
invoices.go
39
invoices.go
|
@ -1,6 +1,7 @@
|
|||
package godo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
@ -14,6 +15,8 @@ const invoicesBasePath = "v2/customers/my/invoices"
|
|||
// See: https://developers.digitalocean.com/documentation/v2/#invoices
|
||||
type InvoicesService interface {
|
||||
Get(context.Context, string, *ListOptions) (*Invoice, *Response, error)
|
||||
GetPDF(context.Context, string) ([]byte, *Response, error)
|
||||
GetCSV(context.Context, string) ([]byte, *Response, error)
|
||||
List(context.Context, *ListOptions) (*InvoiceList, *Response, error)
|
||||
GetSummary(context.Context, string) (*InvoiceSummary, *Response, error)
|
||||
}
|
||||
|
@ -184,3 +187,39 @@ func (s *InvoicesServiceOp) GetSummary(ctx context.Context, invoiceUUID string)
|
|||
|
||||
return root, resp, err
|
||||
}
|
||||
|
||||
// Get the pdf for an Invoice
|
||||
func (s *InvoicesServiceOp) GetPDF(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
|
||||
path := fmt.Sprintf("%s/%s/pdf", invoicesBasePath, invoiceUUID)
|
||||
|
||||
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var root bytes.Buffer
|
||||
resp, err := s.client.Do(ctx, req, &root)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return root.Bytes(), resp, err
|
||||
}
|
||||
|
||||
// Get the csv for an Invoice
|
||||
func (s *InvoicesServiceOp) GetCSV(ctx context.Context, invoiceUUID string) ([]byte, *Response, error) {
|
||||
path := fmt.Sprintf("%s/%s/csv", invoicesBasePath, invoiceUUID)
|
||||
|
||||
req, err := s.client.NewRequest(ctx, http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var root bytes.Buffer
|
||||
resp, err := s.client.Do(ctx, req, &root)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return root.Bytes(), resp, err
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package godo
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
@ -269,3 +270,43 @@ func TestInvoices_GetSummary(t *testing.T) {
|
|||
t.Errorf("Invoices.GetSummary\nInvoiceSummary: got=%#v\nwant=%#v", invoiceSummaryResponse, &expectedSummary)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvoices_GetPDF(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid/pdf", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
fmt.Fprint(w, `some pdf content`)
|
||||
})
|
||||
|
||||
invoicePDFResponse, _, err := client.Invoices.GetPDF(ctx, "example-invoice-uuid")
|
||||
if err != nil {
|
||||
t.Errorf("Invoices.GetPDF returned error: %v", err)
|
||||
}
|
||||
|
||||
expected := []byte("some pdf content")
|
||||
if !bytes.Equal(invoicePDFResponse, expected) {
|
||||
t.Errorf("Invoices.GetPDF\n got=%#v\nwant=%#v", invoicePDFResponse, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvoices_GetCSV(t *testing.T) {
|
||||
setup()
|
||||
defer teardown()
|
||||
|
||||
mux.HandleFunc("/v2/customers/my/invoices/example-invoice-uuid/csv", func(w http.ResponseWriter, r *http.Request) {
|
||||
testMethod(t, r, http.MethodGet)
|
||||
fmt.Fprint(w, `some csv content`)
|
||||
})
|
||||
|
||||
invoiceCSVResponse, _, err := client.Invoices.GetCSV(ctx, "example-invoice-uuid")
|
||||
if err != nil {
|
||||
t.Errorf("Invoices.GetCSV returned error: %v", err)
|
||||
}
|
||||
|
||||
expected := []byte("some csv content")
|
||||
if !bytes.Equal(invoiceCSVResponse, expected) {
|
||||
t.Errorf("Invoices.GetCSV\n got=%#v\nwant=%#v", invoiceCSVResponse, expected)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue