-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
179 lines (153 loc) · 5.6 KB
/
client.go
File metadata and controls
179 lines (153 loc) · 5.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Package salesforce provides a comprehensive Go SDK for Salesforce.
package salesforce
import (
"context"
"errors"
"fmt"
"net/http"
"github.com/PramithaMJ/salesforce/v2/analytics"
"github.com/PramithaMJ/salesforce/v2/apex"
"github.com/PramithaMJ/salesforce/v2/auth"
"github.com/PramithaMJ/salesforce/v2/bulk"
"github.com/PramithaMJ/salesforce/v2/composite"
"github.com/PramithaMJ/salesforce/v2/connect"
sfhttp "github.com/PramithaMJ/salesforce/v2/http"
"github.com/PramithaMJ/salesforce/v2/limits"
"github.com/PramithaMJ/salesforce/v2/query"
"github.com/PramithaMJ/salesforce/v2/search"
"github.com/PramithaMJ/salesforce/v2/sobjects"
"github.com/PramithaMJ/salesforce/v2/tooling"
"github.com/PramithaMJ/salesforce/v2/types"
"github.com/PramithaMJ/salesforce/v2/uiapi"
)
// Client is the main Salesforce API client.
type Client struct {
config *Config
httpClient *sfhttp.Client
auth auth.Authenticator
// Services
sobjects *sobjects.Service
query *query.Service
bulk *bulk.Service
composite *composite.Service
analytics *analytics.Service
tooling *tooling.Service
connect *connect.Service
limits *limits.Service
uiapi *uiapi.Service
search *search.Service
apex *apex.Service
}
// NewClient creates a new Salesforce client with the given options.
func NewClient(opts ...Option) (*Client, error) {
cfg := &Config{
APIVersion: types.DefaultAPIVersion,
Timeout: types.DefaultTimeout,
MaxRetries: types.DefaultMaxRetries,
TokenURL: "https://login.salesforce.com/services/oauth2/token",
}
for _, opt := range opts {
if err := opt(cfg); err != nil {
return nil, err
}
}
if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("invalid config: %w", err)
}
httpClient := cfg.HTTPClient
if httpClient == nil {
httpClient = &http.Client{Timeout: cfg.Timeout}
}
client := &Client{config: cfg}
// Create authenticator
switch {
case cfg.RefreshToken != "":
client.auth = auth.NewRefreshTokenAuthenticator(
cfg.ClientID, cfg.ClientSecret, cfg.RefreshToken, cfg.TokenURL)
case cfg.Username != "" && cfg.Password != "":
client.auth = auth.NewPasswordAuthenticator(
cfg.ClientID, cfg.ClientSecret, cfg.Username,
cfg.Password, cfg.SecurityToken, cfg.TokenURL)
case cfg.AccessToken != "":
client.auth = auth.NewTokenAuthenticator(cfg.AccessToken, cfg.InstanceURL)
default:
return nil, errors.New("no authentication method configured")
}
// Create HTTP client
client.httpClient = sfhttp.NewClient(sfhttp.Config{
HTTPClient: httpClient,
APIVersion: cfg.APIVersion,
Logger: cfg.Logger,
MaxRetries: cfg.MaxRetries,
})
return client, nil
}
// Connect authenticates and establishes connection to Salesforce.
func (c *Client) Connect(ctx context.Context) error {
token, err := c.auth.Authenticate(ctx)
if err != nil {
return fmt.Errorf("authentication failed: %w", err)
}
c.httpClient.SetBaseURL(token.InstanceURL)
c.httpClient.SetAccessToken(token.AccessToken)
c.initServices()
return nil
}
// SetAccessToken sets the access token directly.
func (c *Client) SetAccessToken(token, instanceURL string) {
c.httpClient.SetBaseURL(instanceURL)
c.httpClient.SetAccessToken(token)
c.initServices()
}
func (c *Client) initServices() {
apiVersion := c.config.APIVersion
c.sobjects = sobjects.NewService(c.httpClient, apiVersion)
c.query = query.NewService(c.httpClient, apiVersion)
c.bulk = bulk.NewService(c.httpClient, apiVersion)
c.composite = composite.NewService(c.httpClient, apiVersion)
c.analytics = analytics.NewService(c.httpClient, apiVersion)
c.tooling = tooling.NewService(c.httpClient, apiVersion)
c.connect = connect.NewService(c.httpClient, apiVersion)
c.limits = limits.NewService(c.httpClient, apiVersion)
c.uiapi = uiapi.NewService(c.httpClient, apiVersion)
c.search = search.NewService(c.httpClient, apiVersion)
c.apex = apex.NewService(c.httpClient)
}
// Services access methods
// SObjects returns the SObject service.
func (c *Client) SObjects() *sobjects.Service { return c.sobjects }
// Query returns the Query service.
func (c *Client) Query() *query.Service { return c.query }
// Bulk returns the Bulk API 2.0 service.
func (c *Client) Bulk() *bulk.Service { return c.bulk }
// Composite returns the Composite API service.
func (c *Client) Composite() *composite.Service { return c.composite }
// Analytics returns the Analytics/Reports service.
func (c *Client) Analytics() *analytics.Service { return c.analytics }
// Tooling returns the Tooling API service.
func (c *Client) Tooling() *tooling.Service { return c.tooling }
// Chatter returns the Connect/Chatter service.
func (c *Client) Chatter() *connect.Service { return c.connect }
// Limits returns the Limits service.
func (c *Client) Limits() *limits.Service { return c.limits }
// UIAPI returns the User Interface API service.
func (c *Client) UIAPI() *uiapi.Service { return c.uiapi }
// Search returns the SOSL Search service.
func (c *Client) Search() *search.Service { return c.search }
// Apex returns the Apex REST service.
func (c *Client) Apex() *apex.Service { return c.apex }
// GetToken returns the current access token.
func (c *Client) GetToken() *types.Token { return c.auth.GetToken() }
// APIVersion returns the configured API version.
func (c *Client) APIVersion() string { return c.config.APIVersion }
// InstanceURL returns the Salesforce instance URL.
func (c *Client) InstanceURL() string { return c.httpClient.BaseURL() }
// RefreshToken refreshes the access token.
func (c *Client) RefreshToken(ctx context.Context) error {
token, err := c.auth.Refresh(ctx)
if err != nil {
return err
}
c.httpClient.SetAccessToken(token.AccessToken)
return nil
}