shopify-client 0.0.3 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f5f47627632cde83fad96703fba4335e00b5f04a303c40d7c276558ed0d59c04
4
- data.tar.gz: 9b0fffc153c69cd1caf0cc67aae87070af2a780dbec6d13c71b63513c0165bf3
3
+ metadata.gz: 970f20e5e3da6050c6b5d6dd494ca7426079b34c679390e6e0eeee0113eace8a
4
+ data.tar.gz: '080d4c2dd0f31c41ab6da721a10808d984899318ed591f9c5cf32238bd1d42ee'
5
5
  SHA512:
6
- metadata.gz: 400820d737e8aa67dea8d4e08e3c3f3d77f5185c91c69ee1e3605707d56d22508cfb64a7cbe9789e1d5f5b9274cbee8ce9f654e7da580800a3c5aa4edd74a294
7
- data.tar.gz: 21ecdc9776deb76f0a36b7be42b4a0ab8ad90659f2a47c756f2f9aebfb69377722b6af7710c5915272cec99b1067ecaf9e5cff635de995804fb379a9a3b1ba3a
6
+ metadata.gz: 29c0d2d0a38ab6f7d335230b6354ac750f318c6094499908e2332b2713c19b1ebd3b6e6566519f3924f549d6b9b3fd7934334731cf72ff9da30b0bf68d379a4d
7
+ data.tar.gz: 8229de3254fd8bb22401a86278cba84c16718b620cea1f89cb1c4d5c0d57eccfbe6c33d033d5db432647d5cd287d6daee76f032f6ef968df46a715862bb248ed
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'async/http/faraday'
3
4
  require 'faraday'
4
5
  require 'faraday_middleware'
5
6
 
@@ -18,6 +19,7 @@ module ShopifyClient
18
19
  },
19
20
  url: "https://#{myshopify_domain}/admin/api/#{ShopifyClient.config.api_version}",
20
21
  ) do |conn|
22
+ conn.adapter :async_http
21
23
  # Request throttling to avoid API rate limit.
22
24
  conn.use default_throttling_strategy
23
25
  # Retry for 429, too many requests.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyClient
4
+ ConfigError = Class.new(Error)
5
+ end
@@ -6,20 +6,52 @@ module ShopifyClient
6
6
  #
7
7
  # @param client [Client]
8
8
  #
9
- # @return [Array<Hash>] response data
9
+ # @return [Array<String>] GraphQL IDs
10
10
  def call(client)
11
- create_webhook = CreateWebhook.new
11
+ raise ConfigError, 'webhook_uri is not set' unless ShopifyClient.config.webhook_uri
12
12
 
13
- ShopifyClient.webhooks.map do |topic, options|
14
- Thread.new do
15
- webhook = {}.tap do |webhook|
16
- webhook[:topic] = topic
17
- webhook[:fields] = options[:fields] unless options[:fields].empty?
18
- end
13
+ webhooks_with_index = ShopifyClient.webhooks.each_with_index
19
14
 
20
- create_webhook.(client, webhook)
15
+ return [] unless webhooks_with_index.any?
16
+
17
+ client.graphql(%(
18
+ mutation webhookSubscriptionCreate(
19
+ #{webhooks_with_index.map { |_, i| %(
20
+ $topic#{i}: WebhookSubscriptionTopic!
21
+ $webhookSubscription#{i}: WebhookSubscriptionInput!
22
+ )}.join("\n")}
23
+ ) {
24
+ #{webhooks_with_index.map { |_, i| %(
25
+ webhookSubscriptionCreate#{i}: webhookSubscriptionCreate(
26
+ topic: $topic#{i}
27
+ webhookSubscription: $webhookSubscription#{i}
28
+ ) {
29
+ userErrors {
30
+ field
31
+ message
32
+ }
33
+ webhookSubscription {
34
+ id
35
+ }
36
+ }
37
+ )}.join("\n")}
38
+ }
39
+ ), webhooks_with_index.each_with_object({}) do |((topic, options), i), variables|
40
+ variables["topic#{i}"] = topic_to_graphql(topic)
41
+ variables["webhookSubscription#{i}"] = {}.tap do |subscription|
42
+ subscription['callbackUrl'] = ShopifyClient.config.webhook_uri
43
+ subscription['includeFields'] = options[:fields] unless options[:fields].empty?
21
44
  end
