forked from transip/gotransip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
71 lines (58 loc) · 2.5 KB
/
repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package invoice
import (
"fmt"
"github.com/assi010/gotransip/v6/repository"
"github.com/assi010/gotransip/v6/rest"
"net/url"
)
// Repository can be used to get a list of your invoices, invoice subitems (a specific product)
// or an invoice as pdf
type Repository repository.RestRepository
// GetAll returns a list of all invoices attached to your TransIP account
func (r *Repository) GetAll() ([]Invoice, error) {
var response invoicesResponse
restRequest := rest.Request{Endpoint: "/invoices"}
err := r.Client.Get(restRequest, &response)
return response.Invoices, err
}
// GetSelection returns a limited list of invoices,
// specify how many and which page/chunk of invoices you want to retrieve
func (r *Repository) GetSelection(page int, itemsPerPage int) ([]Invoice, error) {
var response invoicesResponse
params := url.Values{
"pageSize": []string{fmt.Sprintf("%d", itemsPerPage)},
"page": []string{fmt.Sprintf("%d", page)},
}
restRequest := rest.Request{Endpoint: "/invoices", Parameters: params}
err := r.Client.Get(restRequest, &response)
return response.Invoices, err
}
// GetByInvoiceNumber returns an Invoice object for the given invoice number.
//
// invoiceNumber corresponds to the InvoiceNumber property on a Invoice struct
func (r *Repository) GetByInvoiceNumber(invoiceNumber string) (Invoice, error) {
var response invoiceResponse
restRequest := rest.Request{Endpoint: fmt.Sprintf("/invoices/%s", invoiceNumber)}
err := r.Client.Get(restRequest, &response)
return response.Invoice, err
}
// GetInvoiceItems returns a list of InvoiceItems,
// detailing what specific products or services are on this invoice.
//
// invoiceNumber corresponds to the InvoiceNumber property on a Invoice struct.
func (r *Repository) GetInvoiceItems(invoiceNumber string) ([]Item, error) {
var response invoiceItemsResponse
restRequest := rest.Request{Endpoint: fmt.Sprintf("/invoices/%s/invoice-items", invoiceNumber)}
err := r.Client.Get(restRequest, &response)
return response.InvoiceItems, err
}
// GetInvoicePdf returns a pdf struct containing the contents of the pdf encoded in base64
// there are specific Pdf struct functions that help you decode the contents of the pdf to a file.
//
// invoiceNumber corresponds to the InvoiceNumber property on a Invoice struct.
func (r *Repository) GetInvoicePdf(invoiceNumber string) (Pdf, error) {
var response Pdf
restRequest := rest.Request{Endpoint: fmt.Sprintf("/invoices/%s/pdf", invoiceNumber)}
err := r.Client.Get(restRequest, &response)
return response, err
}