diff --git a/lib/mailgun/client.rb b/lib/mailgun/client.rb index a4b145e..0e84e78 100644 --- a/lib/mailgun/client.rb +++ b/lib/mailgun/client.rb @@ -185,7 +185,7 @@ def delete(resource_path, params = nil, body_params = false) request.body = params.to_json end else - @http_client.delete(resource_path, params: params) + @http_client.delete(resource_path, params) end else @http_client.delete(resource_path) diff --git a/lib/mailgun/webhooks/account_webhooks.rb b/lib/mailgun/webhooks/account_webhooks.rb new file mode 100644 index 0000000..017b88b --- /dev/null +++ b/lib/mailgun/webhooks/account_webhooks.rb @@ -0,0 +1,85 @@ +# frozen_string_literal: true + +module Mailgun + # A Mailgun::AccountWebhooks object is a simple CRUD interface to Account Mailgun Webhooks. + # Uses Mailgun + class AccountWebhooks + include ApiVersionChecker + + # Public creates a new Mailgun::Webhooks instance. + # Defaults to Mailgun::Client + def initialize(client = Mailgun::Client.new(Mailgun.api_key, Mailgun.api_host || 'api.mailgun.net', 'v1')) + @client = client + end + + # Public: List account-level webhooks + # + # webhook_ids - [String] Comma-separated list of webhook IDs to filter results. If specified, + # only webhooks with matching IDs will be returned. + # + # Retrieve all account-level webhooks or filter by specific webhook IDs. + # Returns webhook details including associated event types. + def list(webhook_ids = '') + res = @client.get('webhooks', webhook_ids: webhook_ids) + res.to_h['webhooks'] + end + + # Public: Create an account-level webhook + # options - [Hash] of + # description - [String] Description for the webhook + # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. + # Maximum of 3 unique URLs per event type. + # url - [String] URL for webhook to be sent to + # + # Returns the Unique identifier for the webhook + def create(_options = {}) + res = @client.post('webhooks', description: description, event_types: event_types, url: url) + res.to_h + end + + # Public: Delete account-level webhooks + # + # webhook_ids - [String] Comma-separated list of webhook IDs to delete. + # If provided, only these specific webhooks will be deleted. + # all - [Boolean] The required String of the webhook action to delete + # + # Returns a Boolean of the success + def remove_all(webhook_ids = nil, all: false) + @client.delete('webhooks', { webhook_ids: webhook_ids, all: all }.compact).status == 204 + end + + # Public: Get account-level webhook by ID + # + # webhook_id - [String] The webhook ID to retrieve + # + # Returns webhook details including associated event types. + def get(webhook_id) + res = @client.get("webhooks/#{webhook_id}") + res.to_h + end + + # Public: Update an account-level webhook + # + # options - [Hash] of + # description - [String] Description for the webhook + # event_types - [String] Event types to subscribe to. Use multiple times to specify multiple event types. + # Maximum of 3 unique URLs per event type. + # url - [String] URL for webhook to be sent to + # + # Returns a Boolean of the success + def update(webhook_id, options = {}) + @client.put("webhooks/#{webhook_id}", options).status == 204 + end + + # Public: Delete account-level webhook by ID + # + # webhook_id - [String] The webhook ID to delete + # + # Returns a Boolean of the success + def remove(webhook_id) + @client.delete("webhooks/#{webhook_id}").status == 204 + end + + enforces_api_version 'v1', :list, :create, :remove_all, :get, :update, :remove + end +end diff --git a/vcr_cassettes/domains.yml b/vcr_cassettes/domains.yml index 5d9d7b1..b84b659 100644 --- a/vcr_cassettes/domains.yml +++ b/vcr_cassettes/domains.yml @@ -1045,7 +1045,7 @@ http_interactions: recorded_at: Thu, 28 Aug 2025 12:45:35 GMT - request: method: delete - uri: https://api.mailgun.net/v1/dkim/keys?params=%7Bsigning_domain:%20%22integration-test.domain.invalid%22,%20selector:%20%22test%22%7D + uri: https://api.mailgun.net/v1/dkim/keys?selector=test&signing_domain=integration-test.domain.invalid body: encoding: US-ASCII string: ''