From b914bf911a4308a0e0c5db39b186658f1cf2de85 Mon Sep 17 00:00:00 2001 From: jolheiser Date: Fri, 16 Dec 2022 21:54:36 -0600 Subject: [PATCH 1/3] feat: webhook URL parse Signed-off-by: jolheiser --- webhook/webhook_client_impl.go | 24 +++++++++++++++ webhook/webook_test.go | 56 ++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 webhook/webook_test.go diff --git a/webhook/webhook_client_impl.go b/webhook/webhook_client_impl.go index 29537409..87eb9f2c 100644 --- a/webhook/webhook_client_impl.go +++ b/webhook/webhook_client_impl.go @@ -2,12 +2,36 @@ package webhook import ( "context" + "errors" + "net/url" + "strings" "github.com/disgoorg/disgo/discord" "github.com/disgoorg/disgo/rest" "github.com/disgoorg/snowflake/v2" ) +// ParseURL parses a webhook URL +func ParseURL(webhook string, opts ...ConfigOpt) (Client, error) { + u, err := url.Parse(webhook) + if err != nil { + return nil, err + } + + parts := strings.FieldsFunc(u.Path, func(r rune) bool { return r == '/' }) + if len(parts) != 4 { + return nil, errors.New("invalid webhook URL") + } + id, token := parts[2], parts[3] + + sf, err := snowflake.Parse(id) + if err != nil { + return nil, err + } + + return New(sf, token, opts...), nil +} + // New creates a new Client with the given ID, token and ConfigOpt(s). func New(id snowflake.ID, token string, opts ...ConfigOpt) Client { config := DefaultConfig() diff --git a/webhook/webook_test.go b/webhook/webook_test.go new file mode 100644 index 00000000..ae6dee42 --- /dev/null +++ b/webhook/webook_test.go @@ -0,0 +1,56 @@ +package webhook + +import ( + "testing" + + "github.com/disgoorg/snowflake/v2" + "github.com/stretchr/testify/require" +) + +func TestParseURL(t *testing.T) { + tt := []struct { + URL string + ID snowflake.ID + Token string + Err bool + }{ + { + URL: "https://discord.com/api/webhooks/123456789123456789/foo", + ID: snowflake.ID(123456789123456789), + Token: "foo", + }, + { + URL: "https://discord.com/api/webhooks/123456789123456789/foo/", + ID: snowflake.ID(123456789123456789), + Token: "foo", + }, + { + URL: "https://canary.discord.com/api/webhooks/123456789123456789/foo", + ID: snowflake.ID(123456789123456789), + Token: "foo", + }, + { + URL: "foobarbaz", + Err: true, + }, + { + URL: "https://discord.com/api/webhooks/123456789123456789/foo?wait=10", + ID: snowflake.ID(123456789123456789), + Token: "foo", + }, + } + + for _, tc := range tt { + t.Run(tc.URL, func(t *testing.T) { + assert := require.New(t) + + c, err := ParseURL(tc.URL) + if tc.Err { + assert.Error(err, "URL parsing should have resulted in an error") + return + } + assert.Equal(tc.ID, c.ID(), "URL ID should match") + assert.Equal(tc.Token, c.Token(), "URL token should match") + }) + } +} From 0adfd37d04288f96859756ea227a95b7fd93f8e6 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Fri, 16 Dec 2022 22:09:42 -0600 Subject: [PATCH 2/3] review: update func name MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ToπSenpai <15636011+TopiSenpai@users.noreply.github.com> --- webhook/webhook_client_impl.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/webhook_client_impl.go b/webhook/webhook_client_impl.go index 87eb9f2c..0c523bef 100644 --- a/webhook/webhook_client_impl.go +++ b/webhook/webhook_client_impl.go @@ -12,7 +12,7 @@ import ( ) // ParseURL parses a webhook URL -func ParseURL(webhook string, opts ...ConfigOpt) (Client, error) { +func NewWithURL(webhook string, opts ...ConfigOpt) (Client, error) { u, err := url.Parse(webhook) if err != nil { return nil, err From 590339a24c019118676807b94bd15bb38460c216 Mon Sep 17 00:00:00 2001 From: John Olheiser Date: Fri, 16 Dec 2022 22:39:59 -0600 Subject: [PATCH 3/3] review: fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: ToπSenpai <15636011+TopiSenpai@users.noreply.github.com> --- webhook/webook_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/webook_test.go b/webhook/webook_test.go index ae6dee42..3c00ce37 100644 --- a/webhook/webook_test.go +++ b/webhook/webook_test.go @@ -44,7 +44,7 @@ func TestParseURL(t *testing.T) { t.Run(tc.URL, func(t *testing.T) { assert := require.New(t) - c, err := ParseURL(tc.URL) + c, err := NewWithURL(tc.URL) if tc.Err { assert.Error(err, "URL parsing should have resulted in an error") return