forked from jpoehls/go-conduit
-
Notifications
You must be signed in to change notification settings - Fork 17
/
dialer.go
82 lines (67 loc) · 1.78 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
package gonduit
import (
"github.com/etcinit/gonduit/core"
"github.com/etcinit/gonduit/responses"
"github.com/etcinit/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) {
var d Dialer
d.ClientName = "gonduit"
d.ClientVersion = "1"
return d.Dial(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) {
var res responses.ConduitCapabilitiesResponse
// We use conduit.connect for authentication and it establishes a session.
err := core.PerformCall(
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
}