-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathoptions.go
More file actions
100 lines (81 loc) · 2.72 KB
/
Copy pathoptions.go
File metadata and controls
100 lines (81 loc) · 2.72 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// @oagen-ignore-file
// Package workos provides a Go client for the WorkOS API.
package workos
import (
"net/http"
"time"
)
const (
defaultBaseURL = "https://api.workos.com"
defaultTimeout = 60 * time.Second
defaultMaxRetries = 3
)
// ClientOption configures the Client.
type ClientOption func(*Client)
// WithBaseURL sets a custom base URL.
func WithBaseURL(url string) ClientOption {
return func(c *Client) { c.baseURL = url }
}
// WithHTTPClient sets a custom HTTP client.
func WithHTTPClient(client *http.Client) ClientOption {
return func(c *Client) { c.httpClient = client }
}
// WithMaxRetries sets the maximum number of retries.
func WithMaxRetries(n int) ClientOption {
return func(c *Client) { c.maxRetries = n }
}
// WithClientID sets the client ID (used for authentication flows).
func WithClientID(id string) ClientOption {
return func(c *Client) { c.clientID = id }
}
// Logger is a minimal logging interface for HTTP request tracing.
type Logger interface {
Printf(format string, args ...any)
}
// WithLogger sets a logger for HTTP request tracing (method, path, status, duration).
func WithLogger(l Logger) ClientOption {
return func(c *Client) { c.logger = l }
}
// WithAppInfo sets application info that is appended to the User-Agent header.
// This is useful for framework vendors and integration libraries to identify
// themselves in API requests.
func WithAppInfo(name, version, url string) ClientOption {
return func(c *Client) {
c.appInfo = appInfo{Name: name, Version: version, URL: url}
}
}
// RequestOption configures a single API request.
type RequestOption func(*requestConfig)
type requestConfig struct {
extraHeaders http.Header
timeout time.Duration
maxRetries *int
baseURL string
idempotencyKey string
}
// WithExtraHeaders adds extra headers to the request.
func WithExtraHeaders(h http.Header) RequestOption {
return func(r *requestConfig) { r.extraHeaders = h }
}
// WithTimeout sets a timeout for the request.
func WithTimeout(d time.Duration) RequestOption {
return func(r *requestConfig) { r.timeout = d }
}
// WithIdempotencyKey sets an idempotency key for the request.
func WithIdempotencyKey(key string) RequestOption {
return func(r *requestConfig) { r.idempotencyKey = key }
}
// WithRequestMaxRetries overrides the max retries for a single request.
func WithRequestMaxRetries(n int) RequestOption {
return func(r *requestConfig) { r.maxRetries = &n }
}
// WithRequestBaseURL overrides the base URL for a single request.
func WithRequestBaseURL(url string) RequestOption {
return func(r *requestConfig) { r.baseURL = url }
}
// appInfo identifies an application or framework for the User-Agent header.
type appInfo struct {
Name string
Version string
URL string
}