@@ -12,6 +12,7 @@ import (
1212 "net/url"
1313 "strings"
1414
15+ "github.com/gorilla/websocket"
1516 "github.com/urfave/cli/v2"
1617)
1718
@@ -122,6 +123,32 @@ func (c *cmdBase) doHTTPRequest(ctx context.Context, p doHTTPRequestParams) erro
122123 return p .RespProcessor (resp )
123124}
124125
126+ func (c * cmdBase ) dialWebSocket (ctx context.Context , path string ) (* websocket.Conn , error ) {
127+ url , err := url .Parse (c .apiHost + "/v3" + path )
128+ if err != nil {
129+ return nil , fmt .Errorf ("parse url: %w" , err )
130+ }
131+ if url .Scheme == "https" {
132+ url .Scheme = "wss"
133+ } else {
134+ url .Scheme = "ws"
135+ }
136+
137+ headers := make (http.Header )
138+ headers .Add ("X-Enapter-Auth-Token" , c .token )
139+
140+ //nolint:bodyclose // body should be closed by callers
141+ conn , resp , err := websocket .DefaultDialer .DialContext (ctx , url .String (), headers )
142+ if err != nil {
143+ return nil , fmt .Errorf ("dial: %w" , err )
144+ }
145+ if resp .StatusCode != http .StatusSwitchingProtocols {
146+ return nil , cli .Exit (parseRespErrorMessage (resp ), 1 )
147+ }
148+
149+ return conn , nil
150+ }
151+
125152func (c * cmdBase ) defaultRespProcessor (resp * http.Response ) error {
126153 if resp .StatusCode != http .StatusOK {
127154 return cli .Exit (parseRespErrorMessage (resp ), 1 )
0 commit comments