From 900178cb49aad9b9652b5ed39e055ba5dc2136a8 Mon Sep 17 00:00:00 2001 From: jerempy <95110820+jerempy@users.noreply.github.com> Date: Wed, 4 Oct 2023 00:11:00 -0400 Subject: [PATCH] add feeds api --- lib/feeds.go | 55 ++++++++++++++++++++++++++++++ lib/feeds_test.go | 87 +++++++++++++++++++++++++++++++++++++++++++++++ lib/novu.go | 2 ++ 3 files changed, 144 insertions(+) create mode 100644 lib/feeds.go create mode 100644 lib/feeds_test.go diff --git a/lib/feeds.go b/lib/feeds.go new file mode 100644 index 0000000..3843806 --- /dev/null +++ b/lib/feeds.go @@ -0,0 +1,55 @@ +package lib + +import ( + "bytes" + "context" + "encoding/json" + "net/http" +) + +type FeedsService service + +func (e *FeedsService) CreateFeed(ctx context.Context, name string) (JsonResponse, error) { + var resp JsonResponse + URL := e.client.config.BackendURL.JoinPath("feeds") + n := map[string]string{"name": name} + jsonBody, _ := json.Marshal(n) + b := bytes.NewBuffer(jsonBody) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), b) + if err != nil { + return resp, err + } + _, err = e.client.sendRequest(req, &resp) + if err != nil { + return resp, err + } + return resp, nil +} + +func (e *FeedsService) GetFeeds(ctx context.Context) (JsonResponse, error) { + var resp JsonResponse + URL := e.client.config.BackendURL.JoinPath("feeds") + req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) + if err != nil { + return resp, err + } + _, err = e.client.sendRequest(req, &resp) + if err != nil { + return resp, err + } + return resp, nil +} + +func (e *FeedsService) DeleteFeed(ctx context.Context, feedId string) (JsonResponse, error) { + var resp JsonResponse + URL := e.client.config.BackendURL.JoinPath("feeds", feedId) + req, err := http.NewRequestWithContext(ctx, http.MethodDelete, URL.String(), http.NoBody) + if err != nil { + return resp, err + } + _, err = e.client.sendRequest(req, &resp) + if err != nil { + return resp, err + } + return resp, nil +} diff --git a/lib/feeds_test.go b/lib/feeds_test.go new file mode 100644 index 0000000..fad82b2 --- /dev/null +++ b/lib/feeds_test.go @@ -0,0 +1,87 @@ +package lib_test + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/novuhq/go-novu/lib" +) + +var feedsApiResponse = `{ + "data": { + "_id": "string", + "name": "string", + "identifier": "string", + "_environmentId": "string", + "_organizationId": "string" + } +} +` + +func TestCreateFeed(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + t.Errorf("Want POST, got %s", r.Method) + } + if r.URL.Path != "/v1/feeds" { + t.Errorf("Want /v1/feeds, got %s", r.URL.Path) + } + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(feedsApiResponse)) + })) + defer server.Close() + c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) + resp, err := c.FeedsApi.CreateFeed(context.Background(), "FeedyMcFeederson") + if err != nil { + t.Errorf("Error should be nil, got %v", err) + } + if resp.Data == nil || resp.Data == "" { + t.Error("Expected response, got none") + } +} + +func TestGetFeeds(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("Want GET, got %s", r.Method) + } + if r.URL.Path != "/v1/feeds" { + t.Errorf("Want /v1/feeds, got %s", r.URL.Path) + } + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(feedsApiResponse)) + })) + defer server.Close() + c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) + resp, err := c.FeedsApi.GetFeeds(context.Background()) + if err != nil { + t.Errorf("Error should be nil, got %v", err) + } + if resp.Data == nil || resp.Data == "" { + t.Error("Expected response, got none") + } +} + +func TestDeleteFeed(t *testing.T) { + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + t.Errorf("Want DELETE, got %s", r.Method) + } + if r.URL.Path != "/v1/feeds/FeedId" { + t.Errorf("Want /v1/feeds/FeedId, got %s", r.URL.Path) + } + w.Header().Set("Content-Type", "application/json") + w.Write([]byte(feedsApiResponse)) + })) + defer server.Close() + c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)}) + resp, err := c.FeedsApi.DeleteFeed(context.Background(), "FeedId") + if err != nil { + t.Errorf("Error should be nil, got %v", err) + } + if resp.Data == nil || resp.Data == "" { + t.Error("Expected response, got none") + } +} diff --git a/lib/novu.go b/lib/novu.go index 4417f18..ce1b424 100644 --- a/lib/novu.go +++ b/lib/novu.go @@ -32,6 +32,7 @@ type APIClient struct { EventApi *EventService ExecutionsApi *ExecutionsService MessagesApi *MessagesService + FeedsApi *FeedsService TopicsApi *TopicService IntegrationsApi *IntegrationService InboundParserApi *InboundParserService @@ -55,6 +56,7 @@ func NewAPIClient(apiKey string, cfg *Config) *APIClient { // API Services c.EventApi = (*EventService)(&c.common) c.ExecutionsApi = (*ExecutionsService)(&c.common) + c.FeedsApi = (*FeedsService)(&c.common) c.SubscriberApi = (*SubscriberService)(&c.common) c.MessagesApi = (*MessagesService)(&c.common) c.TopicsApi = (*TopicService)(&c.common)