generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Milos Gajdos <milosthegajdos@gmail.com>
- Loading branch information
1 parent
b03a00c
commit 530305e
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |