From 42746a31fea008dd045f4f532abfd820d9e54d44 Mon Sep 17 00:00:00 2001 From: Chang Sheng Date: Mon, 4 Sep 2023 16:02:23 +0800 Subject: [PATCH] feat: adds DELETE method for topic endpoint --- lib/topic.go | 18 ++++++++++++++++++ lib/topic_test.go | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/lib/topic.go b/lib/topic.go index f1e756b..5af2c35 100644 --- a/lib/topic.go +++ b/lib/topic.go @@ -16,6 +16,7 @@ type ITopic interface { RemoveSubscribers(ctx context.Context, key string, subscribers []string) error Get(ctx context.Context, key string) (*GetTopicResponse, error) Rename(ctx context.Context, key string, name string) (*GetTopicResponse, error) + Delete(ctx context.Context, key string) error } type TopicService service @@ -150,3 +151,20 @@ func (t *TopicService) Rename(ctx context.Context, key string, name string) (*Ge return &resp, nil } + +func (t *TopicService) Delete(ctx context.Context, key string) error { + var resp interface{} + URL := t.client.config.BackendURL.JoinPath("topics", key) + + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, URL.String(), http.NoBody) + if err != nil { + return err + } + + _, err = t.client.sendRequest(req, &resp) + if err != nil { + return err + } + + return nil +} diff --git a/lib/topic_test.go b/lib/topic_test.go index f4d0a91..4ccf48a 100644 --- a/lib/topic_test.go +++ b/lib/topic_test.go @@ -223,3 +223,22 @@ func TestRenameTopic_Success(t *testing.T) { require.NoError(t, err) require.Equal(t, resp, expectedResponse) } + +func TestDeleteTopic_Success(t *testing.T) { + key := "topicKey" + body := map[string]string{} + + httpServer := createTestServer(t, TestServerOptions[map[string]string, map[string]string]{ + expectedURLPath: fmt.Sprintf("/v1/topics/%s", key), + expectedSentMethod: http.MethodDelete, + expectedSentBody: body, + responseStatusCode: http.StatusNoContent, + responseBody: body, + }) + + ctx := context.Background() + c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)}) + err := c.TopicsApi.Delete(ctx, key) + + require.NoError(t, err) +}