Skip to content

Commit

Permalink
Merge pull request #55 from Bezaeel/52-ft-blueprints-methods
Browse files Browse the repository at this point in the history
feat: add blueprints api
  • Loading branch information
Cliftonz authored Oct 6, 2023
2 parents 4c63a38 + 9fe98ab commit 1449de3
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
42 changes: 42 additions & 0 deletions lib/blueprints.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package lib

import (
"context"
"net/http"
)

type BlueprintService service

func (b *BlueprintService) GetGroupByCategory(ctx context.Context) (BlueprintGroupByCategoryResponse, error) {
var resp BlueprintGroupByCategoryResponse
URL := b.client.config.BackendURL.JoinPath("blueprints", "group-by-category")

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody)
if err != nil {
return resp, err
}

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

return resp, nil
}

func (b *BlueprintService) GetByTemplateID(ctx context.Context, templateID string) (BlueprintByTemplateIdResponse, error) {
var resp BlueprintByTemplateIdResponse
URL := b.client.config.BackendURL.JoinPath("blueprints", templateID)

req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody)
if err != nil {
return resp, err
}

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

return resp, nil
}
70 changes: 70 additions & 0 deletions lib/blueprints_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package lib_test

import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"path/filepath"
"testing"

"github.com/novuhq/go-novu/lib"
"github.com/stretchr/testify/require"
)

func responseStringToStruct(str string, s interface{}) io.Reader {
bb := []byte(str)
json.Unmarshal(bb, s)
return bytes.NewReader(bb)
}

const templateID = "650ae12e61c036a0a5419480"

var groupByCategory = `{
"general": [
{}
],
"popular": {}
}`

func TestBlueprintService_GetGroupByCategory_Success(t *testing.T) {
var expectedResponse lib.BlueprintGroupByCategoryResponse
responseStringToStruct(groupByCategory, &expectedResponse)

httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.BlueprintGroupByCategoryResponse]{
expectedURLPath: fmt.Sprintf("/v1/blueprints/group-by-category"),
expectedSentMethod: http.MethodGet,
expectedSentBody: http.NoBody,
responseStatusCode: http.StatusOK,
responseBody: &expectedResponse,
})

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

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}

func TestBlueprintService_GetByTemplateID_Success(t *testing.T) {
var expectedResponse lib.BlueprintByTemplateIdResponse
fileToStruct(filepath.Join("../testdata", "blueprint_response.json"), &expectedResponse)

httpServer := createTestServer(t, TestServerOptions[io.Reader, *lib.BlueprintByTemplateIdResponse]{
expectedURLPath: fmt.Sprintf("/v1/blueprints/%s", templateID),
expectedSentMethod: http.MethodGet,
expectedSentBody: http.NoBody,
responseStatusCode: http.StatusOK,
responseBody: &expectedResponse,
})

ctx := context.Background()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(httpServer.URL)})
resp, err := c.BlueprintApi.GetByTemplateID(ctx, templateID)

require.NoError(t, err)
require.Equal(t, resp, expectedResponse)
}
31 changes: 31 additions & 0 deletions lib/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,3 +408,34 @@ type MxRecordConfiguredStatus struct {
type InboundParserResponse struct {
Data MxRecordConfiguredStatus `json:"data"`
}

type BlueprintByTemplateIdResponse struct {
Id string `json:"_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Active bool `json:"active,omitempty"`
Draft bool `json:"draft,omitempty"`
PreferenceSettings interface{} `json:"preferenceSettings,omitempty"`
Critical bool `json:"critical,omitempty"`
Tags []string `json:"tags,omitempty"`
Steps []interface{} `json:"steps,omitempty"`
OrganizationID string `json:"_organizationId,omitempty"`
CreatorID string `json:"_creatorId,omitempty"`
EnvironmentID string `json:"_environmentId,omitempty"`
Triggers []interface{} `json:"triggers,omitempty"`
NotificationGroupID string `json:"_notificationGroupId,omitempty"`
ParentId string `json:"_parentId,omitempty"`
Deleted bool `json:"deleted,omitempty"`
DeletedAt string `json:"deletedAt,omitempty"`
DeletedBy string `json:"deletedBy,omitempty"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
NotificationGroup interface{} `json:"notificationGroup,omitempty"`
IsBlueprint bool `json:"isBlueprint,omitempty"`
BlueprintID string `json:"blueprintId,omitempty"`
}

type BlueprintGroupByCategoryResponse struct {
General []interface{} `json:"general,omitempty"`
Popular interface{} `json:"popular,omitempty"`
}
2 changes: 2 additions & 0 deletions lib/novu.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type APIClient struct {
common service

// Api Service
BlueprintApi *BlueprintService
SubscriberApi *SubscriberService
EventApi *EventService
ExecutionsApi *ExecutionsService
Expand Down Expand Up @@ -62,6 +63,7 @@ func NewAPIClient(apiKey string, cfg *Config) *APIClient {
c.TopicsApi = (*TopicService)(&c.common)
c.IntegrationsApi = (*IntegrationService)(&c.common)
c.InboundParserApi = (*InboundParserService)(&c.common)
c.BlueprintApi = (*BlueprintService)(&c.common)
return c
}

Expand Down
31 changes: 31 additions & 0 deletions testdata/blueprint_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"_id": "string",
"name": "string",
"description": "string",
"active": true,
"draft": true,
"preferenceSettings": {},
"critical": true,
"tags": [
"string"
],
"steps": [
{}
],
"_organizationId": "string",
"_creatorId": "string",
"_environmentId": "string",
"triggers": [
{}
],
"_notificationGroupId": "string",
"_parentId": "string",
"deleted": true,
"deletedAt": "string",
"deletedBy": "string",
"createdAt": "string",
"updatedAt": "string",
"notificationGroup": {},
"isBlueprint": true,
"blueprintId": "string"
}

0 comments on commit 1449de3

Please sign in to comment.