-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
76 lines (62 loc) · 1.55 KB
/
example_test.go
File metadata and controls
76 lines (62 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package posthook_test
import (
"context"
"fmt"
"io"
"net/http"
"time"
posthook "github.com/posthook/posthook-go"
)
func ExampleNewClient() {
client, err := posthook.NewClient("pk_...")
if err != nil {
panic(err)
}
_ = client // use client to schedule hooks, verify signatures, etc.
}
func ExampleHooksService_Schedule() {
client, err := posthook.NewClient("pk_...")
if err != nil {
panic(err)
}
hook, _, err := client.Hooks.Schedule(context.Background(), &posthook.HookScheduleParams{
Path: "/webhooks/user-created",
PostIn: "5m",
Data: map[string]any{"userId": "123"},
})
if err != nil {
panic(err)
}
fmt.Println(hook.ID)
}
func ExampleHooksService_Schedule_postAt() {
client, err := posthook.NewClient("pk_...")
if err != nil {
panic(err)
}
hook, _, err := client.Hooks.Schedule(context.Background(), &posthook.HookScheduleParams{
Path: "/webhooks/reminder",
PostAt: time.Now().Add(24 * time.Hour),
Data: map[string]any{"userId": "123"},
})
if err != nil {
panic(err)
}
fmt.Println(hook.ID)
}
func ExampleSignaturesService_ParseDelivery() {
client, err := posthook.NewClient("pk_...", posthook.WithSigningKey("ph_sk_..."))
if err != nil {
panic(err)
}
http.HandleFunc("/webhooks/test", func(w http.ResponseWriter, r *http.Request) {
body, _ := io.ReadAll(r.Body)
delivery, err := client.Signatures.ParseDelivery(body, r.Header)
if err != nil {
http.Error(w, "invalid signature", http.StatusUnauthorized)
return
}
fmt.Println(delivery.HookID, delivery.Path)
w.WriteHeader(http.StatusOK)
})
}