generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.go
55 lines (44 loc) · 1.17 KB
/
usage.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
package vocode
import (
"context"
"encoding/json"
"net/http"
"net/url"
"github.com/milosgajdos/go-vocode/request"
)
type PlanType string
const (
PlanFree PlanType = "plan_free"
PlanDeveloper PlanType = "plan_developer"
PlanEnterprise PlanType = "plan_enterprise"
PlanUnlimited PlanType = "plan_unlimited"
)
type Usage struct {
UserID string `json:"user_id"`
PlanType PlanType `json:"plan_type"`
MonthlyMinutes int `json:"monthly_usage_minutes"`
MonthlyLimitMinutes int `json:"monthly_usage_limit_minutes"`
}
func (c *Client) GetUsage(ctx context.Context) (*Usage, error) {
u, err := url.Parse(c.opts.BaseURL + "/" + c.opts.Version + "/usage")
if err != nil {
return nil, err
}
options := []request.HTTPOption{
request.WithBearer(c.opts.APIKey),
}
req, err := request.NewHTTP(ctx, http.MethodGet, u.String(), nil, options...)
if err != nil {
return nil, err
}
resp, err := request.Do[*APIError](c.opts.HTTPClient, req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
usage := new(Usage)
if err := json.NewDecoder(resp.Body).Decode(usage); err != nil {
return nil, err
}
return usage, nil
}