Skip to content

Commit

Permalink
fix(hangouts): fix generate compat
Browse files Browse the repository at this point in the history
  • Loading branch information
piksel committed Feb 3, 2021
1 parent e19ba77 commit 41c5999
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
17 changes: 16 additions & 1 deletion pkg/services/hangouts/hangouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ func (service *Service) Send(message string, _ *types.Params) error {
return err
}

postURL := getAPIURL(config)

jsonBuffer := bytes.NewBuffer(jsonBody)
resp, err := http.Post(config.URL.String(), "application/json", jsonBuffer)
resp, err := http.Post(postURL.String(), "application/json", jsonBuffer)

if err != nil {
return fmt.Errorf("failed to send notification to Hangouts Chat: %s", err)
Expand All @@ -55,3 +57,16 @@ func (service *Service) Send(message string, _ *types.Params) error {

return nil
}

func getAPIURL(config *Config) *url.URL {
query := url.Values{}
query.Set("key", config.Key)
query.Set("token", config.Token)

return &url.URL{
Path: config.Path,
Host: config.Host,
Scheme: "https",
RawQuery: query.Encode(),
}
}
51 changes: 47 additions & 4 deletions pkg/services/hangouts/hangouts_config.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,68 @@
package hangouts

import (
"errors"
"net/url"

"github.com/containrrr/shoutrrr/pkg/format"
"github.com/containrrr/shoutrrr/pkg/services/standard"
"github.com/containrrr/shoutrrr/pkg/types"
)

// Config for use within the Hangouts Chat plugin.
type Config struct {
standard.EnumlessConfig
URL *url.URL
Host string `default:"chat.googleapis.com"`
Path string
Token string
Key string
}

// SetURL updates a ServiceConfig from a URL representation of it's field values.
// GetURL returns a URL representation of it's current field values
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 {
config.URL = url
config.URL.Scheme = "https"
resolver := format.NewPropKeyResolver(config)
return config.setURL(&resolver, url)
}

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

query := serviceURL.Query()
config.Key = query.Get("key")
config.Token = query.Get("token")

if config.Key == "" {
return errors.New("missing field 'key'")
}

if config.Key == "" {
return errors.New("missing field 'token'")
}

return nil
}

func (config *Config) getURL(_ types.ConfigQueryResolver) *url.URL {
query := url.Values{}
query.Set("key", config.Key)
query.Set("token", config.Token)

return &url.URL{
Host: config.Host,
Path: config.Path,
RawQuery: query.Encode(),
Scheme: Scheme,
}
}

const (
// Scheme is the identifying part of this service's configuration URL.
Scheme = "hangouts"
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/hangouts/hangouts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ var _ = Describe("the Hangouts Chat plugin URL building", func() {
config.SetURL(configURL)

expectedURL := "https://chat.googleapis.com/v1/spaces/FOO/messages?key=bar&token=baz"
Expect(config.URL.String()).To(Equal(expectedURL))
Expect(getAPIURL(&config).String()).To(Equal(expectedURL))
})
})

0 comments on commit 41c5999

Please sign in to comment.