forked from getlago/lago-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.go
More file actions
89 lines (74 loc) · 2.9 KB
/
usage.go
File metadata and controls
89 lines (74 loc) · 2.9 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package lago
import (
"context"
"encoding/json"
)
type UsageRequest struct {
client *Client
}
type UsageCustomerType string
type UsageTimeGranularityType string
const (
UsageCompanyCustomerType UsageCustomerType = "company"
UsageIndividualCustomerType UsageCustomerType = "individual"
)
const (
UsageDailyTimeGranularity UsageTimeGranularityType = "daily"
UsageWeeklyTimeGranularity UsageTimeGranularityType = "weekly"
UsageMonthlyTimeGranularity UsageTimeGranularityType = "monthly"
)
type UsageListInput struct {
Currency string `json:"currency,omitempty"`
CustomerCountry string `json:"customer_country,omitempty,string"`
CustomerType UsageCustomerType `json:"customer_type,omitempty,string"`
FromDate string `json:"from_date,omitempty"`
ToDate string `json:"to_date,omitempty"`
IsBillableMetricRecurring bool `json:"is_billable_metric_recurring,omitempty"`
TimeGranularity UsageTimeGranularityType `json:"time_granularity,omitempty"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
ExternalSubscriptionID string `json:"external_subscription_id,omitempty"`
BillableMetricCode string `json:"billable_metric_code,omitempty"`
PlanCode string `json:"plan_code,omitempty"`
}
type UsageResult struct {
Usages []Usage `json:"usages,omitempty"`
}
type Usage struct {
OrganizationID string `json:"organization_id,omitempty"`
StartOfPeriodDt string `json:"start_of_period_dt,omitempty"`
EndOfPeriodDt string `json:"end_of_period_dt,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
BillableMetricCode string `json:"billable_metric_code,omitempty"`
Units string `json:"units,omitempty"`
IsBillableMetricDeleted bool `json:"is_billable_metric_deleted,omitempty"`
}
func (c *Client) Usage() *UsageRequest {
return &UsageRequest{
client: c,
}
}
func (adr *UsageRequest) GetList(ctx context.Context, UsageListInput *UsageListInput) (*UsageResult, *Error) {
jsonQueryparams, err := json.Marshal(UsageListInput)
if err != nil {
return nil, &Error{Err: err}
}
queryParams := make(map[string]string)
if err = json.Unmarshal(jsonQueryparams, &queryParams); err != nil {
return nil, &Error{Err: err}
}
clientRequest := &ClientRequest{
Path: "analytics/usage",
QueryParams: queryParams,
Result: &UsageResult{},
}
result, clientErr := adr.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
UsageResult, ok := result.(*UsageResult)
if !ok {
return nil, &ErrorTypeAssert
}
return UsageResult, nil
}