Skip to content

Commit

Permalink
Add usage API support (#14)
Browse files Browse the repository at this point in the history
Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
  • Loading branch information
milosgajdos authored Jun 22, 2024
1 parent b03a00c commit 530305e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
18 changes: 18 additions & 0 deletions examples/usage/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"context"
"log"

"github.com/milosgajdos/go-vocode"
)

func main() {
client := vocode.NewClient()

usage, err := client.GetUsage(context.Background())
if err != nil {
log.Fatalf("failed getting usage: %v", err)
}
log.Printf("got usage: %+v", usage)
}
55 changes: 55 additions & 0 deletions usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 530305e

Please sign in to comment.