-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into chore/update-readme
- Loading branch information
Showing
35 changed files
with
2,576 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package lib | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
"net/url" | ||
"strconv" | ||
) | ||
|
||
type ChangesService service | ||
|
||
func (c *ChangesService) GetChangesCount(ctx context.Context) (ChangesCountResponse, error) { | ||
var resp ChangesCountResponse | ||
URL := c.client.config.BackendURL.JoinPath("changes", "count") | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = c.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (c *ChangesService) GetChanges(ctx context.Context, q ChangesGetQuery) (ChangesGetResponse, error) { | ||
var resp ChangesGetResponse | ||
URL := c.client.config.BackendURL.JoinPath("changes") | ||
URL.RawQuery = q.BuildQuery() | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = c.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (c *ChangesService) ApplyChange(ctx context.Context, changeId string) (ChangesApplyResponse, error) { | ||
var resp ChangesApplyResponse | ||
URL := c.client.config.BackendURL.JoinPath("changes", changeId, "apply") | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), http.NoBody) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = c.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (c *ChangesService) ApplyBulkChanges(ctx context.Context, payload ChangesBulkApplyPayload) (ChangesApplyResponse, error) { | ||
var resp ChangesApplyResponse | ||
URL := c.client.config.BackendURL.JoinPath("changes", "bulk", "apply") | ||
jsonBody, err := json.Marshal(payload) | ||
if err != nil { | ||
return resp, err | ||
} | ||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, URL.String(), bytes.NewBuffer(jsonBody)) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
_, err = c.client.sendRequest(req, &resp) | ||
if err != nil { | ||
return resp, err | ||
} | ||
|
||
return resp, nil | ||
} | ||
|
||
func (c *ChangesGetQuery) BuildQuery() string { | ||
params := url.Values{} | ||
|
||
if c.Page == 0 { | ||
c.Page = 1 | ||
} | ||
|
||
if c.Limit == 0 { | ||
c.Limit = 10 | ||
} | ||
|
||
if c.Promoted == "" { | ||
c.Promoted = "false" | ||
} | ||
params.Add("page", strconv.Itoa(c.Page)) | ||
params.Add("limit", strconv.Itoa(c.Limit)) | ||
params.Add("promoted", c.Promoted) | ||
return params.Encode() | ||
} |
Oops, something went wrong.