// SPDX-License-Identifier: AGPL-3.0-or-later package httpapi import ( "context" "net/http" "docwell/server/internal/access" "docwell/server/internal/directory" "docwell/server/internal/webhooks" ) // WebhookService is the part of directory.Service for workspace webhooks. type WebhookService interface { ListWebhooks(ctx context.Context, c access.Caller, wsID string) ([]directory.Webhook, error) CreateWebhook(ctx context.Context, c access.Caller, wsID string, in directory.WebhookInput) (directory.Webhook, error) UpdateWebhook(ctx context.Context, c access.Caller, id string, in directory.WebhookInput) (directory.Webhook, error) DeleteWebhook(ctx context.Context, c access.Caller, id string) error WebhookDeliveries(ctx context.Context, c access.Caller, id string) ([]webhooks.Delivery, error) TestWebhook(ctx context.Context, c access.Caller, id string) (webhooks.Delivery, error) } func (a *api) webhookRoutes(mux *routeMux) { mux.HandleFunc("GET /api/v1/workspaces/{id}/webhooks", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.ListWebhooks(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/workspaces/{id}/webhooks", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in directory.WebhookInput if !a.decode(w, r, &in) { return } h, err := a.dir.CreateWebhook(r.Context(), caller(r), r.PathValue("id"), in) a.respondStatus(w, http.StatusCreated, h, err) })) mux.HandleFunc("PATCH /api/v1/webhooks/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { var in directory.WebhookInput if !a.decode(w, r, &in) { return } h, err := a.dir.UpdateWebhook(r.Context(), caller(r), r.PathValue("id"), in) a.respond(w, h, err) })) mux.HandleFunc("DELETE /api/v1/webhooks/{id}", a.requireUser(func(w http.ResponseWriter, r *http.Request) { a.respondNoContent(w, a.dir.DeleteWebhook(r.Context(), caller(r), r.PathValue("id"))) })) mux.HandleFunc("GET /api/v1/webhooks/{id}/deliveries", a.requireUser(func(w http.ResponseWriter, r *http.Request) { list, err := a.dir.WebhookDeliveries(r.Context(), caller(r), r.PathValue("id")) a.respond(w, list, err) })) mux.HandleFunc("POST /api/v1/webhooks/{id}/test", a.requireUser(func(w http.ResponseWriter, r *http.Request) { d, err := a.dir.TestWebhook(r.Context(), caller(r), r.PathValue("id")) a.respond(w, d, err) })) }