Skip to content

Commit

Permalink
Merge branch 'master' into chore/update-readme
Browse files Browse the repository at this point in the history
  • Loading branch information
unicodeveloper authored Feb 16, 2024
2 parents f495317 + 52bd1a7 commit 8a7df94
Show file tree
Hide file tree
Showing 35 changed files with 2,576 additions and 22 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Novu's API exposes the entire Novu features via a standardized programmatic interface. Please refer to the full [documentation](https://docs.novu.co/docs/overview/introduction) to learn more.

## Installation & Usage

Install the package to your GoLang project.

```golang
go get github.com/novuhq/go-novu
```
Expand Down Expand Up @@ -61,6 +63,7 @@ func main() {
fmt.Println(integrations)
}
```

**NOTE**
Check the `cmd` directory to see a sample implementation and test files to see sample tests

Expand Down Expand Up @@ -93,20 +96,24 @@ Class | Method
*IntegrationsApi* | [**Delete**](https://docs.novu.co/platform/integrations) | **Delete** /integrations/:integrationId | Delete an integration
*IntegrationsApi* | [**Get**](https://docs.novu.co/platform/integrations) | **Get** /integrations | Get all integrations
*IntegrationsApi* | [**GetActive**](https://docs.novu.co/platform/integrations) | **Get** /integrations/active | Get all active integrations
_InboundParserApi_ | [**Get**](https://docs.novu.co/platform/inbound-parse-webhook/) | **Get** /inbound-parse/mx/status | Validate the mx record setup for the inbound parse functionality


## Authorization (api-key)

- **Type**: API key
- **API key parameter name**: ApiKey
- **Location**: HTTP header

### For more information about these methods and their parameters, see the [API documentation](https://docs.novu.co/api-reference/overview).

## Support and Feedback

Be sure to visit the Novu official [documentation website](https://docs.novu.co/docs) for additional information about our API.

If you find a bug, please post the issue on [Github](https://github.com/novuhq/go-novu/issues).

As always, if you need additional assistance, join our Discord us a note [here](https://discord.gg/TT6TttXjRe).
As always, if you need additional assistance, join our Discord us a note [here](https://discord.gg/novu).

## Contributors

Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ require (

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-retryablehttp v0.7.4 h1:ZQgVdpTdAL7WpMIwLzCfbalOcSUdkDZnpUv3/+BxzFA=
github.com/hashicorp/go-retryablehttp v0.7.4/go.mod h1:Jy/gPYAdjqffZ/yFGCFV2doI5wjtH1ewM9u8iYVjtX8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.5 h1:s5PTfem8p8EbKQOctVV53k6jCJt3UX4IEJzwh+C324Q=
github.com/stretchr/testify v1.7.5/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down
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)
}
104 changes: 104 additions & 0 deletions lib/changes.go
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()
}
Loading

0 comments on commit 8a7df94

Please sign in to comment.