Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-add feeds api #52

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions lib/feeds.go
Original file line number Diff line number Diff line change
@@ -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
}
87 changes: 87 additions & 0 deletions lib/feeds_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
2 changes: 2 additions & 0 deletions lib/novu.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type APIClient struct {
EventApi *EventService
ExecutionsApi *ExecutionsService
MessagesApi *MessagesService
FeedsApi *FeedsService
TopicsApi *TopicService
IntegrationsApi *IntegrationService
InboundParserApi *InboundParserService
Expand All @@ -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)
Expand Down
Loading