|
| 1 | +package planetscale |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "path" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +var _ WebhooksService = &webhooksService{} |
| 11 | + |
| 12 | +// WebhooksService is an interface for communicating with the PlanetScale |
| 13 | +// Webhooks API. |
| 14 | +type WebhooksService interface { |
| 15 | + List(context.Context, *ListWebhooksRequest, ...ListOption) ([]*Webhook, error) |
| 16 | + Create(context.Context, *CreateWebhookRequest) (*Webhook, error) |
| 17 | + Get(context.Context, *GetWebhookRequest) (*Webhook, error) |
| 18 | + Update(context.Context, *UpdateWebhookRequest) (*Webhook, error) |
| 19 | + Delete(context.Context, *DeleteWebhookRequest) error |
| 20 | + Test(context.Context, *TestWebhookRequest) error |
| 21 | +} |
| 22 | + |
| 23 | +type webhooksResponse struct { |
| 24 | + Webhooks []*Webhook `json:"data"` |
| 25 | +} |
| 26 | + |
| 27 | +// Webhook represents a PlanetScale webhook. |
| 28 | +type Webhook struct { |
| 29 | + ID string `json:"id"` |
| 30 | + URL string `json:"url"` |
| 31 | + Secret string `json:"secret"` |
| 32 | + Enabled bool `json:"enabled"` |
| 33 | + LastSentResult string `json:"last_sent_result"` |
| 34 | + LastSentSuccess bool `json:"last_sent_success"` |
| 35 | + LastSentAt time.Time `json:"last_sent_at"` |
| 36 | + CreatedAt time.Time `json:"created_at"` |
| 37 | + UpdatedAt time.Time `json:"updated_at"` |
| 38 | + Events []string `json:"events"` |
| 39 | +} |
| 40 | + |
| 41 | +// ListWebhooksRequest is the request for listing webhooks. |
| 42 | +type ListWebhooksRequest struct { |
| 43 | + Organization string `json:"-"` |
| 44 | + Database string `json:"-"` |
| 45 | +} |
| 46 | + |
| 47 | +// CreateWebhookRequest is the request for creating a webhook. |
| 48 | +type CreateWebhookRequest struct { |
| 49 | + Organization string `json:"-"` |
| 50 | + Database string `json:"-"` |
| 51 | + URL string `json:"url"` |
| 52 | + Enabled *bool `json:"enabled,omitempty"` |
| 53 | + Events []string `json:"events,omitempty"` |
| 54 | +} |
| 55 | + |
| 56 | +// GetWebhookRequest is the request for getting a webhook. |
| 57 | +type GetWebhookRequest struct { |
| 58 | + Organization string `json:"-"` |
| 59 | + Database string `json:"-"` |
| 60 | + ID string `json:"-"` |
| 61 | +} |
| 62 | + |
| 63 | +// UpdateWebhookRequest is the request for updating a webhook. |
| 64 | +type UpdateWebhookRequest struct { |
| 65 | + Organization string `json:"-"` |
| 66 | + Database string `json:"-"` |
| 67 | + ID string `json:"-"` |
| 68 | + URL *string `json:"url,omitempty"` |
| 69 | + Enabled *bool `json:"enabled,omitempty"` |
| 70 | + Events []string `json:"events,omitempty"` |
| 71 | +} |
| 72 | + |
| 73 | +// DeleteWebhookRequest is the request for deleting a webhook. |
| 74 | +type DeleteWebhookRequest struct { |
| 75 | + Organization string `json:"-"` |
| 76 | + Database string `json:"-"` |
| 77 | + ID string `json:"-"` |
| 78 | +} |
| 79 | + |
| 80 | +// TestWebhookRequest is the request for testing a webhook. |
| 81 | +type TestWebhookRequest struct { |
| 82 | + Organization string `json:"-"` |
| 83 | + Database string `json:"-"` |
| 84 | + ID string `json:"-"` |
| 85 | +} |
| 86 | + |
| 87 | +type webhooksService struct { |
| 88 | + client *Client |
| 89 | +} |
| 90 | + |
| 91 | +func (w *webhooksService) List(ctx context.Context, listReq *ListWebhooksRequest, opts ...ListOption) ([]*Webhook, error) { |
| 92 | + listOpts := defaultListOptions(opts...) |
| 93 | + |
| 94 | + req, err := w.client.newRequest(http.MethodGet, webhooksAPIPath(listReq.Organization, listReq.Database), nil, WithQueryParams(*listOpts.URLValues)) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + |
| 99 | + resp := &webhooksResponse{} |
| 100 | + if err := w.client.do(ctx, req, &resp); err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + return resp.Webhooks, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (w *webhooksService) Create(ctx context.Context, createReq *CreateWebhookRequest) (*Webhook, error) { |
| 108 | + req, err := w.client.newRequest(http.MethodPost, webhooksAPIPath(createReq.Organization, createReq.Database), createReq) |
| 109 | + if err != nil { |
| 110 | + return nil, err |
| 111 | + } |
| 112 | + |
| 113 | + webhook := &Webhook{} |
| 114 | + if err := w.client.do(ctx, req, &webhook); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return webhook, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (w *webhooksService) Get(ctx context.Context, getReq *GetWebhookRequest) (*Webhook, error) { |
| 122 | + req, err := w.client.newRequest(http.MethodGet, webhookAPIPath(getReq.Organization, getReq.Database, getReq.ID), nil) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + webhook := &Webhook{} |
| 128 | + if err := w.client.do(ctx, req, &webhook); err != nil { |
| 129 | + return nil, err |
| 130 | + } |
| 131 | + |
| 132 | + return webhook, nil |
| 133 | +} |
| 134 | + |
| 135 | +func (w *webhooksService) Update(ctx context.Context, updateReq *UpdateWebhookRequest) (*Webhook, error) { |
| 136 | + req, err := w.client.newRequest(http.MethodPatch, webhookAPIPath(updateReq.Organization, updateReq.Database, updateReq.ID), updateReq) |
| 137 | + if err != nil { |
| 138 | + return nil, err |
| 139 | + } |
| 140 | + |
| 141 | + webhook := &Webhook{} |
| 142 | + if err := w.client.do(ctx, req, &webhook); err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + |
| 146 | + return webhook, nil |
| 147 | +} |
| 148 | + |
| 149 | +func (w *webhooksService) Delete(ctx context.Context, deleteReq *DeleteWebhookRequest) error { |
| 150 | + req, err := w.client.newRequest(http.MethodDelete, webhookAPIPath(deleteReq.Organization, deleteReq.Database, deleteReq.ID), nil) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + |
| 155 | + return w.client.do(ctx, req, nil) |
| 156 | +} |
| 157 | + |
| 158 | +func (w *webhooksService) Test(ctx context.Context, testReq *TestWebhookRequest) error { |
| 159 | + req, err := w.client.newRequest(http.MethodPost, webhookTestAPIPath(testReq.Organization, testReq.Database, testReq.ID), nil) |
| 160 | + if err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + return w.client.do(ctx, req, nil) |
| 165 | +} |
| 166 | + |
| 167 | +func webhooksAPIPath(org, db string) string { |
| 168 | + return path.Join("v1/organizations", org, "databases", db, "webhooks") |
| 169 | +} |
| 170 | + |
| 171 | +func webhookAPIPath(org, db, id string) string { |
| 172 | + return path.Join(webhooksAPIPath(org, db), id) |
| 173 | +} |
| 174 | + |
| 175 | +func webhookTestAPIPath(org, db, id string) string { |
| 176 | + return path.Join(webhookAPIPath(org, db, id), "test") |
| 177 | +} |
| 178 | + |
0 commit comments