Skip to content

Commit

Permalink
Merge pull request #40 from king-11/topicMethods
Browse files Browse the repository at this point in the history
feat: add missing methods integration and topic
  • Loading branch information
Cliftonz authored Oct 4, 2023
2 parents cdb0872 + f8c7c15 commit 6f83919
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
20 changes: 20 additions & 0 deletions lib/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type IIntegration interface {
Create(ctx context.Context, request CreateIntegrationRequest) (*IntegrationResponse, error)
GetAll(ctx context.Context) (*GetIntegrationsResponse, error)
GetActive(ctx context.Context) (*GetIntegrationsResponse, error)
GetWebhookSupportStatus(ctx context.Context, providerId string) (bool, error)
Update(ctx context.Context, integrationId string, request UpdateIntegrationRequest) (*IntegrationResponse, error)
Delete(ctx context.Context, integrationId string) (*IntegrationResponse, error)
}
Expand Down Expand Up @@ -83,6 +84,25 @@ func (i IntegrationService) GetActive(ctx context.Context) (*GetIntegrationsResp
return &response, nil
}

func (i IntegrationService) GetWebhookSupportStatus(ctx context.Context, providerId string) (bool, error) {
URL := i.client.config.BackendURL.JoinPath("integrations", "webhook", "provider", providerId, "status")

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody)

if err != nil {
return false, err
}

var status bool
_, err = i.client.sendRequest(req, &status)

if err != nil {
return false, err
}

return status, nil
}

func (i IntegrationService) Update(ctx context.Context, integrationId string, request UpdateIntegrationRequest) (*IntegrationResponse, error) {
var response IntegrationResponse
URL := i.client.config.BackendURL.JoinPath("integrations", integrationId)
Expand Down
22 changes: 22 additions & 0 deletions lib/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,28 @@ func TestGetActiveIntegration_Success(t *testing.T) {
require.NoError(t, err)
}

func TestGetWebhookSupportStatusIntegration_Success(t *testing.T) {
providerId := "sendgrid"
response := true
httpServer := IntegrationTestServer(t, IntegrationServerOptions[interface{}]{
ExpectedRequest: IntegrationRequestDetails[interface{}]{
Url: fmt.Sprintf("/v1/integrations/webhook/provider/%s/status", providerId),
Method: http.MethodGet,
},
ExpectedResponse: IntegrationResponseDetails{
StatusCode: http.StatusOK,
Body: response,
},
})

ctx := context.Background()
novuClient := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})

res, err := novuClient.IntegrationsApi.GetWebhookSupportStatus(ctx, providerId)
assert.Equal(t, response, res)
require.NoError(t, err)
}

func TestUpdateIntegration_Success(t *testing.T) {
const integrationId = "integrationId"

Expand Down
9 changes: 9 additions & 0 deletions lib/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ type GetTopicResponse struct {
Subscribers []string `json:"subscribers"`
}

type CheckTopicSubscriberResponse struct {
OrganizationId string `json:"_organizationId"`
EnvironmentId string `json:"_environmentId"`
SubsriberId string `json:"_subscriberId"`
Id string `json:"_topicId"`
Key string `json:"topicKey"`
ExternalSubscriberId string `json:"externalSubscriberId"`
}

type ListTopicsOptions struct {
Page *int `json:"page,omitempty"`
PageSize *int `json:"pageSize,omitempty"`
Expand Down
18 changes: 18 additions & 0 deletions lib/topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type ITopic interface {
Create(ctx context.Context, key string, name string) error
List(ctx context.Context, options *ListTopicsOptions) (*ListTopicsResponse, error)
CheckTopicSubscriber(ctx context.Context, key string, externalsubscriber string) (*CheckTopicSubscriberResponse, error)
AddSubscribers(ctx context.Context, key string, subscribers []string) error
RemoveSubscribers(ctx context.Context, key string, subscribers []string) error
Get(ctx context.Context, key string) (*GetTopicResponse, error)
Expand Down Expand Up @@ -71,6 +72,23 @@ func (t *TopicService) List(ctx context.Context, options *ListTopicsOptions) (*L
return &resp, nil
}

func (t *TopicService) CheckTopicSubscriber(ctx context.Context, key string, externalsubscriber string) (*CheckTopicSubscriberResponse, error) {
var resp CheckTopicSubscriberResponse
URL := t.client.config.BackendURL.JoinPath("topics", key, "subscribers", externalsubscriber)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), bytes.NewBuffer([]byte{}))
if err != nil {
return nil, err
}

_, err = t.client.sendRequest(req, &resp)
if err != nil {
return nil, err
}

return &resp, nil
}

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

Expand Down
20 changes: 20 additions & 0 deletions lib/topic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,26 @@ func TestCreateTopic_Success(t *testing.T) {
require.NoError(t, err)
}

func TestCheckTopicSubscriber_Success(t *testing.T) {
topicKey := "topicKey"
subscriber := "subId"
httpServer := createTestServer(t, TestServerOptions[map[string]string, lib.CheckTopicSubscriberResponse]{
expectedURLPath: fmt.Sprintf("/v1/topics/%s/subscribers/%s", topicKey, subscriber),
expectedSentMethod: http.MethodGet,
expectedSentBody: map[string]string{},
responseStatusCode: http.StatusOK,
responseBody: lib.CheckTopicSubscriberResponse{
ExternalSubscriberId: subscriber,
},
})

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

require.NoError(t, err)
}

func TestAddSubscription_Success(t *testing.T) {
subs := []string{"subId"}
key := "topicKey"
Expand Down

0 comments on commit 6f83919

Please sign in to comment.