Add GetCSV and GetPDF methods to InvoiceService (#305)

* Add GetCSV and GetPDF methods to InvoiceService

Co-authored-by: Zach Gershman <zachgersh@users.noreply.github.com>
This commit is contained in:
Ryan Butler 2020-02-28 10:06:47 -06:00 committed by GitHub
parent 40dd0cc93a
commit ca789800d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 0 deletions

View File

@ -2,6 +2,7 @@
## unreleased
- #305 invoices: GetPDF and GetCSV methods - @rbutler
- #301 invoices: Get, Summary, and List methods - @rbutler
## [v1.30.0] - 2020-02-03

View File

@ -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
}

View File

@ -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)
}
}