Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat - add execution details api #54

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions lib/executions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lib

import (
"context"
"net/http"
"net/url"
)

type ExecutionsService service

func (e *ExecutionsService) GetExecutions(ctx context.Context, q QueryBuilder) (JsonResponse, error) {
var resp JsonResponse
URL := e.client.config.BackendURL.JoinPath("execution-details")
URL.RawQuery = q.BuildQuery()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, URL.String(), http.NoBody)
if err != nil {
return resp, err
}
_, err = e.client.sendRequest(req, &resp)
if err != nil {
return resp, err
}
return resp, nil
}

func (q ExecutionsQueryParams) BuildQuery() string {
params := url.Values{}
if q.NotificationId != "" {
params.Add("notificationId", q.NotificationId)
}
if q.SubscriberId != "" {
params.Add("subscriberId", q.SubscriberId)
}
return params.Encode()
}
60 changes: 60 additions & 0 deletions lib/executions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package lib_test

import (
"context"
"net/http"
"net/http/httptest"
"testing"

"github.com/novuhq/go-novu/lib"
)

var executionsGetResponse = `{
"data": [
{
"_id": "string",
"_organizationId": "string",
"_jobId": "string",
"_environmentId": "string",
"_notificationId": "string",
"_notificationTemplateId": "string",
"_subscriberId": "string",
"_messageId": "string",
"providerId": "string",
"transactionId": "string",
"channel": "in_app",
"detail": "string",
"source": "Credentials",
"status": "Success",
"isTest": true,
"isRetry": true,
"createdAt": "string"
}
]
}
`

func TestGetExecutions(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Errorf("Want GET, got %s", r.Method)
}
expected := "/v1/execution-details?notificationId=12345&subscriberId=XYZ"
if r.URL.String() != expected {
t.Errorf("Want %s, got %s", expected, r.URL.String())
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(executionsGetResponse))
}))
defer server.Close()
c := lib.NewAPIClient(novuApiKey, &lib.Config{BackendURL: lib.MustParseURL(server.URL)})

q := lib.ExecutionsQueryParams{NotificationId: "12345", SubscriberId: "XYZ"}
resp, err := c.ExecutionsApi.GetExecutions(context.Background(), q)
if err != nil {
t.Errorf("Error should be nil, got %v", err)
}
if resp.Data == nil || resp.Data == "" {
t.Error("Expected response, got none")
}
}
16 changes: 14 additions & 2 deletions lib/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,21 @@ type IAttachmentOptions struct {
Name string `json:"name,omitempty"`
Channels []ChannelType `json:"channels,omitempty"`
}
type JsonResponse struct {
Data interface{} `json:"data"`
}
type ExecutionsQueryParams struct {
NotificationId string
SubscriberId string
}

// QueryBuilder gives us an interface to pass as arg to our API methods.
// See messages.go for an example of implementing this interface
type QueryBuilder interface {
BuildQuery() string
}
type EventResponse struct {
Data interface{} `json:"data"`
JsonResponse
}

type EventRequest struct {
Expand All @@ -109,7 +121,7 @@ type EventRequest struct {
}

type SubscriberResponse struct {
Data interface{} `json:"data"`
JsonResponse
}

type SubscriberBulkCreateResponse struct {
Expand Down
2 changes: 2 additions & 0 deletions lib/novu.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type APIClient struct {
// Api Service
SubscriberApi *SubscriberService
EventApi *EventService
ExecutionsApi *ExecutionsService
TopicsApi *TopicService
IntegrationsApi *IntegrationService
InboundParserApi *InboundParserService
Expand All @@ -52,6 +53,7 @@ func NewAPIClient(apiKey string, cfg *Config) *APIClient {

// API Services
c.EventApi = (*EventService)(&c.common)
c.ExecutionsApi = (*ExecutionsService)(&c.common)
c.SubscriberApi = (*SubscriberService)(&c.common)
c.TopicsApi = (*TopicService)(&c.common)
c.IntegrationsApi = (*IntegrationService)(&c.common)
Expand Down
Loading