From 73b06d5de42bd46d3615ef1029c8b66578adacb6 Mon Sep 17 00:00:00 2001 From: Alexey Chelnakov Date: Fri, 28 Jun 2024 18:04:49 +0300 Subject: [PATCH 1/4] Add websocket options --- v1/client.go | 22 +++++++++++++++++++++- v1/client_test.go | 18 ++++++++++++++++++ v1/types.go | 7 +++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/v1/client.go b/v1/client.go index afb1860..efc9e65 100644 --- a/v1/client.go +++ b/v1/client.go @@ -912,8 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe return resp, status, err } +type wsOptions struct { + params []string +} + +type WsOption interface { + apply(*wsOptions) +} + +func (c WsOptionParam) apply(opts *wsOptions) { + opts.params = append(opts.params, string(c)) +} + // WsMeta let you receive url & headers to open web socket connection -func (c *MgClient) WsMeta(events []string) (string, http.Header, error) { +func (c *MgClient) WsMeta(events []string, opts ...WsOption) (string, http.Header, error) { var url string if len(events) < 1 { @@ -923,6 +935,14 @@ func (c *MgClient) WsMeta(events []string) (string, http.Header, error) { url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) + var wsOpts wsOptions + for _, opt := range opts { + opt.apply(&wsOpts) + } + if len(wsOpts.params) > 0 { + url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.params, ",")) + } + if url == "" { err := errors.New("empty WS URL") return url, nil, err diff --git a/v1/client_test.go b/v1/client_test.go index 5965543..718b538 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -846,6 +846,24 @@ func TestMgClient_CommandEditDelete(t *testing.T) { t.Logf("%v", d) } +func TestMgClient_WsMeta_With_Options(t *testing.T) { + c := client() + events := []string{"user_updated", "user_join_chat"} + options := []WsOption{WsOptionIncludeMassCommunication} + + url, headers, err := c.WsMeta(events, options...) + + if err != nil { + t.Errorf("%v", err) + } + + resUrl := "wss://api.example.com/api/bot/v1/ws?events=user_updated,user_join_chat&options=include_mass_communication" + resToken := c.Token + + assert.Equal(t, resUrl, url) + assert.Equal(t, resToken, headers["X-Bot-Token"][0]) +} + func TestMgClient_WsMeta(t *testing.T) { c := client() events := []string{"user_updated", "user_join_chat"} diff --git a/v1/types.go b/v1/types.go index 2376efc..4261892 100644 --- a/v1/types.go +++ b/v1/types.go @@ -43,6 +43,8 @@ const ( WsEventSettingsUpdated string = "settings_updated" WsEventChatsDeleted string = "chats_deleted" + WsOptionIncludeMassCommunication WsOptionParam = "include_mass_communication" + ChannelFeatureNone string = "none" ChannelFeatureReceive string = "receive" ChannelFeatureSend string = "send" @@ -434,6 +436,11 @@ type ( } ) +// WS options +type ( + WsOptionParam string +) + // Single entity types type ( Message struct { From de3eca3a55938106f2da5bb51514554e2b4039f9 Mon Sep 17 00:00:00 2001 From: Alexey Chelnakov Date: Thu, 11 Jul 2024 13:49:29 +0300 Subject: [PATCH 2/4] fix --- v1/client.go | 20 ++++++++++---------- v1/client_test.go | 10 +++++----- v1/types.go | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/v1/client.go b/v1/client.go index efc9e65..fc66d00 100644 --- a/v1/client.go +++ b/v1/client.go @@ -912,20 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe return resp, status, err } -type wsOptions struct { - params []string +type wsParams struct { + options []string } -type WsOption interface { - apply(*wsOptions) +type WsParams interface { + apply(*wsParams) } -func (c WsOptionParam) apply(opts *wsOptions) { - opts.params = append(opts.params, string(c)) +func (c WsOption) apply(opts *wsParams) { + opts.options = append(opts.options, string(c)) } // WsMeta let you receive url & headers to open web socket connection -func (c *MgClient) WsMeta(events []string, opts ...WsOption) (string, http.Header, error) { +func (c *MgClient) WsMeta(events []string, opts ...WsParams) (string, http.Header, error) { var url string if len(events) < 1 { @@ -935,12 +935,12 @@ func (c *MgClient) WsMeta(events []string, opts ...WsOption) (string, http.Heade url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) - var wsOpts wsOptions + var wsOpts wsParams for _, opt := range opts { opt.apply(&wsOpts) } - if len(wsOpts.params) > 0 { - url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.params, ",")) + if len(wsOpts.options) > 0 { + url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.options, ",")) } if url == "" { diff --git a/v1/client_test.go b/v1/client_test.go index 718b538..d3ba722 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -849,7 +849,7 @@ func TestMgClient_CommandEditDelete(t *testing.T) { func TestMgClient_WsMeta_With_Options(t *testing.T) { c := client() events := []string{"user_updated", "user_join_chat"} - options := []WsOption{WsOptionIncludeMassCommunication} + options := []WsParams{WsOptionIncludeMassCommunication} url, headers, err := c.WsMeta(events, options...) @@ -857,10 +857,10 @@ func TestMgClient_WsMeta_With_Options(t *testing.T) { t.Errorf("%v", err) } - resUrl := "wss://api.example.com/api/bot/v1/ws?events=user_updated,user_join_chat&options=include_mass_communication" + resURL := "wss://api.example.com/api/bot/v1/ws?events=user_updated,user_join_chat&options=include_mass_communication" resToken := c.Token - assert.Equal(t, resUrl, url) + assert.Equal(t, resURL, url) assert.Equal(t, resToken, headers["X-Bot-Token"][0]) } @@ -873,10 +873,10 @@ func TestMgClient_WsMeta(t *testing.T) { t.Errorf("%v", err) } - resUrl := fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) + resURL := fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) resToken := c.Token - assert.Equal(t, resUrl, url) + assert.Equal(t, resURL, url) assert.Equal(t, resToken, headers["X-Bot-Token"][0]) } diff --git a/v1/types.go b/v1/types.go index 4261892..cf1f146 100644 --- a/v1/types.go +++ b/v1/types.go @@ -43,7 +43,7 @@ const ( WsEventSettingsUpdated string = "settings_updated" WsEventChatsDeleted string = "chats_deleted" - WsOptionIncludeMassCommunication WsOptionParam = "include_mass_communication" + WsOptionIncludeMassCommunication WsOption = "include_mass_communication" ChannelFeatureNone string = "none" ChannelFeatureReceive string = "receive" @@ -438,7 +438,7 @@ type ( // WS options type ( - WsOptionParam string + WsOption string ) // Single entity types From 3e9078ee86548d3182400a2e7c2423e56a4462dc Mon Sep 17 00:00:00 2001 From: Alexey Chelnakov Date: Thu, 11 Jul 2024 14:02:42 +0300 Subject: [PATCH 3/4] fix --- v1/client.go | 12 ++++++------ v1/client_test.go | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/v1/client.go b/v1/client.go index fc66d00..8a6f31d 100644 --- a/v1/client.go +++ b/v1/client.go @@ -925,7 +925,7 @@ func (c WsOption) apply(opts *wsParams) { } // WsMeta let you receive url & headers to open web socket connection -func (c *MgClient) WsMeta(events []string, opts ...WsParams) (string, http.Header, error) { +func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http.Header, error) { var url string if len(events) < 1 { @@ -935,12 +935,12 @@ func (c *MgClient) WsMeta(events []string, opts ...WsParams) (string, http.Heade url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) - var wsOpts wsParams - for _, opt := range opts { - opt.apply(&wsOpts) + var params wsParams + for _, opt := range urlParams { + opt.apply(¶ms) } - if len(wsOpts.options) > 0 { - url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.options, ",")) + if len(params.options) > 0 { + url = fmt.Sprintf("%s&options=%s", url, strings.Join(params.options, ",")) } if url == "" { diff --git a/v1/client_test.go b/v1/client_test.go index d3ba722..039f060 100644 --- a/v1/client_test.go +++ b/v1/client_test.go @@ -849,9 +849,9 @@ func TestMgClient_CommandEditDelete(t *testing.T) { func TestMgClient_WsMeta_With_Options(t *testing.T) { c := client() events := []string{"user_updated", "user_join_chat"} - options := []WsParams{WsOptionIncludeMassCommunication} + params := []WsParams{WsOptionIncludeMassCommunication} - url, headers, err := c.WsMeta(events, options...) + url, headers, err := c.WsMeta(events, params...) if err != nil { t.Errorf("%v", err) From e0900803251726f74732e5ee0862206bfa7d0464 Mon Sep 17 00:00:00 2001 From: Alexey Chelnakov Date: Thu, 11 Jul 2024 14:04:49 +0300 Subject: [PATCH 4/4] fix --- v1/client.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/v1/client.go b/v1/client.go index 8a6f31d..c4a352e 100644 --- a/v1/client.go +++ b/v1/client.go @@ -936,8 +936,8 @@ func (c *MgClient) WsMeta(events []string, urlParams ...WsParams) (string, http. url = fmt.Sprintf("%s%s%s%s", strings.Replace(c.URL, "https", "wss", 1), prefix, "/ws?events=", strings.Join(events[:], ",")) var params wsParams - for _, opt := range urlParams { - opt.apply(¶ms) + for _, param := range urlParams { + param.apply(¶ms) } if len(params.options) > 0 { url = fmt.Sprintf("%s&options=%s", url, strings.Join(params.options, ","))