Skip to content

Commit

Permalink
Merge pull request #7 from samsoft00/revamp/novu-base-url
Browse files Browse the repository at this point in the history
revamp novu base url and update test
  • Loading branch information
unicodeveloper authored Mar 20, 2023
2 parents ebe03f7 + abdc29f commit 670a3ca
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
# vendor/
5 changes: 2 additions & 3 deletions lib/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
)

Expand All @@ -16,7 +15,7 @@ type EventService service

func (e *EventService) Trigger(ctx context.Context, eventId string, data ITriggerPayloadOptions) (EventResponse, error) {
var resp EventResponse
URL := fmt.Sprintf(e.client.config.BackendURL+"/%s", "events/trigger")
URL := e.client.config.BackendURL.JoinPath("events/trigger")

reqBody := EventRequest{
Name: eventId,
Expand All @@ -29,7 +28,7 @@ func (e *EventService) Trigger(ctx context.Context, eventId string, data ITrigge

jsonBody, _ := json.Marshal(reqBody)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return resp, err
}
Expand Down
8 changes: 4 additions & 4 deletions lib/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"context"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
Expand All @@ -25,7 +25,7 @@ const (
)

func fileToStruct(filepath string, s interface{}) io.Reader {
bb, _ := ioutil.ReadFile(filepath)
bb, _ := os.ReadFile(filepath)
json.Unmarshal(bb, s)
return bytes.NewReader(bb)
}
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestEventServiceTrigger_Success(t *testing.T) {
ctx := context.Background()
fileToStruct(filepath.Join("../testdata", "novu_send_trigger.json"), &triggerPayload)

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: eventService.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(eventService.URL)})
_, err := c.EventApi.Trigger(ctx, novuEventId, triggerPayload)

require.Nil(t, err)
Expand Down Expand Up @@ -124,7 +124,7 @@ func TestEventServiceTriggerForTopic_Success(t *testing.T) {
ctx := context.Background()
fileToStruct(filepath.Join("../testdata", "novu_send_trigger_topic_recipient.json"), &triggerPayload)

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: eventService.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(eventService.URL)})
_, err := c.EventApi.Trigger(ctx, novuEventId, triggerPayload)

require.Nil(t, err)
Expand Down
21 changes: 14 additions & 7 deletions lib/novu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"

"github.com/pkg/errors"
)

const (
NovuURL = "https://api.novu.co"
NovuVersion = "v1"
)

type Config struct {
BackendURL string
BackendURL *url.URL
HttpClient *http.Client
}

Expand Down Expand Up @@ -101,15 +107,16 @@ func (c APIClient) decode(v interface{}, b []byte) (err error) {
return nil
}

func buildBackendURL(cfg *Config) string {
novuVersion := "v1"
func buildBackendURL(cfg *Config) *url.URL {

if cfg.BackendURL == "" {
return fmt.Sprintf("https://api.novu.co/%s", novuVersion)
if cfg.BackendURL == nil {
rawURL := fmt.Sprintf("%s/%s", NovuURL, NovuVersion)
return MustParseURL(rawURL)
}

if strings.Contains(cfg.BackendURL, "novu.co/v") {
if strings.Contains(cfg.BackendURL.String(), "novu.co/v") {
return cfg.BackendURL
}
return fmt.Sprintf(cfg.BackendURL+"/%s", novuVersion)

return cfg.BackendURL.JoinPath(NovuVersion)
}
13 changes: 6 additions & 7 deletions lib/subscribers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
Expand All @@ -20,7 +19,7 @@ type SubscriberService service

func (s *SubscriberService) Identify(ctx context.Context, subscriberID string, data interface{}) (SubscriberResponse, error) {
var resp SubscriberResponse
URL := fmt.Sprintf(s.client.config.BackendURL+"/%s", "subscribers")
URL := s.client.config.BackendURL.JoinPath("subscribers")

reqBody, err := s.client.mergeStruct(data, map[string]interface{}{"subscriberId": subscriberID})
if err != nil {
Expand All @@ -29,7 +28,7 @@ func (s *SubscriberService) Identify(ctx context.Context, subscriberID string, d

jsonBody, _ := json.Marshal(reqBody)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return resp, err
}
Expand All @@ -44,11 +43,11 @@ func (s *SubscriberService) Identify(ctx context.Context, subscriberID string, d

func (s *SubscriberService) Update(ctx context.Context, subscriberID string, data interface{}) (SubscriberResponse, error) {
var resp SubscriberResponse
URL := fmt.Sprintf(s.client.config.BackendURL+"/subscribers/%s", subscriberID)
URL := s.client.config.BackendURL.JoinPath("subscribers", subscriberID)

jsonBody, _ := json.Marshal(data)

req, err := http.NewRequestWithContext(ctx, http.MethodPut, URL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, http.MethodPut, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return resp, err
}
Expand All @@ -63,9 +62,9 @@ func (s *SubscriberService) Update(ctx context.Context, subscriberID string, dat

func (s *SubscriberService) Delete(ctx context.Context, subscriberID string) (SubscriberResponse, error) {
var resp SubscriberResponse
URL := fmt.Sprintf(s.client.config.BackendURL+"/subscribers/%s", subscriberID)
URL := s.client.config.BackendURL.JoinPath("subscribers", subscriberID)

req, err := http.NewRequestWithContext(ctx, http.MethodDelete, URL, http.NoBody)
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, URL.String(), http.NoBody)
if err != nil {
return resp, err
}
Expand Down
6 changes: 3 additions & 3 deletions lib/subscribers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestSubscriberService_Identify_Success(t *testing.T) {
ctx := context.Background()
fileToStruct(filepath.Join("../testdata", "identify_subscriber.json"), &subscriberPayload)

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: subscriberService.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(subscriberService.URL)})

resp, err := c.SubscriberApi.Identify(ctx, subscriberID, subscriberPayload)
require.Nil(t, err)
Expand Down Expand Up @@ -116,7 +116,7 @@ func TestSubscriberService_Update_Success(t *testing.T) {
ctx := context.Background()
fileToStruct(filepath.Join("../testdata", "update_subscriber.json"), &updateSubscriber)

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: subscriberService.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(subscriberService.URL)})

resp, err := c.SubscriberApi.Update(ctx, subscriberID, updateSubscriber)
require.Nil(t, err)
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestSubscriberService_Delete_Success(t *testing.T) {
w.Write(bb)
}))

c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: subscriberService.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(subscriberService.URL)})

resp, err := c.SubscriberApi.Delete(ctx, subscriberID)
require.Nil(t, err)
Expand Down
25 changes: 12 additions & 13 deletions lib/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"

"github.com/pkg/errors"
Expand All @@ -23,7 +22,7 @@ type TopicService service

func (t *TopicService) Create(ctx context.Context, key string, name string) error {
var resp interface{}
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s", "topics")
URL := t.client.config.BackendURL.JoinPath("topics")

reqBody := CreateTopicRequest{
Name: name,
Expand All @@ -32,7 +31,7 @@ func (t *TopicService) Create(ctx context.Context, key string, name string) erro

jsonBody, _ := json.Marshal(reqBody)

req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return err
}
Expand All @@ -51,14 +50,14 @@ func (t *TopicService) Create(ctx context.Context, key string, name string) erro

func (t *TopicService) List(ctx context.Context, options *ListTopicsOptions) (*ListTopicsResponse, error) {
var resp ListTopicsResponse
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s", "topics")
URL := t.client.config.BackendURL.JoinPath("topics")

if options == nil {
options = &ListTopicsOptions{}
}
queryParams, _ := json.Marshal(options)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL, bytes.NewBuffer(queryParams))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), bytes.NewBuffer(queryParams))
if err != nil {
return nil, err
}
Expand All @@ -72,13 +71,13 @@ func (t *TopicService) List(ctx context.Context, options *ListTopicsOptions) (*L
}

func (t *TopicService) AddSubscribers(ctx context.Context, key string, subscribers []string) error {
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s/%s/subscribers", "topics", key)
URL := t.client.config.BackendURL.JoinPath("topics", key, "subscribers")

queryParams, _ := json.Marshal(SubscribersTopicRequest{
Subscribers: subscribers,
})

req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, bytes.NewBuffer(queryParams))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(queryParams))
if err != nil {
return err
}
Expand All @@ -93,13 +92,13 @@ func (t *TopicService) AddSubscribers(ctx context.Context, key string, subscribe
}