22
- end.map(&:value)
45
+ end).data['data'].map do |_, mutation|
46
+ mutation['webhookSubscription']['id']
47
+ end
48
+ end
49
+
50
+ # @param topic [String]
51
+ #
52
+ # @return [String]
53
+ private def topic_to_graphql(topic)
54
+ topic.upcase.sub('/', '_')
23
55
  end
24
56
  end
25
57
  end
@@ -7,11 +7,13 @@ module ShopifyClient
7
7
  # @option webhook [String] :topic
8
8
  # @option webhook [Array<String>] :fields
9
9
  #
10
- # @return [Hash] response data
10
+ # @return [Integer] ID
11
11
  def call(client, webhook)
12
+ raise ConfigError, 'webhook_uri is not set' unless ShopifyClient.config.webhook_uri
13
+
12
14
  client.post('webhooks', webhook: webhook.merge(
13
15
  address: ShopifyClient.config.webhook_uri,
14
- ))
16
+ )).data['webhook']['id']
15
17
  rescue Response::Error => e
16
18
  raise e unless e.response.errors.message?([
17
19
  /has already been taken/,
@@ -5,18 +5,40 @@ module ShopifyClient
5
5
  # Delete any existing webhooks.
6
6
  #
7
7
  # @param client [Client]
8
- #
9
- # @return [Array<Hash>] response data
10
- def call(client)
11
- webhooks = client.get('webhooks').data['webhooks']
8
+ # @param ids [Array<Integer>, nil] GraphQL IDs
9
+ def call(client, ids: nil)
10
+ ids ||= client.graphql(%({
11
+ webhookSubscriptions(first: 100) {
12
+ edges {
13
+ node {
14
+ id
15
+ }
16
+ }
17
+ }
18
+ })).data['data']['webhookSubscriptions']['edges'].map do |edge|
19
+ edge['node']['id']
20
+ end
12
21
 
13
- delete_webhook = DeleteWebhook.new
22
+ return if ids.empty?
14
23
 
15
- webhooks.map do |webhook|
16
- Thread.new do
17
- delete_webhook.(client, webhook['id'])
18
- end
19
- end.map(&:value)
24
+ client.graphql(%(
25
+ mutation webhookSubscriptionDelete(
26
+ #{ids.each_with_index.map { |_, i| %(
27
+ $id#{i}: ID!
28
+ )}.join("\n")}
29
+ ) {
30
+ #{ids.each_with_index.map { |_, i| %(
31
+ webhookSubscriptionDelete#{i}: webhookSubscriptionDelete(id: $id#{i}) {
32
+ userErrors {
33
+ field
34
+ message
35
+ }
36
+ }
37
+ )}.join("\n")}
38
+ }
39
+ ), ids.each_with_index.each_with_object({}) do |(id, i), variables|
40
+ variables["id#{i}"] = id
41
+ end)
20
42
  end
21
43
  end
22
44
  end
@@ -4,8 +4,6 @@ module ShopifyClient
4
4
  class DeleteWebhook
5
5
  # @param client [Client]
6
6
  # @param id [Integer]
7
- #
8
- # @return [Hash] response data
9
7
  def call(client, id)
10
8
  client.delete("webhooks/#{id}")
11
9
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShopifyClient
4
- VERSION = '0.0.3'
4
+ VERSION = '0.0.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kelsey Judson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-19 00:00:00.000000000 Z
11
+ date: 2021-07-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dotenv
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: async
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.29'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.29'
83
+ - !ruby/object:Gem::Dependency
84
+ name: async-http-faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.11'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.11'
69
97
  - !ruby/object:Gem::Dependency
70
98
  name: dry-configurable
71
99
  requirement: !ruby/object:Gem::Requirement
@@ -153,6 +181,7 @@ files:
153
181
  - lib/shopify-client/client.rb
154
182
  - lib/shopify-client/client/logging.rb
155
183
  - lib/shopify-client/client/normalise_path.rb
184
+ - lib/shopify-client/config_error.rb
156
185
  - lib/shopify-client/cookieless/check_header.rb
157
186
  - lib/shopify-client/cookieless/decode_session_token.rb
158
187
  - lib/shopify-client/cookieless/middleware.rb