-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
option.go
124 lines (111 loc) · 3.26 KB
/
option.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package fcm
import (
"fmt"
"net/http"
"net/url"
"os"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)
// Option configurates Client with defined option.
type Option func(*Client) error
// WithHTTPClient returns Option to configure HTTP Client.
func WithHTTPClient(httpClient *http.Client) Option {
return func(c *Client) error {
c.httpClient = httpClient
return nil
}
}
// WithHTTPProxy returns Option to configure HTTP Client proxy.
func WithHTTPProxy(proxyURL string) Option {
return func(c *Client) error {
proxy, err := url.Parse(proxyURL)
if err != nil {
return err
}
httpClient := &http.Client{
Transport: &http.Transport{Proxy: http.ProxyURL(proxy)},
}
c.httpClient = httpClient
return nil
}
}
// WithCredentialsFile returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials file.
func WithCredentialsFile(filename string) Option {
return func(c *Client) error {
data, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("cannot read credentials file: %v", err)
}
c.credentialsJSON = data
c.options = append(c.options, option.WithCredentialsJSON(data))
return nil
}
}
// WithCredentialsJSON returns a ClientOption that authenticates
// API calls with the given service account or refresh token JSON
// credentials.
func WithCredentialsJSON(data []byte) Option {
return func(c *Client) error {
c.credentialsJSON = data
c.options = append(c.options, option.WithCredentialsJSON(data))
return nil
}
}
// WithEndpoint returns Option to configure endpoint.
func WithEndpoint(endpoint string) Option {
return func(c *Client) error {
c.options = append(c.options, option.WithEndpoint(endpoint))
return nil
}
}
// WithServiceAccount returns Option to configure service account.
func WithServiceAccount(serviceAccount string) Option {
return func(c *Client) error {
c.serviceAcount = serviceAccount
return nil
}
}
// WithProjectID returns Option to configure project ID.
func WithProjectID(projectID string) Option {
return func(c *Client) error {
c.projectID = projectID
return nil
}
}
// WithTokenSource returns a ClientOption that specifies an OAuth2 token
// source to be used as the basis for authentication.
func WithTokenSource(s oauth2.TokenSource) Option {
return func(c *Client) error {
c.options = append(c.options, option.WithTokenSource(s))
return nil
}
}
// WithDebug returns Option to configure debug mode.
func WithDebug(debug bool) Option {
return func(c *Client) error {
c.debug = debug
return nil
}
}
// WithCustomClientOption is an option function that allows you to provide custom client options.
// It appends the provided custom options to the client's options list.
// The custom options are applied when sending requests to the FCM server.
// If no custom options are provided, this function does nothing.
//
// Parameters:
// - opts: The custom client options to be appended to the client's options list.
//
// Returns:
// - An error if there was an issue appending the custom options to the client's options list, or nil otherwise.
func WithCustomClientOption(opts ...option.ClientOption) Option {
return func(c *Client) error {
if len(opts) == 0 {
return nil
}
c.options = append(c.options, opts...)
return nil
}
}