func (t *TopicService) RemoveSubscribers(ctx context.Context, key string, subscribers []string) error {
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s/%s/subscribers/removal", "topics", key)
URL := t.client.config.BackendURL.JoinPath("topics", key, "subscribers/removal")

queryParams, _ := json.Marshal(SubscribersTopicRequest{
Subscribers: subscribers,
})

req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL, bytes.NewBuffer(queryParams))
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(queryParams))
if err != nil {
return err
}
Expand All @@ -114,9 +113,9 @@ func (t *TopicService) RemoveSubscribers(ctx context.Context, key string, subscr

func (t *TopicService) Get(ctx context.Context, key string) (*GetTopicResponse, error) {
var resp GetTopicResponse
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s/%s", "topics", key)
URL := t.client.config.BackendURL.JoinPath("topics", key)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL, bytes.NewBuffer([]byte{}))
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), bytes.NewBuffer([]byte{}))
if err != nil {
return nil, err
}
Expand All @@ -131,15 +130,15 @@ func (t *TopicService) Get(ctx context.Context, key string) (*GetTopicResponse,

func (t *TopicService) Rename(ctx context.Context, key string, name string) (*GetTopicResponse, error) {
var resp GetTopicResponse
URL := fmt.Sprintf(t.client.config.BackendURL+"/%s/%s", "topics", key)
URL := t.client.config.BackendURL.JoinPath("topics", key)

reqBody := RenameTopicRequest{
Name: name,
}

jsonBody, _ := json.Marshal(reqBody)

req, err := http.NewRequestWithContext(ctx, http.MethodPatch, URL, bytes.NewBuffer(jsonBody))
req, err := http.NewRequestWithContext(ctx, http.MethodPatch, URL.String(), bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions lib/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestCreateTopic_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
err := c.TopicsApi.Create(ctx, "topicKey", "topic")

require.NoError(t, err)
Expand All @@ -106,7 +106,7 @@ func TestAddSubscription_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
err := c.TopicsApi.AddSubscribers(ctx, key, subs)

require.NoError(t, err)
Expand All @@ -127,7 +127,7 @@ func TestAddSubscriptionRemoval_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
err := c.TopicsApi.RemoveSubscribers(ctx, key, subs)

require.NoError(t, err)
Expand All @@ -154,7 +154,7 @@ func TestGetTopic_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.TopicsApi.Get(ctx, key)

require.NoError(t, err)
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestListTopics_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.TopicsApi.List(ctx, nil)

require.NoError(t, err)
Expand Down Expand Up @@ -217,7 +217,7 @@ func TestRenameTopic_Success(t *testing.T) {
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: httpServer.URL})
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.TopicsApi.Rename(ctx, topicKey, newName)

require.NoError(t, err)
Expand Down
12 changes: 12 additions & 0 deletions lib/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lib

import "net/url"

func MustParseURL(rawURL string) *url.URL {
u, err := url.Parse(rawURL)
if err != nil {
panic(err)
}

return u
}

0 comments on commit 670a3ca

Please sign in to comment.