forked from 1Password/onepassword-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_builder.go
86 lines (72 loc) · 2.38 KB
/
client_builder.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
package onepassword
import (
"context"
"fmt"
"runtime"
"github.com/1password/onepassword-sdk-go/internal"
)
const (
DefaultIntegrationName = "Unknown"
DefaultIntegrationVersion = "Unknown"
)
// NewClient returns a 1Password Go SDK client using the provided ClientOption list.
func NewClient(ctx context.Context, opts ...ClientOption) (*Client, error) {
core, err := internal.GetSharedCore()
if err != nil {
return nil, err
}
return createClient(ctx, core, opts...)
}
func createClient(ctx context.Context, core internal.Core, opts ...ClientOption) (*Client, error) {
client := Client{
config: internal.NewDefaultConfig(),
}
for _, opt := range opts {
err := opt(&client)
if err != nil {
return nil, err
}
}
clientID, err := core.InitClient(ctx, client.config)
if err != nil {
return nil, fmt.Errorf("error initializing client: %w", err)
}
inner := internal.InnerClient{
ID: *clientID,
Core: core,
}
initAPIs(&client, inner)
runtime.SetFinalizer(&client, func(f *Client) {
core.ReleaseClient(*clientID)
})
return &client, nil
}
type ClientOption func(client *Client) error
// WithServiceAccountToken specifies the [1Password Service Account](https://developer.1password.com/docs/service-accounts) token to use to authenticate the SDK client. Read more about how to get started with service accounts: https://developer.1password.com/docs/service-accounts/get-started/#create-a-service-account
func WithServiceAccountToken(token string) ClientOption {
return func(c *Client) error {
c.config.SAToken = token
return nil
}
}
// WithIntegrationInfo specifies the name and version of the integration built using the 1Password Go SDK. If you don't know which name and version to use, use `DefaultIntegrationName` and `DefaultIntegrationVersion`, respectively.
func WithIntegrationInfo(name string, version string) ClientOption {
return func(c *Client) error {
c.config.IntegrationName = name
c.config.IntegrationVersion = version
return nil
}
}
func clientInvoke(ctx context.Context, innerClient internal.InnerClient, invocation string, params map[string]interface{}) (*string, error) {
invocationResponse, err := innerClient.Core.Invoke(ctx, internal.InvokeConfig{
ClientID: innerClient.ID,
Invocation: internal.Invocation{
MethodName: invocation,
SerializedParams: params,
},
})
if err != nil {
return nil, err
}
return invocationResponse, nil
}