Skip to content

Commit

Permalink
fix(zulip): fix generate compat
Browse files Browse the repository at this point in the history
also updates config with descriptions etc.
  • Loading branch information
piksel committed Feb 3, 2021
1 parent 41c5999 commit e97873f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 28 deletions.
24 changes: 13 additions & 11 deletions pkg/services/zulip/zulip.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,34 @@ func (service *Service) Initialize(configURL *url.URL, logger *log.Logger) error
service.Logger.SetLogger(logger)
service.config = &Config{}

if err := service.config.SetURL(nil, configURL); err != nil {
if err := service.config.setURL(nil, configURL); err != nil {
return err
}

return nil
}

func (service *Service) doSend(config *Config, message string) error {
apiURL := service.getURL(config)
apiURL := service.getAPIURL(config)
payload := CreatePayload(config, message)
res, err := http.Post(apiURL, "application/x-www-form-urlencoded", strings.NewReader(payload.Encode()))

if res.StatusCode != http.StatusOK {
return fmt.Errorf("failed to send notification to service, response status code %s", res.Status)
if err == nil && res.StatusCode != http.StatusOK {
err = fmt.Errorf("response status code %s", res.Status)
}

return err
if err != nil {
return fmt.Errorf("failed to send zulip message: %s", err)
}

return nil
}

func (service *Service) getURL(config *Config) string {
url := url.URL{
func (service *Service) getAPIURL(config *Config) string {
return (&url.URL{
User: url.UserPassword(config.BotMail, config.BotKey),
Host: config.Host,
Path: config.Path,
Path: "api/v1/messages",
Scheme: "https",
}

return url.String()
}).String()
}
34 changes: 22 additions & 12 deletions pkg/services/zulip/zulip_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@ package zulip

import (
"errors"
"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
"net/url"
)

// Config for the zulip service
type Config struct {
BotMail string
BotKey string
Host string
Path string
Stream string `key:"stream"`
Topic string `key:"topic"`
standard.EnumlessConfig
BotMail string `desc:"Bot e-mail address"`
BotKey string `desc:"API Key"`
Host string `desc:"API server hostname"`
Stream string `key:"stream" description:"Target stream name"`
Topic string `key:"topic,title" default:""`
}

// GetURL returns a URL representation of it's current field values
func (config *Config) GetURL(_ types.ConfigQueryResolver) *url.URL {
func (config *Config) GetURL() *url.URL {
resolver := format.NewPropKeyResolver(config)
return config.getURL(&resolver)
}

// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) SetURL(url *url.URL) error {
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}

func (config *Config) getURL(_ types.ConfigQueryResolver) *url.URL {
query := &url.Values{}

if config.Stream != "" {
Expand All @@ -31,14 +44,13 @@ func (config *Config) GetURL(_ types.ConfigQueryResolver) *url.URL {
return &url.URL{
User: url.UserPassword(config.BotMail, config.BotKey),
Host: config.Host,
Path: config.Path,
RawQuery: query.Encode(),
Scheme: Scheme,
}
}

// SetURL updates a ServiceConfig from a URL representation of it's field values
func (config *Config) SetURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error {
func (config *Config) setURL(_ types.ConfigQueryResolver, serviceURL *url.URL) error {
var ok bool

config.BotMail = serviceURL.User.Username()
Expand All @@ -59,7 +71,6 @@ func (config *Config) SetURL(_ types.ConfigQueryResolver, serviceURL *url.URL) e
return errors.New(string(MissingHost))
}

config.Path = "api/v1/messages"
config.Stream = serviceURL.Query().Get("stream")
config.Topic = serviceURL.Query().Get("topic")

Expand All @@ -72,7 +83,6 @@ func (config *Config) Clone() *Config {
BotMail: config.BotMail,
BotKey: config.BotKey,
Host: config.Host,
Path: config.Path,
Stream: config.Stream,
Topic: config.Topic,
}
Expand All @@ -86,7 +96,7 @@ const (
// CreateConfigFromURL to use within the zulip service
func CreateConfigFromURL(serviceURL *url.URL) (*Config, error) {
config := Config{}
err := config.SetURL(nil, serviceURL)
err := config.setURL(nil, serviceURL)

return &config, err
}
7 changes: 2 additions & 5 deletions pkg/services/zulip/zulip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ var _ = Describe("the zulip service", func() {
BotMail: "[email protected]",
BotKey: "correcthorsebatterystable",
Host: "example.zulipchat.com",
Path: "api/v1/messages",
Stream: "foo",
Topic: "bar",
}
Expand All @@ -100,7 +99,6 @@ var _ = Describe("the zulip service", func() {
BotMail: "[email protected]",
BotKey: "correcthorsebatterystable",
Host: "example.zulipchat.com",
Path: "api/v1/messages",
Stream: "foo",
Topic: "bar",
}
Expand All @@ -121,7 +119,6 @@ var _ = Describe("the zulip service", func() {
BotMail: "[email protected]",
BotKey: "correcthorsebatterystable",
Host: "example.zulipchat.com",
Path: "api/v1/messages",
Stream: "foo",
Topic: "bar",
}
Expand All @@ -137,7 +134,7 @@ var _ = Describe("the zulip service", func() {
Stream: "foo",
Topic: "bar",
}
url := config.GetURL(nil)
url := config.GetURL()
Expect(url.String()).To(Equal("zulip://bot-name%40zulipchat.com:[email protected]?stream=foo&topic=bar"))
})
})
Expand All @@ -149,7 +146,7 @@ var _ = Describe("the zulip service", func() {
Host: "example.zulipchat.com",
Stream: "foo",
}
url := config.GetURL(nil)
url := config.GetURL()
Expect(url.String()).To(Equal("zulip://bot-name%40zulipchat.com:[email protected]?stream=foo"))
})
})
Expand Down

0 comments on commit e97873f

Please sign in to comment.