Repository navigation
Expand file tree
/
Copy pathactivity_endpoint.go
More file actions
64 lines (58 loc) · 1.92 KB
/
Copy pathactivity_endpoint.go
File metadata and controls
64 lines (58 loc) · 1.92 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
package openrouter
import (
"context"
"fmt"
"net/url"
)
// ActivityOptions contains optional parameters for retrieving activity data.
type ActivityOptions struct {
// Date filters by a single UTC date in the last 30 days (YYYY-MM-DD format).
// Example: "2024-01-15"
// Note: The API returns dates with timestamps (e.g., "2024-01-15 00:00:00") but expects
// the filter parameter in YYYY-MM-DD format.
Date string
}
// GetActivity retrieves daily user activity data grouped by model endpoint for the last 30 (completed) UTC days.
//
// If ingesting on a schedule, it is recommended to wait for ~30 minutes after the UTC boundary to
// request the previous day, because events are aggregated by request start time, and some reasoning
// models may take a few minutes to complete.
//
// Note: A provisioning key is required to access this endpoint, to ensure that your historic usage
// is not accessible to just anyone in your org with an inference API key.
//
// Example:
//
// ctx := context.Background()
// // Get all activity
// activity, err := client.GetActivity(ctx, nil)
// if err != nil {
// log.Fatal(err)
// }
//
// // Filter by date
// activity, err := client.GetActivity(ctx, &openrouter.ActivityOptions{
// Date: "2024-01-15",
// })
// if err != nil {
// log.Fatal(err)
// }
//
// for _, data := range activity.Data {
// fmt.Printf("%s - %s: %.2f requests, $%.4f usage\n",
// data.Date, data.Model, data.Requests, data.Usage)
// }
func (c *Client) GetActivity(ctx context.Context, opts *ActivityOptions) (*ActivityResponse, error) {
endpoint := "/activity"
// Add query parameters if options are provided
if opts != nil && opts.Date != "" {
params := url.Values{}
params.Add("date", opts.Date)
endpoint = fmt.Sprintf("%s?%s", endpoint, params.Encode())
}
var response ActivityResponse
if err := c.doRequest(ctx, "GET", endpoint, nil, &response); err != nil {
return nil, err
}
return &response, nil
}