From ca789800d2895b76b6108d0131dd129dbd116821 Mon Sep 17 00:00:00 2001 From: Ryan Butler <1170939+rbutler@users.noreply.github.com> Date: Fri, 28 Feb 2020 10:06:47 -0600 Subject: [PATCH] Add GetCSV and GetPDF methods to InvoiceService (#305) * Add GetCSV and GetPDF methods to InvoiceService Co-authored-by: Zach Gershman --- CHANGELOG.md | 1 + invoices.go | 39 +++++++++++++++++++++++++++++++++++++++ invoices_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa2df89..4b6a5c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/invoices.go b/invoices.go index 220eb8c..cc111f8 100644 --- a/invoices.go +++ b/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 +} diff --git a/invoices_test.go b/invoices_test.go index 5a8989d..a3d53d5 100644 --- a/invoices_test.go +++ b/invoices_test.go @@ -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) + } +}