-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathuser.go
More file actions
67 lines (59 loc) · 2.17 KB
/
Copy pathuser.go
File metadata and controls
67 lines (59 loc) · 2.17 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
package apify
import (
"context"
"encoding/json"
"errors"
"net/http"
)
// UserClient is a client for accessing user data (/v2/users/{userId} or /v2/users/me).
//
// For the current user ("me"), it also exposes account usage and limits. Those endpoints
// only exist for "me" and return an error if called on another user's client.
type UserClient struct {
ctx *resourceContext
isMe bool
}
func newUserClient(hc *httpClient, baseURL, id string) *UserClient {
return &UserClient{
ctx: newSingleContext(hc, baseURL, "users", id),
isMe: id == meUserPlaceholder,
}
}
// errNotMe is returned by the me-only methods when called on another user's client.
var errNotMe = errors.New("this operation is only available for the current user (use Me())")
// Get fetches the user. For "me" it returns private account details; for other users it
// returns the public profile. The bool reports whether the user exists.
func (c *UserClient) Get(ctx context.Context) (User, bool, error) {
return getResource[User](ctx, c.ctx, "", NewQueryParams())
}
// MonthlyUsage fetches the current account's monthly usage for the current month. Only
// available for "me".
//
// It returns the raw JSON usage report from the API (a JSON object with the account's usage
// breakdown and totals for the period).
func (c *UserClient) MonthlyUsage(ctx context.Context) (json.RawMessage, error) {
if !c.isMe {
return nil, errNotMe
}
return getResourceRequired[json.RawMessage](ctx, c.ctx, "usage/monthly", NewQueryParams())
}
// Limits fetches the current account's resource limits. Only available for "me".
func (c *UserClient) Limits(ctx context.Context) (json.RawMessage, error) {
if !c.isMe {
return nil, errNotMe
}
return getResourceRequired[json.RawMessage](ctx, c.ctx, "limits", NewQueryParams())
}
// UpdateLimits updates the current account's resource limits. Only available for "me".
func (c *UserClient) UpdateLimits(ctx context.Context, newLimits any) error {
if !c.isMe {
return errNotMe
}
data, err := json.Marshal(newLimits)
if err != nil {
return err
}
url := c.ctx.subURL("limits")
_, err = c.ctx.http.call(ctx, http.MethodPut, url, data, contentTypeJSON, defaultRequestTimeout)
return err
}