forked from etcinit/gonduit
-
Notifications
You must be signed in to change notification settings - Fork 22
/
dialer.go
103 lines (85 loc) · 2.4 KB
/
dialer.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
package gonduit
import (
"context"
"github.com/uber/gonduit/core"
"github.com/uber/gonduit/responses"
"github.com/uber/gonduit/util"
)
// A Dialer contains options for connecting to an address.
type Dialer struct {
ClientName string
ClientVersion string
ClientDescription string
}
// Dial connects to conduit and confirms the API capabilities for future calls.
func Dial(host string, options *core.ClientOptions) (*Conn, error) {
ctx := context.Background()
return DialContext(ctx, host, options)
}
// DialContext connects to conduit and confirms the API capabilities for future calls,
// passing the given context through.
func DialContext(ctx context.Context, host string, options *core.ClientOptions) (*Conn, error) {
var d Dialer
d.ClientName = "gonduit"
d.ClientVersion = "1"
return d.DialContext(ctx, host, options)
}
// Dial connects to conduit and confirms the API capabilities for future calls.
func (d *Dialer) Dial(
host string,
options *core.ClientOptions,
) (*Conn, error) {
ctx := context.Background()
return d.DialContext(ctx, host, options)
}
// DialContext connects to conduit and confirms the API capabilities for future calls,
// passing the given context through.
func (d *Dialer) DialContext(
ctx context.Context,
host string,
options *core.ClientOptions,
) (*Conn, error) {
var res responses.ConduitCapabilitiesResponse
// We use conduit.connect for authentication and it establishes a session.
err := core.PerformCallContext(
ctx,
core.GetEndpointURI(host, "conduit.getcapabilities"),
nil,
&res,
options,
)
if err != nil {
return nil, err
}
// Now, we need to assert that the conduit API supports this client.
assertSupportedCapabilities(res, options)
conn := Conn{
host: host,
capabilities: &res,
dialer: d,
options: options,
}
return &conn, nil
}
func assertSupportedCapabilities(
res responses.ConduitCapabilitiesResponse,
options *core.ClientOptions,
) error {
if options.APIToken != "" {
if !util.ContainsString(res.Authentication, "token") {
return core.ErrTokenAuthUnsupported
}
}
if options.Cert != "" {
if !util.ContainsString(res.Authentication, "session") {
return core.ErrSessionAuthUnsupported
}
}
if !util.ContainsString(res.Input, "urlencoded") {
return core.ErrURLEncodedInputUnsupported
}
if !util.ContainsString(res.Output, "json") {
return core.ErrJSONOutputUnsupported
}
return nil
}