This commit is contained in:
Alexey Chelnakov 2024-07-11 13:49:29 +03:00
parent 73b06d5de4
commit de3eca3a55
3 changed files with 17 additions and 17 deletions

View File

@ -912,20 +912,20 @@ func (c *MgClient) UploadFileByURL(request UploadFileByUrlRequest) (UploadFileRe
return resp, status, err return resp, status, err
} }
type wsOptions struct { type wsParams struct {
params []string options []string
} }
type WsOption interface { type WsParams interface {
apply(*wsOptions) apply(*wsParams)
} }
func (c WsOptionParam) apply(opts *wsOptions) { func (c WsOption) apply(opts *wsParams) {
opts.params = append(opts.params, string(c)) opts.options = append(opts.options, string(c))
} }
// WsMeta let you receive url & headers to open web socket connection // 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 var url string
if len(events) < 1 { 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[:], ",")) 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 { for _, opt := range opts {
opt.apply(&wsOpts) opt.apply(&wsOpts)
} }
if len(wsOpts.params) > 0 { if len(wsOpts.options) > 0 {
url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.params, ",")) url = fmt.Sprintf("%s&options=%s", url, strings.Join(wsOpts.options, ","))
} }
if url == "" { if url == "" {

View File

@ -849,7 +849,7 @@ func TestMgClient_CommandEditDelete(t *testing.T) {
func TestMgClient_WsMeta_With_Options(t *testing.T) { func TestMgClient_WsMeta_With_Options(t *testing.T) {
c := client() c := client()
events := []string{"user_updated", "user_join_chat"} events := []string{"user_updated", "user_join_chat"}
options := []WsOption{WsOptionIncludeMassCommunication} options := []WsParams{WsOptionIncludeMassCommunication}
url, headers, err := c.WsMeta(events, options...) url, headers, err := c.WsMeta(events, options...)
@ -857,10 +857,10 @@ func TestMgClient_WsMeta_With_Options(t *testing.T) {
t.Errorf("%v", err) 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 resToken := c.Token
assert.Equal(t, resUrl, url) assert.Equal(t, resURL, url)
assert.Equal(t, resToken, headers["X-Bot-Token"][0]) assert.Equal(t, resToken, headers["X-Bot-Token"][0])
} }
@ -873,10 +873,10 @@ func TestMgClient_WsMeta(t *testing.T) {
t.Errorf("%v", err) 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 resToken := c.Token
assert.Equal(t, resUrl, url) assert.Equal(t, resURL, url)
assert.Equal(t, resToken, headers["X-Bot-Token"][0]) assert.Equal(t, resToken, headers["X-Bot-Token"][0])
} }

View File

@ -43,7 +43,7 @@ const (
WsEventSettingsUpdated string = "settings_updated" WsEventSettingsUpdated string = "settings_updated"
WsEventChatsDeleted string = "chats_deleted" WsEventChatsDeleted string = "chats_deleted"
WsOptionIncludeMassCommunication WsOptionParam = "include_mass_communication" WsOptionIncludeMassCommunication WsOption = "include_mass_communication"
ChannelFeatureNone string = "none" ChannelFeatureNone string = "none"
ChannelFeatureReceive string = "receive" ChannelFeatureReceive string = "receive"
@ -438,7 +438,7 @@ type (
// WS options // WS options
type ( type (
WsOptionParam string WsOption string
) )
// Single entity types // Single entity types