Skip to content

Commit

Permalink
feat(SPV-848): adjust example to new event models
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-4chain committed Jun 28, 2024
1 parent 10760ad commit 59c6c72
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 5 additions & 5 deletions examples/webhooks/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func main() {
defer examples.HandlePanic()

client := walletclient.NewWithAdminKey("http://localhost:3003/v1", examples.ExampleAdminKey)
client := walletclient.NewWithAdminKey("http://localhost:3003/v1", "xprv9s21ZrQH143K2pmNeAHBzU4JHNDaFaPTbzKbBCw55ErhMDLsxDwKqcaDVV3PwmEmRZa9qUaU261iJaUx8eBiBF77zrPxTH8JGXC7LZQnsgA")
//"Authorization", "this-is-the-token", 3
wh := notifications.NewWebhook(
context.Background(),
Expand All @@ -33,16 +33,16 @@ func main() {

http.Handle("/notification", wh.HTTPHandler())

if err = notifications.RegisterHandler(wh, func(gpe *notifications.NumericEvent) {
if err = notifications.RegisterHandler(wh, func(gpe *notifications.StringEvent) {
time.Sleep(50 * time.Millisecond) // simulate processing time
fmt.Printf("Processing event-numeric: %d\n", gpe.Numeric)
fmt.Printf("Processing event-string: %s\n", gpe.Value)
}); err != nil {
panic(err)
}

if err = notifications.RegisterHandler(wh, func(gpe *notifications.StringEvent) {
if err = notifications.RegisterHandler(wh, func(gpe *notifications.TransactionEvent) {
time.Sleep(50 * time.Millisecond) // simulate processing time
fmt.Printf("Processing event-string: %s\n", gpe.Value)
fmt.Printf("Processing event-transaction: XPubID: %s, TxID: %s\n", gpe.XPubID, gpe.TransactionID)
}); err != nil {
panic(err)
}
Expand Down
17 changes: 13 additions & 4 deletions notifications/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,28 @@ func (nd *Webhook) process() {

////////////////////// BELOW it should be imported from spv-wallet models

// RawEvent - the base event type
type RawEvent struct {
Type string `json:"type"`
Content json.RawMessage `json:"content"`
}

// StringEvent - event with string value; can be used for generic messages and it's used for testing
type StringEvent struct {
Value string
Value string `json:"value"`
}

type NumericEvent struct {
Numeric int
type UserEvent struct {
XPubID string `json:"xpubId"`
}

type TransactionEvent struct {
UserEvent `json:",inline"`

TransactionID string `json:"transactionId"`
}

// Events - interface for all supported events
type Events interface {
StringEvent | NumericEvent
StringEvent | TransactionEvent
}

0 comments on commit 59c6c72

Please sign in to comment.