Skip to content

feat(invoices): add billing invoices in client handler #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/fixtures/invoices/get_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "9qzw9Hj4",
"number": 843071,
"parent_id": null,
"status": "paid",
"date": "2024-06-23",
"type": "invoice",
"total_due": 11.90,
"currency": "EUR",
"csv_url": "url",
"pdf_url": "url"
}
53 changes: 53 additions & 0 deletions pkg/invoices.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package serverscom

import (
"context"
"encoding/json"
)

const (
invoicesListPath = "/billing/invoices"

InvoicePath = "/billing/invoices/%s"
)

// BilligService is an interface for interfacing with Billing Invoices endpoints
// API documentation:
// https://developers.servers.com/api-documentation/v1/#tag/Invoice
type InvoiceService interface {
// Primary collection
Collection() Collection[InvoiceList]

// Generic operations
GetBillingInvoice(ctx context.Context, id string) (*Invoice, error)
}

// InvoiceHandler handles operations around hosts
type InvoiceHandler struct {
client *Client
}

// Collection builds a new Collection[InvoiceList] interface
func (h *InvoiceHandler) Collection() Collection[InvoiceList] {
return NewCollection[InvoiceList](h.client, invoicesListPath)
}

// GetBillingInvoice returns an invoice
// Endpoint: https://developers.servers.com/api-documentation/v1/#tag/Invoice/operation/GetAnInvoice
func (h *InvoiceHandler) GetBillingInvoice(ctx context.Context, id string) (*Invoice, error) {
url := h.client.buildURL(InvoicePath, []interface{}{id}...)

body, err := h.client.buildAndExecRequest(ctx, "GET", url, nil)

if err != nil {
return nil, err
}

invoice := new(Invoice)

if err := json.Unmarshal(body, &invoice); err != nil {
return nil, err
}

return invoice, nil
}
63 changes: 63 additions & 0 deletions pkg/invoices_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package serverscom

import (
"context"
. "github.com/onsi/gomega"
"testing"
)

func TestInvoicesCollection(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/billing/invoices").
WithRequestMethod("GET").
WithResponseBodyStubInline(`[]`).
WithResponseCode(200).
Build()

defer ts.Close()

collection := client.Invoices.Collection()

ctx := context.TODO()

list, err := collection.List(ctx)

g.Expect(err).To(BeNil())
g.Expect(list).To(BeEmpty())
g.Expect(collection.HasNextPage()).To(Equal(false))
g.Expect(collection.HasPreviousPage()).To(Equal(false))
g.Expect(collection.HasFirstPage()).To(Equal(false))
g.Expect(collection.HasLastPage()).To(Equal(false))
}

func TestGetInvoice(t *testing.T) {
g := NewGomegaWithT(t)

ts, client := newFakeServer().
WithRequestPath("/billing/invoices/9qzw9Hj4").
WithRequestMethod("GET").
WithResponseBodyStubFile("fixtures/invoices/get_response.json").
WithResponseCode(200).
Build()

defer ts.Close()

ctx := context.TODO()

invoice, err := client.Invoices.GetBillingInvoice(ctx, "9qzw9Hj4")

g.Expect(err).To(BeNil())
g.Expect(invoice).ToNot(BeNil())
g.Expect(invoice.ID).To(Equal("9qzw9Hj4"))
g.Expect(invoice.Number).To(Equal(int64(843071)))
g.Expect(invoice.ParentID).To(BeNil())
g.Expect(invoice.Status).To(Equal("paid"))
g.Expect(invoice.Date).To(Equal("2024-06-23"))
g.Expect(invoice.Type).To(Equal("invoice"))
g.Expect(invoice.TotalDue).To(Equal(11.90))
g.Expect(invoice.Currency).To(Equal("EUR"))
g.Expect(invoice.CsvUrl).To(Equal("url"))
g.Expect(invoice.PdfUrl).To(Equal("url"))
}
3 changes: 3 additions & 0 deletions pkg/serverscom.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Client struct {

KubernetesClusters KubernetesClustersService

Invoices InvoiceService

client *resty.Client
}

Expand Down Expand Up @@ -95,6 +97,7 @@ func (cli *Client) configureResources() {
cli.CloudBlockStorageBackups = &CloudBlockStorageBackupsHandler{cli}
cli.CloudBlockStorageVolumes = &CloudBlockStorageVolumesHandler{cli}
cli.KubernetesClusters = &KubernetesClustersHandler{cli}
cli.Invoices = &InvoiceHandler{cli}
}

func (cli *Client) buildURL(path string, values ...interface{}) string {
Expand Down
27 changes: 26 additions & 1 deletion pkg/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,6 @@ type KubernetesCluster struct {
}

// KubernetesClusterNode represents Kubernetes cluster node

type KubernetesClusterNode struct {
ID string `json:"id"`
Number int64 `json:"number"`
Expand All @@ -998,3 +997,29 @@ type KubernetesClusterNode struct {
type KubernetesClusterUpdateInput struct {
Labels map[string]string `json:"labels,omitempty"`
}

// InvoiceList represents invoices list
type InvoiceList struct {
ID string `json:"id"`
Number int64 `json:"number"`
ParentID *string `json:"parent_id"`
Status string `json:"status"`
Date string `json:"date"`
Type string `json:"type"`
TotalDue float64 `json:"total_due"`
Currency string `json:"currency"`
}

// Invoice represents an invoice
type Invoice struct {
ID string `json:"id"`
Number int64 `json:"number"`
ParentID *string `json:"parent_id"`
Status string `json:"status"`
Date string `json:"date"`
Type string `json:"type"`
TotalDue float64 `json:"total_due"`
Currency string `json:"currency"`
CsvUrl string `json:"csv_url"`
PdfUrl string `json:"pdf_url"`
}
Loading