-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
81 lines (67 loc) · 2.08 KB
/
client.go
File metadata and controls
81 lines (67 loc) · 2.08 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package gogeek
import (
"go.uber.org/ratelimit"
)
// AuthMode represents the authentication method for the client
type AuthMode int
const (
// AuthNone indicates no authentication
AuthNone AuthMode = iota
// AuthAPIKey indicates API key authentication using Bearer token
AuthAPIKey
// AuthCookie indicates cookie-based authentication
AuthCookie
)
// Client represents a GoGeek API client with configurable authentication
type Client struct {
limiter ratelimit.Limiter
authMode AuthMode
apiKey string
cookieString string
}
// Limiter returns the rate limiter for this client
func (c *Client) Limiter() ratelimit.Limiter {
return c.limiter
}
// AuthMode returns the authentication mode for this client
func (c *Client) AuthMode() AuthMode {
return c.authMode
}
// APIKey returns the API key for this client (only valid when AuthMode is AuthAPIKey)
func (c *Client) APIKey() string {
return c.apiKey
}
// CookieString returns the cookie string for this client (only valid when AuthMode is AuthCookie)
func (c *Client) CookieString() string {
return c.cookieString
}
// ClientOption is a functional option for configuring a Client
type ClientOption func(*Client)
// WithAPIKey configures the client to use API key authentication
// The API key will be sent as a Bearer token in the Authorization header
func WithAPIKey(key string) ClientOption {
return func(c *Client) {
c.authMode = AuthAPIKey
c.apiKey = key
}
}
// WithCookie configures the client to use cookie-based authentication
// The cookie string should be the raw Cookie header value (e.g., "session=abc; token=xyz")
func WithCookie(cookie string) ClientOption {
return func(c *Client) {
c.authMode = AuthCookie
c.cookieString = cookie
}
}
// NewClient creates a new GoGeek API client with optional configuration
// By default, the client uses no authentication and has a rate limit of 2 requests per second
func NewClient(opts ...ClientOption) *Client {
client := &Client{
limiter: ratelimit.New(2, ratelimit.WithoutSlack),
authMode: AuthNone,
}
for _, opt := range opts {
opt(client)
}
return client
}