Class: ShopifyAPI::Webhooks::Registry
- Inherits:
-
Object
- Object
- ShopifyAPI::Webhooks::Registry
- Extended by:
- T::Sig
- Defined in:
- lib/shopify_api/webhooks/registry.rb
Constant Summary collapse
- MANDATORY_TOPICS =
T.let([ "shop/redact", "customers/redact", "customers/data_request", ].freeze, T::Array[String])
Class Method Summary collapse
- .add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, filter: nil, metafield_namespaces: nil) ⇒ Object
- .clear ⇒ Object
- .get_webhook_id(topic:, client:) ⇒ Object
- .mandatory_registration_result(topic) ⇒ Object
- .process(request) ⇒ Object
- .register(topic:, session:) ⇒ Object
- .register_all(session:) ⇒ Object
- .unregister(topic:, session:) ⇒ Object
Class Method Details
.add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, filter: nil, metafield_namespaces: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/shopify_api/webhooks/registry.rb', line 25 def add_registration(topic:, delivery_method:, path:, handler: nil, fields: nil, filter: nil, metafield_namespaces: nil) @registry[topic] = case delivery_method when :pub_sub Registrations::PubSub.new(topic: topic, path: path, fields: fields, metafield_namespaces: , filter: filter) when :event_bridge Registrations::EventBridge.new(topic: topic, path: path, fields: fields, metafield_namespaces: , filter: filter) when :http unless handler raise Errors::InvalidWebhookRegistrationError, "Cannot create an Http registration without a handler." end Registrations::Http.new(topic: topic, path: path, handler: handler, fields: fields, metafield_namespaces: , filter: filter) else raise Errors::InvalidWebhookRegistrationError, "Unsupported delivery method #{delivery_method}. Allowed values: {:http, :pub_sub, :event_bridge}." end end |
.clear ⇒ Object
48 49 50 |
# File 'lib/shopify_api/webhooks/registry.rb', line 48 def clear @registry.clear end |
.get_webhook_id(topic:, client:) ⇒ Object
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/shopify_api/webhooks/registry.rb', line 162 def get_webhook_id(topic:, client:) fetch_id_query = <<~QUERY { webhookSubscriptions(first: 1, topics: #{topic.gsub(%r{/|\.}, "_").upcase}) { edges { node { id } } } } QUERY fetch_id_response = client.query(query: fetch_id_query, response_as_struct: false) raise Errors::WebhookRegistrationError, "Failed to fetch webhook from Shopify" unless fetch_id_response.ok? body = T.cast(fetch_id_response.body, T::Hash[String, T.untyped]) errors = body["errors"] || {} raise Errors::WebhookRegistrationError, "Failed to fetch webhook from Shopify: #{errors[0]["message"]}" unless errors.empty? edges = body.dig("data", "webhookSubscriptions", "edges") || {} return nil if edges.empty? edges[0]["node"]["id"].to_s end |
.mandatory_registration_result(topic) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/shopify_api/webhooks/registry.rb', line 91 def mandatory_registration_result(topic) RegisterResult.new( topic: topic, success: false, body: "Mandatory webhooks are to be registered in the partners dashboard", ) end |
.process(request) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/shopify_api/webhooks/registry.rb', line 189 def process(request) raise Errors::InvalidWebhookError, "Invalid webhook HMAC." unless Utils::HmacValidator.validate(request) handler = @registry[request.topic]&.handler unless handler raise Errors::NoWebhookHandler, "No webhook handler found for topic: #{request.topic}." end if handler.is_a?(WebhookHandler) handler.handle(data: WebhookMetadata.new(topic: request.topic, shop: request.shop, body: request.parsed_body, api_version: request.api_version, webhook_id: request.webhook_id)) else handler.handle(topic: request.topic, shop: request.shop, body: request.parsed_body) warning = <<~WARNING DEPRECATED: Use ShopifyAPI::Webhooks::WebhookHandler#handle instead of ShopifyAPI::Webhooks::Handler#handle. https://github.com/Shopify/shopify-api-ruby/blob/main/docs/usage/webhooks.md#create-a-webhook-handler WARNING ShopifyAPI::Logger.deprecated(warning, "15.0.0") end end |
.register(topic:, session:) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/shopify_api/webhooks/registry.rb', line 58 def register(topic:, session:) return mandatory_registration_result(topic) if mandatory_webhook_topic?(topic) registration = @registry[topic] unless registration raise Errors::InvalidWebhookRegistrationError, "Webhook topic #{topic} has not been added to the registry." end client = Clients::Graphql::Admin.new(session: session) register_check_result = webhook_registration_needed?(client, registration) registered = true register_body = nil if register_check_result[:must_register] register_body = send_register_request( client, registration, register_check_result[:webhook_id], ) registered = registration_sucessful?( register_body, registration.mutation_name(register_check_result[:webhook_id]), ) end RegisterResult.new(topic: topic, success: registered, body: register_body) end |
.register_all(session:) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/shopify_api/webhooks/registry.rb', line 104 def register_all(session:) topics = @registry.keys result = T.let([], T::Array[RegisterResult]) topics.each do |topic| register_response = register( topic: topic, session: session, ) result.push(register_response) end result end |
.unregister(topic:, session:) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/shopify_api/webhooks/registry.rb', line 123 def unregister(topic:, session:) return { "response": nil } if mandatory_webhook_topic?(topic) client = Clients::Graphql::Admin.new(session: session) webhook_id = get_webhook_id(topic: topic, client: client) return {} if webhook_id.nil? delete_mutation = <<~MUTATION mutation webhookSubscription { webhookSubscriptionDelete(id: "#{webhook_id}") { userErrors { field message } deletedWebhookSubscriptionId } } MUTATION delete_response = client.query(query: delete_mutation, response_as_struct: false) raise Errors::WebhookRegistrationError, "Failed to delete webhook from Shopify" unless delete_response.ok? result = T.cast(delete_response.body, T::Hash[String, T.untyped]) errors = result["errors"] || {} raise Errors::WebhookRegistrationError, "Failed to delete webhook from Shopify: #{errors[0]["message"]}" unless errors.empty? user_errors = result.dig("data", "webhookSubscriptionDelete", "userErrors") || {} raise Errors::WebhookRegistrationError, "Failed to delete webhook from Shopify: #{user_errors[0]["message"]}" unless user_errors.empty? result end |