|
| 1 | +package admin |
| 2 | + |
| 3 | +// Enables you to manage webhook notification triggers for your Cloudinary product environment. |
| 4 | +// |
| 5 | +// https://cloudinary.com/documentation/notifications |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + |
| 10 | + "github.com/cloudinary/cloudinary-go/v2/api" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + triggersEndpoint api.EndPoint = "triggers" |
| 15 | +) |
| 16 | + |
| 17 | +// Trigger represents a single webhook notification trigger. |
| 18 | +type Trigger struct { |
| 19 | + ID string `json:"id,omitempty"` |
| 20 | + URI string `json:"uri,omitempty"` |
| 21 | + EventType string `json:"event_type,omitempty"` |
| 22 | + Additive bool `json:"additive"` |
| 23 | + Filter map[string]any `json:"filter,omitempty"` |
| 24 | + FilterLanguage string `json:"filter_language,omitempty"` |
| 25 | + PayloadTemplate map[string]any `json:"payload_template,omitempty"` |
| 26 | + AuthScheme string `json:"auth_scheme,omitempty"` |
| 27 | + ProductEnvironmentID string `json:"product_environment_id,omitempty"` |
| 28 | + URIType string `json:"uri_type,omitempty"` |
| 29 | + CreatedAt string `json:"created_at,omitempty"` |
| 30 | + UpdatedAt string `json:"updated_at,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +// ListTriggersParams are the parameters for ListTriggers. |
| 34 | +type ListTriggersParams struct{} |
| 35 | + |
| 36 | +// ListTriggersResult is the result of ListTriggers. |
| 37 | +type ListTriggersResult struct { |
| 38 | + Triggers []Trigger `json:"triggers"` |
| 39 | + Error api.ErrorResp `json:"error,omitempty"` |
| 40 | +} |
| 41 | + |
| 42 | +// ListTriggers lists all webhook notification triggers for the product environment. |
| 43 | +func (a *API) ListTriggers(ctx context.Context, params ListTriggersParams) (*ListTriggersResult, error) { |
| 44 | + res := &ListTriggersResult{} |
| 45 | + _, err := a.get(ctx, triggersEndpoint, params, res) |
| 46 | + return res, err |
| 47 | +} |
| 48 | + |
| 49 | +// GetTriggerParams are the parameters for GetTrigger. |
| 50 | +type GetTriggerParams struct { |
| 51 | + TriggerID string `json:"-"` |
| 52 | +} |
| 53 | + |
| 54 | +// GetTriggerResult is the result of GetTrigger. |
| 55 | +type GetTriggerResult struct { |
| 56 | + Trigger |
| 57 | + Error api.ErrorResp `json:"error,omitempty"` |
| 58 | +} |
| 59 | + |
| 60 | +// GetTrigger retrieves a single webhook notification trigger by its ID. |
| 61 | +// Cloudinary does not expose a single-resource GET endpoint, so this is |
| 62 | +// implemented as ListTriggers filtered to the matching ID. |
| 63 | +func (a *API) GetTrigger(ctx context.Context, params GetTriggerParams) (*GetTriggerResult, error) { |
| 64 | + list, err := a.ListTriggers(ctx, ListTriggersParams{}) |
| 65 | + if err != nil { |
| 66 | + return nil, err |
| 67 | + } |
| 68 | + if list.Error.Message != "" { |
| 69 | + return &GetTriggerResult{Error: list.Error}, nil |
| 70 | + } |
| 71 | + for _, t := range list.Triggers { |
| 72 | + if t.ID == params.TriggerID { |
| 73 | + return &GetTriggerResult{Trigger: t}, nil |
| 74 | + } |
| 75 | + } |
| 76 | + // Not found — return empty result with no error; caller checks ID == "". |
| 77 | + return &GetTriggerResult{}, nil |
| 78 | +} |
| 79 | + |
| 80 | +// CreateTriggerParams are the parameters for CreateTrigger. |
| 81 | +type CreateTriggerParams struct { |
| 82 | + URI string `json:"uri"` |
| 83 | + EventType string `json:"event_type"` |
| 84 | + Additive bool `json:"additive,omitempty"` |
| 85 | + Filter map[string]any `json:"filter,omitempty"` |
| 86 | + FilterLanguage string `json:"filter_language,omitempty"` |
| 87 | + PayloadTemplate map[string]any `json:"payload_template,omitempty"` |
| 88 | + AuthScheme string `json:"auth_scheme,omitempty"` |
| 89 | +} |
| 90 | + |
| 91 | +// CreateTriggerResult is the result of CreateTrigger. |
| 92 | +type CreateTriggerResult struct { |
| 93 | + Trigger |
| 94 | + Error api.ErrorResp `json:"error,omitempty"` |
| 95 | +} |
| 96 | + |
| 97 | +// CreateTrigger creates a new webhook notification trigger. |
| 98 | +func (a *API) CreateTrigger(ctx context.Context, params CreateTriggerParams) (*CreateTriggerResult, error) { |
| 99 | + res := &CreateTriggerResult{} |
| 100 | + _, err := a.post(ctx, triggersEndpoint, params, res) |
| 101 | + return res, err |
| 102 | +} |
| 103 | + |
| 104 | +// UpdateTriggerParams are the parameters for UpdateTrigger. |
| 105 | +type UpdateTriggerParams struct { |
| 106 | + TriggerID string `json:"-"` |
| 107 | + URI string `json:"uri,omitempty"` |
| 108 | + EventType string `json:"event_type,omitempty"` |
| 109 | + Additive *bool `json:"additive,omitempty"` |
| 110 | + Filter map[string]any `json:"filter,omitempty"` |
| 111 | + FilterLanguage string `json:"filter_language,omitempty"` |
| 112 | + PayloadTemplate map[string]any `json:"payload_template,omitempty"` |
| 113 | + AuthScheme string `json:"auth_scheme,omitempty"` |
| 114 | +} |
| 115 | + |
| 116 | +// UpdateTriggerResult is the result of UpdateTrigger. |
| 117 | +type UpdateTriggerResult struct { |
| 118 | + Trigger |
| 119 | + Error api.ErrorResp `json:"error,omitempty"` |
| 120 | +} |
| 121 | + |
| 122 | +// UpdateTrigger updates an existing webhook notification trigger. |
| 123 | +func (a *API) UpdateTrigger(ctx context.Context, params UpdateTriggerParams) (*UpdateTriggerResult, error) { |
| 124 | + res := &UpdateTriggerResult{} |
| 125 | + _, err := a.put(ctx, api.BuildPath(triggersEndpoint, params.TriggerID), params, res) |
| 126 | + return res, err |
| 127 | +} |
| 128 | + |
| 129 | +// DeleteTriggerParams are the parameters for DeleteTrigger. |
| 130 | +type DeleteTriggerParams struct { |
| 131 | + TriggerID string `json:"-"` |
| 132 | +} |
| 133 | + |
| 134 | +// DeleteTriggerResult is the result of DeleteTrigger. |
| 135 | +type DeleteTriggerResult struct { |
| 136 | + Message string `json:"message,omitempty"` |
| 137 | + Error api.ErrorResp `json:"error,omitempty"` |
| 138 | +} |
| 139 | + |
| 140 | +// DeleteTrigger deletes a webhook notification trigger by its ID. |
| 141 | +func (a *API) DeleteTrigger(ctx context.Context, params DeleteTriggerParams) (*DeleteTriggerResult, error) { |
| 142 | + res := &DeleteTriggerResult{} |
| 143 | + _, err := a.delete(ctx, api.BuildPath(triggersEndpoint, params.TriggerID), params, res) |
| 144 | + return res, err |
| 145 | +} |
0 commit comments