package api import ( "atlas9.dev/c/core" "atlas9.dev/c/demo/lib/tasks" "atlas9.dev/c/demo/lib/webhooks" ) const ( Path_Webhooks_CreateEndpoint = "POST /Webhooks_CreateEndpoint" Path_Webhooks_UpdateEndpoint = "POST /Webhooks_UpdateEndpoint" Path_Webhooks_GetEndpoint = "POST /Webhooks_GetEndpoint" Path_Webhooks_ListEndpoints = "POST /Webhooks_ListEndpoints" Path_Webhooks_DeleteEndpoint = "POST /Webhooks_DeleteEndpoint" // Path_Webhooks_Deliver is the internal task endpoint the delivery worker // replays; it is not called by clients. Path_Webhooks_Deliver = "POST /Webhooks_Deliver" ) type Webhooks_CreateEndpointReq struct { Endpoint webhooks.Endpoint } type Webhooks_CreateEndpointRes struct { Endpoint webhooks.Endpoint // Secret is the plaintext signing secret, returned only here. Store it to // verify the X-Webhook-Signature header on delivered requests; it cannot be // retrieved again. Secret string } type Webhooks_UpdateEndpointReq struct { Endpoint webhooks.Endpoint } type Webhooks_UpdateEndpointRes struct { Endpoint webhooks.Endpoint } type Webhooks_GetEndpointReq struct { Tenant core.ID ID core.ID } type Webhooks_GetEndpointRes struct { Endpoint webhooks.Endpoint } type Webhooks_ListEndpointsReq struct { Tenant core.ID Page core.PageReq } type Webhooks_ListEndpointsRes struct { Page core.Page[webhooks.Endpoint] } type Webhooks_DeleteEndpointReq struct { Tenant core.ID ID core.ID } type Webhooks_DeleteEndpointRes struct{} // Webhooks_DeliverReq is one queued delivery: the endpoint to POST to and the // event body to sign and send. The body is carried in the task payload rather // than looked up, since there is no event table. type Webhooks_DeliverReq struct { Tenant core.ID Endpoint core.ID Type string Body []byte } type Webhooks_DeliverRes struct { Task tasks.Result } // Prepare functions func (r *Webhooks_CreateEndpointReq) Prepare() error { r.Endpoint.ID = core.NewID("whk") // New endpoints are active; deactivate later via Update. The secret is // generated and sealed server-side, so any client-supplied secret is ignored. r.Endpoint.Active = true r.Endpoint.DekID = core.ID{} r.Endpoint.SecretEnc = nil var errs []string if r.Endpoint.Name == "" { errs = append(errs, "endpoint name is required") } if r.Endpoint.URL == "" { errs = append(errs, "endpoint url is required") } if len(errs) > 0 { return &ValidationError{Errors: errs} } return nil } func (r *Webhooks_UpdateEndpointReq) Prepare() error { var errs []string if r.Endpoint.ID.IsEmpty() { errs = append(errs, "endpoint id is required") } if r.Endpoint.Name == "" { errs = append(errs, "endpoint name is required") } if r.Endpoint.URL == "" { errs = append(errs, "endpoint url is required") } if len(errs) > 0 { return &ValidationError{Errors: errs} } return nil } func (r *Webhooks_GetEndpointReq) Prepare() error { return nil } func (r *Webhooks_DeleteEndpointReq) Prepare() error { return nil } func (r *Webhooks_DeliverReq) Prepare() error { return nil } func (r *Webhooks_ListEndpointsReq) Prepare() error { if r.Tenant.IsEmpty() { return &ValidationError{Errors: []string{"Tenant is required"}} } return nil }