Skip to content

feat(webhooks): v2 #1595

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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions components/fctl/cmd/webhooks/attempts/abort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package attempts

import (


fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type AbortStore struct {
ErrorResponse error `json:"error"`
Success bool `json:"success"`
}
type AbortController struct {
store *AbortStore
}

func (c *AbortController) GetStore() *AbortStore {
return c.store
}

var _ fctl.Controller[*AbortStore] = (*AbortController)(nil)


func (c *AbortController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())

request := operations.AbortWaitingAttemptRequest{
AttemptID: args[0],
}

if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to abort an Attempt") {
return nil, fctl.ErrMissingApproval
}

_ , err := store.Client().Webhooks.AbortWaitingAttempt(cmd.Context(), request)
if err!= nil {
c.store.ErrorResponse = err
} else {
c.store.Success = true
}

return c, nil
}

func (c *AbortController) Render(cmd *cobra.Command, args []string) error {


if c.store.ErrorResponse != nil {
pterm.Warning.WithShowLineNumber(false).Printfln(c.store.ErrorResponse.Error())
return nil
}


pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Attempt abort successfully")

return nil
}

func NewAbortController() *AbortController {
return &AbortController{
store: &AbortStore{},
}
}

func NewAbortCommand() *cobra.Command {

return fctl.NewCommand("abort <attempt-id>",
fctl.WithShortDescription("Abort a waiting Attempt"),
fctl.WithAliases("abrt", "ab"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithConfirmFlag(),
fctl.WithController[*AbortStore](NewAbortController()),
)
}
110 changes: 110 additions & 0 deletions components/fctl/cmd/webhooks/attempts/list_aborted.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package attempts

import (
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/fctl/pkg/printer"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type ListAbortedStore struct {
Cursor shared.V2AttemptCursorResponseCursor `json:"attempts"`
ErrorResponse error `json:"error"`
}
type ListAbortedController struct {
store *ListAbortedStore
cursorFlag string
}

func (c *ListAbortedController) GetStore() *ListAbortedStore {
return c.store
}

var _ fctl.Controller[*ListAbortedStore] = (*ListAbortedController)(nil)


func (c *ListAbortedController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
cursor := fctl.GetString(cmd, c.cursorFlag)


request := operations.GetAbortedAttemptsRequest{
Cursor: &cursor,
}

response, err := store.Client().Webhooks.GetAbortedAttempts(cmd.Context(), request)
if err!= nil {
c.store.ErrorResponse = err
} else {
c.store.Cursor = response.V2AttemptCursorResponse.Cursor
}

return c, nil
}

func (c *ListAbortedController) Render(cmd *cobra.Command, args []string) error {


if c.store.ErrorResponse != nil {
pterm.Warning.WithShowLineNumber(false).Printfln(c.store.ErrorResponse.Error())
return nil
}

tableData := fctl.Map(c.store.Cursor.Data, func(attempt shared.V2Attempt) []string{

return []string{
attempt.ID,
string(attempt.Status),
attempt.HookName,
attempt.HookID,
attempt.HookEndpoint,
attempt.Event,
attempt.DateOccured.Format(time.RFC3339),
attempt.NextRetryAfter.Format(time.RFC3339),
attempt.Payload,
}

})

tableData = fctl.Prepend(tableData, []string{"ID", "Status", "Hook Name", "Hook ID", "Hook Endpoint", "Event", "Created At", "Next Try", "Payload"})

tableData = printer.AddCursorRowsToTable(tableData, printer.CursorArgs{
HasMore : c.store.Cursor.HasMore,
Next: &c.store.Cursor.Next,
PageSize: c.store.Cursor.PageSize,
Previous: &c.store.Cursor.Previous,
})


writer := cmd.OutOrStdout()

return pterm.DefaultTable.
WithHasHeader().
WithWriter(writer).
WithData(tableData).
Render()
}

func NewListAbortedController() *ListAbortedController {
return &ListAbortedController{
store: &ListAbortedStore{},
cursorFlag: "cursor",
}
}

func NewListAbortedCommand() *cobra.Command {

c := NewListAbortedController()

return fctl.NewCommand("list-aborted",
fctl.WithShortDescription("List all aborted attempts"),
fctl.WithAliases("lsw", "lw"),
fctl.WithStringFlag(c.cursorFlag, "", "Cursor pagination"),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithController[*ListAbortedStore](NewListAbortedController()),
)
}
112 changes: 112 additions & 0 deletions components/fctl/cmd/webhooks/attempts/list_waiting.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package attempts

import (
"strconv"
"time"

fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/fctl/pkg/printer"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/shared"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type ListWaitingStore struct {
Cursor shared.V2AttemptCursorResponseCursor `json:"attempts"`
ErrorResponse error `json:"error"`

}
type ListWaitingController struct {
store *ListWaitingStore
cursorFlag string
}

func (c *ListWaitingController) GetStore() *ListWaitingStore {
return c.store
}

var _ fctl.Controller[*ListWaitingStore] = (*ListWaitingController)(nil)


func (c *ListWaitingController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())
cursor := fctl.GetString(cmd, c.cursorFlag)


request := operations.GetWaitingAttemptsRequest{
Cursor: &cursor,
}

response, err := store.Client().Webhooks.GetWaitingAttempts(cmd.Context(), request)
if err!= nil {
c.store.ErrorResponse = err
} else {
c.store.Cursor = response.V2AttemptCursorResponse.Cursor
}
return c, nil
}

func (c *ListWaitingController) Render(cmd *cobra.Command, args []string) error {


if c.store.ErrorResponse != nil {
pterm.Warning.WithShowLineNumber(false).Printfln(c.store.ErrorResponse.Error())
return nil
}


tableData := fctl.Map(c.store.Cursor.Data, func(attempt shared.V2Attempt) []string{

return []string{
attempt.ID,
string(attempt.Status),
strconv.FormatInt(attempt.StatusCode, 10),
attempt.HookName,
attempt.HookID,
attempt.HookEndpoint,
attempt.Event,
attempt.DateOccured.Format(time.RFC3339),
attempt.NextRetryAfter.Format(time.RFC3339),
attempt.Payload,
}

})

tableData = fctl.Prepend(tableData, []string{"ID", "Status", "Last Status Code", "Hook Name", "Hook ID", "Hook Endpoint", "Event", "Created At", "Next Try", "Payload"})

tableData = printer.AddCursorRowsToTable(tableData, printer.CursorArgs{
HasMore : c.store.Cursor.HasMore,
Next: &c.store.Cursor.Next,
PageSize: c.store.Cursor.PageSize,
Previous: &c.store.Cursor.Previous,
})


writer := cmd.OutOrStdout()

return pterm.DefaultTable.
WithHasHeader().
WithWriter(writer).
WithData(tableData).
Render()
}

func NewListWaitingController() *ListWaitingController {
return &ListWaitingController{
store: &ListWaitingStore{},
cursorFlag: "cursor",
}
}

func NewListWaitingCommand() *cobra.Command {
c := NewListWaitingController()

return fctl.NewCommand("list-waiting",
fctl.WithShortDescription("List all waiting attempts"),
fctl.WithAliases("lsw", "lw"),
fctl.WithStringFlag(c.cursorFlag, "", "Cursor pagination"),
fctl.WithArgs(cobra.ExactArgs(0)),
fctl.WithController[*ListWaitingStore](NewListWaitingController()),
)
}
76 changes: 76 additions & 0 deletions components/fctl/cmd/webhooks/attempts/retry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package attempts

import (


fctl "github.com/formancehq/fctl/pkg"
"github.com/formancehq/formance-sdk-go/v2/pkg/models/operations"
"github.com/pterm/pterm"
"github.com/spf13/cobra"
)

type RetryStore struct {
ErrorResponse error `json:"error"`
Success bool `json:"success"`
}
type RetryController struct {
store *RetryStore
}

func (c *RetryController) GetStore() *RetryStore {
return c.store
}

var _ fctl.Controller[*RetryStore] = (*RetryController)(nil)


func (c *RetryController) Run(cmd *cobra.Command, args []string) (fctl.Renderable, error) {
store := fctl.GetStackStore(cmd.Context())

request := operations.RetryWaitingAttemptRequest{
AttemptID: args[0],
}

if !fctl.CheckStackApprobation(cmd, store.Stack(), "You are about to Retry an Attempt") {
return nil, fctl.ErrMissingApproval
}

_ , err := store.Client().Webhooks.RetryWaitingAttempt(cmd.Context(), request)

if err!= nil {
c.store.ErrorResponse = err
} else {
c.store.Success = true
}

return c, nil
}

func (c *RetryController) Render(cmd *cobra.Command, args []string) error {

if c.store.ErrorResponse != nil {
pterm.Warning.WithShowLineNumber(false).Printfln(c.store.ErrorResponse.Error())
return nil
}

pterm.Success.WithWriter(cmd.OutOrStdout()).Printfln("Attempt Retry successfully")

return nil
}

func NewRetryController() *RetryController {
return &RetryController{
store: &RetryStore{},
}
}

func NewRetryCommand() *cobra.Command {

return fctl.NewCommand("retry <attempt-id>",
fctl.WithShortDescription("Retry a waiting Attempt"),
fctl.WithAliases("rtry", "rty"),
fctl.WithArgs(cobra.ExactArgs(1)),
fctl.WithConfirmFlag(),
fctl.WithController[*RetryStore](NewRetryController()),
)
}
Loading