Skip to content

Commit

Permalink
Add notify command to resonate cli
Browse files Browse the repository at this point in the history
  • Loading branch information
avillega committed Jan 16, 2025
1 parent f320408 commit b7532e5
Show file tree
Hide file tree
Showing 5 changed files with 464 additions and 0 deletions.
58 changes: 58 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,64 @@ paths:
404:
description: Promise not found

# Notify
/notify:
post:
tags:
- Notify
summary: Create a Notification
operationId: createNotify
parameters:
- in: header
name: request-id
description: Unique tracking id
schema:
type: string
requestBody:
required: true
content:
application/json:
schema:
type: object
required:
- id
- promiseId
- timeout
- recv
properties:
id:
type: string
promiseId:
type: string
timeout:
type: integer
format: int64
recv:
$ref: "#/components/schemas/Recv"
responses:
200:
description: OK
content:
application/json:
schema:
type: object
properties:
promise:
$ref: "#/components/schemas/Promise"
201:
description: Created
content:
application/json:
schema:
type: object
properties:
callback:
$ref: "#/components/schemas/Callback"
promise:
$ref: "#/components/schemas/Promise"
404:
description: Promise not found

# Schedules
/schedules:
get:
Expand Down
96 changes: 96 additions & 0 deletions cmd/notify/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package notify

import (
"context"
"encoding/json"
"errors"
"strings"
"time"

"github.com/resonatehq/resonate/pkg/client"
v1 "github.com/resonatehq/resonate/pkg/client/v1"
"github.com/spf13/cobra"
)

var notifyExample = `
# Create a notify
resonate notify create foo --promise-id bar --timeout 1h --recv default
# Create a notify with url
resonate notify create foo --promise-id bar --timeout 1h --recv poll://default/6fa89b7e-4a56-40e8-ba4e-78864caa3278
# Create a notify with object
resonate notify create foo --promise-id bar --timeout 1h --recv {"type": "poll", "data": {"group": "default", "id": "6fa89b7e-4a56-40e8-ba4e-78864caa3278"}}
`

func CreateNotifyCmd(c client.Client) *cobra.Command {
var (
promiseId string
timeout time.Duration
recvStr string
)
cmd := &cobra.Command{
Use: "create <id>",
Short: "Create notify",
Example: notifyExample,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) != 1 {
return errors.New("must specify an id")
}

Check warning on line 39 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L26-L39

Added lines #L26 - L39 were not covered by tests

id := args[0]

var recv v1.Recv

if json.Valid([]byte(recvStr)) {
var recv0 v1.Recv0

if err := json.Unmarshal([]byte(recvStr), &recv0); err != nil {
return err
}
if err := recv.FromRecv0(recv0); err != nil {
return err
}
} else {
if err := recv.FromRecv1(recvStr); err != nil {
return err
}

Check warning on line 57 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L41-L57

Added lines #L41 - L57 were not covered by tests
}

body := v1.CreateNotifyJSONRequestBody{
Id: id,
PromiseId: promiseId,
Timeout: time.Now().Add(timeout).UnixMilli(),
Recv: recv,
}

res, err := c.V1().CreateNotifyWithResponse(context.TODO(), nil, body)
if err != nil {
return err
}

Check warning on line 70 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L60-L70

Added lines #L60 - L70 were not covered by tests

if res.StatusCode() == 201 {
cmd.Printf("Created callback: %s\n", id)
} else if res.StatusCode() == 200 {
if res.JSON200.Promise != nil && res.JSON200.Promise.State != v1.PromiseStatePENDING {
cmd.Printf("Promise %s already %s\n", promiseId, strings.ToLower(string(res.JSON200.Promise.State)))
} else {
cmd.Printf("Created callback: %s (deduplicated)\n", id)
}
} else {
cmd.PrintErrln(res.Status(), string(res.Body))
}

Check warning on line 82 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L72-L82

Added lines #L72 - L82 were not covered by tests

return nil

Check warning on line 84 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L84

Added line #L84 was not covered by tests
},
}

cmd.Flags().StringVar(&promiseId, "promise-id", "", "promise id")
cmd.Flags().DurationVar(&timeout, "timeout", 0, "task timeout")
cmd.Flags().StringVar(&recvStr, "recv", "default", "task receiver")

_ = cmd.MarkFlagRequired("promise-id")
_ = cmd.MarkFlagRequired("timeout")

return cmd

Check warning on line 95 in cmd/notify/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/create.go#L88-L95

Added lines #L88 - L95 were not covered by tests
}
41 changes: 41 additions & 0 deletions cmd/notify/notify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package notify

import (
"github.com/resonatehq/resonate/pkg/client"
"github.com/spf13/cobra"
)

func NewCmd() *cobra.Command {
var (
c = client.New()
server string
username string
password string
)

cmd := &cobra.Command{
Use: "notifications",
Aliases: []string{"notifications"},
Short: "Resonate notifications",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if username != "" || password != "" {
c.SetBasicAuth(username, password)
}

Check warning on line 26 in cmd/notify/notify.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/notify.go#L8-L26

Added lines #L8 - L26 were not covered by tests

return c.Setup(server)

Check warning on line 28 in cmd/notify/notify.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/notify.go#L28

Added line #L28 was not covered by tests
},
}

// Add subcommands
cmd.AddCommand(CreateNotifyCmd(c))

// Flags
cmd.PersistentFlags().StringVarP(&server, "server", "", "http://localhost:8001", "resonate url")
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "basic auth username")
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "basic auth password")

return cmd

Check warning on line 40 in cmd/notify/notify.go

View check run for this annotation

Codecov / codecov/patch

cmd/notify/notify.go#L33-L40

Added lines #L33 - L40 were not covered by tests
}
Loading

0 comments on commit b7532e5

Please sign in to comment.