shopify_api_bruv 0.3.0 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e8119277813ab65392ec1a963097cd264dc4f610c00ae3e31c8cfaeaaf9b735e
4
- data.tar.gz: 834626e70982174fb36b0bd7451e6e6318ea13c213a05d295262039287aea7c3
3
+ metadata.gz: 19a848bdcf14ee4042a43849b80b920783836c6db174bd32354e139201fe159b
4
+ data.tar.gz: c19e896c3e1aae699c8dd27b04b3c7dd21b08480aa70b3841e5c9c2c1f6d5f2a
5
5
  SHA512:
6
- metadata.gz: e119e67234a23b633eca37f5370054b951669cd9c9363c0d99d215bbfd288c235ffabda5f8456fb43213222c6910092182717c63ff2f0e93211ecf859ba504d7
7
- data.tar.gz: ff88c2d8b7a888a310cd14594d34ffe26c19506913ee1d91e232da4058bed4b3b42be46a3495f6cce6b096d48fc9f70db705875b289a7d35615316b191ee700f
6
+ metadata.gz: 9dbdcedfe8ab7682ee69d54ee0454bd81077ddbae6186b5f436620f4093ed8b7f00e2496e2371f57ee5e730f760563a240f9b4a3b76654a4009d50117f4725d5
7
+ data.tar.gz: 01174bf39bed2c243f24b38e217d5b32693e6b37883ee783a68ca0cd7fb841630cdd3dda2f021fa12a6036e4b0bfd22a64c58d3c767f991f912d871de66aec65
data/.env.template CHANGED
@@ -2,6 +2,3 @@ API_DOMAIN="{API_DOMAIN}"
2
2
  API_VERSION="{API_VERSION}"
3
3
  API_ACCESS_TOKEN="{API_ACCESS_TOKEN}"
4
4
  SHOPIFY_API_BRUV_LOGGER_ENABLED="true"
5
- SHOPIFY_API_BRUV_REQUEST_MAX_TRIES="3"
6
- SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER="4"
7
- SHOPIFY_API_BRUV_REQUEST_MINIMUM_CREDIT_LEFT="4"
data/CHANGELOG.md CHANGED
@@ -1,9 +1,17 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.2] - 2023-12-10
4
+
5
+ - Adds better resource class responses
6
+
7
+ ## [0.3.1] - 2023-12-10
8
+
9
+ - Adds error handling for http requests
10
+
3
11
  ## [0.3.0] - 2023-12-10
4
12
 
5
- - Refactor resource classes
6
- - Update docs
13
+ - Refactors resource classes
14
+ - Updates docs
7
15
 
8
16
  ## [0.2.7] - 2023-12-09
9
17
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- shopify_api_bruv (0.3.0)
4
+ shopify_api_bruv (0.3.2)
5
5
  httparty
6
6
  logger
7
7
 
data/docs/README.md CHANGED
@@ -11,5 +11,4 @@
11
11
  | `SHOPIFY_API_BRUV_LOGGER_ENABLED` | `false` | Boolean | No |
12
12
  | `SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_MAX_TRIES` | `3` | Integer | No |
13
13
  | `SHOPIFY_API_BRUV_RESOURCE_GRAPHQL_SLEEP_TIMER` | `4` | Integer | No |
14
- | `SHOPIFY_API_BRUV_RESOURCE_REST_SLEEP_TIMER` | `4` | Integer | No |
15
14
  | `SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD` | `8` | Integer | No |
@@ -5,6 +5,13 @@ module ShopifyApiBruv
5
5
  class HttpRequest
6
6
  attr_reader :api, :method, :path, :body, :content_type, :query, :headers
7
7
 
8
+ VALID_METHODS = [
9
+ :get,
10
+ :delete,
11
+ :put,
12
+ :post
13
+ ].freeze
14
+
8
15
  def initialize(api:, method:, path:, body:, content_type:, query: nil, headers:)
9
16
  @api = api
10
17
  @method = method
@@ -14,11 +21,27 @@ module ShopifyApiBruv
14
21
  @query = query
15
22
  @headers = headers
16
23
 
24
+ validate
25
+
17
26
  ShopifyApiBruv.logger(
18
27
  method: :info,
19
28
  message: "Shopify API Request (Method: #{method}):\nPath:\n#{path}\n\nBody:\n#{body}"
20
29
  )
21
30
  end
31
+
32
+ private
33
+
34
+ def validate
35
+ raise Errors::HttpRequestError,
36
+ "Invalid method #{method}, valid methods are: #{VALID_METHODS}" unless VALID_METHODS.include?(method)
37
+
38
+ raise Errors::HttpRequestError,
39
+ 'Content-Type missing' if !body.nil? && content_type.nil?
40
+
41
+ return unless [:put, :post].include?(method)
42
+
43
+ raise Errors::HttpRequestError, "Body is required for method #{method}" if body.nil?
44
+ end
22
45
  end
23
46
  end
24
47
  end
@@ -10,12 +10,12 @@ module ShopifyApiBruv
10
10
  @headers = headers
11
11
  @body = body
12
12
 
13
+ validate
14
+
13
15
  ShopifyApiBruv.logger(
14
16
  method: :info,
15
17
  message: "Shopify API Response (Code: #{code}):\nHeaders:\n#{headers}\n\nBody:\n#{body}"
16
18
  )
17
-
18
- validate
19
19
  end
20
20
 
21
21
  private
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Errors
5
+ class HttpRequestError < StandardError
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Errors
5
+ class ResourceResponseError < StandardError
6
+ end
7
+ end
8
+ end
@@ -23,12 +23,10 @@ module ShopifyApiBruv
23
23
  end
24
24
 
25
25
  response = client.request(query:, variables:)
26
- body = response.body
27
26
 
28
- handle_response_errors(body:, query:, variables:, tries:)
27
+ handle_response_errors(body: response.body, query:, variables:, tries:)
29
28
 
30
- mutation_object_name = query.match(/mutation\s+(\w+)\s*\(.*/)&.captures&.first
31
- mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
29
+ ResourceResponse.new(body: response.body, query:)
32
30
  end
33
31
 
34
32
  private
@@ -36,7 +34,7 @@ module ShopifyApiBruv
36
34
  def handle_response_errors(body:, query:, variables:, tries:)
37
35
  errors = body['errors'] || body.dig('data', 'errors')
38
36
 
39
- raise Errors::ResourceError, errors if tries == MAX_TRIES
37
+ raise Errors::ResourceResponseError, errors if tries == MAX_TRIES
40
38
 
41
39
  if errors&.any? { |error| error['message'] == 'Throttled' }
42
40
  sleep(SLEEP_TIMER)
@@ -44,7 +42,7 @@ module ShopifyApiBruv
44
42
  call(tries: + 1)
45
43
  end
46
44
 
47
- raise Errors::ResourceError, errors unless errors.nil?
45
+ raise Errors::ResourceResponseError, errors unless errors.nil?
48
46
  end
49
47
  end
50
48
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Resources
5
+ module Graphql
6
+ class ResourceResponse
7
+ attr_accessor :data, :extensions, :throttle_status
8
+
9
+ def initialize(body:, query:)
10
+ parse_body(body:, query:)
11
+ end
12
+
13
+ def as_open_struct
14
+ # rubocop:disable Style/OpenStructUse
15
+ JSON.parse(data.to_json, object_class: OpenStruct)
16
+ # rubocop:enable Style/OpenStructUse
17
+ end
18
+
19
+ private
20
+
21
+ def parse_body(body:, query:)
22
+ mutation_object_name = query.match(/mutation\s+(\w+)\s*\(.*/)&.captures&.first
23
+
24
+ @data = mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
25
+ @throttle_status = body.dig('extensions', 'cost', 'throttleStatus')
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -19,7 +19,7 @@ module ShopifyApiBruv
19
19
  !page_info[:previous].nil?
20
20
  end
21
21
 
22
- def purpose?
22
+ def page_exists?
23
23
  next_page? || previous_page?
24
24
  end
25
25
 
@@ -7,9 +7,6 @@ module ShopifyApiBruv
7
7
  attr_reader :client, :method, :path, :body
8
8
  attr_accessor :query, :pagination
9
9
 
10
- SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_SLEEP_TIMER', 4).to_i
11
- CREDIT_LEFT_THRESHOLD = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD', 8).to_i
12
-
13
10
  def initialize(config:, method:, path:, body: nil, query: nil)
14
11
  @client = Clients::Rest::Client.new(config:)
15
12
  @method = method
@@ -17,38 +14,25 @@ module ShopifyApiBruv
17
14
  @body = body
18
15
  @query = query
19
16
 
20
- validate_arguments
17
+ validate
21
18
  end
22
19
 
23
20
  def call
24
21
  response = client.public_send(method, path:, body:, query:)
25
22
 
26
- handle_response_api_limits(headers: response.headers)
27
-
28
23
  pagination = Pagination.new(resource: self, page_info: response.page_info)
29
- @pagination = pagination if pagination.purpose?
24
+ @pagination = pagination if pagination.page_exists?
30
25
 
31
- response.body
26
+ ResourceResponse.new(body: response.body, headers: response.headers)
32
27
  end
33
28
 
34
29
  private
35
30
 
36
- def validate_arguments
31
+ def validate
37
32
  if [:put, :post].include?(method)
38
33
  raise Errors::ResourceError, "Argument 'body' is required for method: #{method}" if body.nil?
39
34
  end
40
35
  end
41
-
42
- def handle_response_api_limits(headers:)
43
- api_call_limit_header = headers['x-shopify-shop-api-call-limit'].split('/')
44
-
45
- limit = api_call_limit_header.pop.to_i - 1
46
- used = api_call_limit_header.shift.to_i
47
-
48
- if (limit - used) <= CREDIT_LEFT_THRESHOLD
49
- sleep(SLEEP_TIMER)
50
- end
51
- end
52
36
  end
53
37
  end
54
38
  end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ShopifyApiBruv
4
+ module Resources
5
+ module Rest
6
+ class ResourceResponse
7
+ attr_accessor :data
8
+ attr_reader :credit_limit, :credit_used, :credit_left
9
+
10
+ CREDIT_LEFT_THRESHOLD = ENV.fetch('SHOPIFY_API_BRUV_RESOURCE_REST_CREDIT_LEFT_THRESHOLD', 8).to_i
11
+
12
+ def initialize(body:, headers:)
13
+ @data = body
14
+
15
+ parse_api_call_limit(headers:)
16
+ end
17
+
18
+ def as_open_struct
19
+ # rubocop:disable Style/OpenStructUse
20
+ JSON.parse(data.to_json, object_class: OpenStruct)
21
+ # rubocop:enable Style/OpenStructUse
22
+ end
23
+
24
+ def credit_left?
25
+ credit_left > CREDIT_LEFT_THRESHOLD
26
+ end
27
+
28
+ private
29
+
30
+ def parse_api_call_limit(headers:)
31
+ api_call_limit_header = headers['x-shopify-shop-api-call-limit'].split('/')
32
+
33
+ @credit_limit = api_call_limit_header.pop.to_i - 1
34
+ @credit_used = api_call_limit_header.shift.to_i
35
+ @credit_left = credit_limit - credit_used
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ShopifyApiBruv
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify_api_bruv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Idjent
@@ -127,13 +127,17 @@ files:
127
127
  - lib/shopify_api_bruv/clients/rest/client.rb
128
128
  - lib/shopify_api_bruv/clients/rest/http_response.rb
129
129
  - lib/shopify_api_bruv/errors/http_client_error.rb
130
+ - lib/shopify_api_bruv/errors/http_request_error.rb
130
131
  - lib/shopify_api_bruv/errors/http_response_error.rb
131
132
  - lib/shopify_api_bruv/errors/resource_error.rb
133
+ - lib/shopify_api_bruv/errors/resource_response_error.rb
132
134
  - lib/shopify_api_bruv/logger.rb
133
135
  - lib/shopify_api_bruv/resources/base.rb
134
136
  - lib/shopify_api_bruv/resources/graphql/resource.rb
137
+ - lib/shopify_api_bruv/resources/graphql/resource_response.rb
135
138
  - lib/shopify_api_bruv/resources/rest/pagination.rb
136
139
  - lib/shopify_api_bruv/resources/rest/resource.rb
140
+ - lib/shopify_api_bruv/resources/rest/resource_response.rb
137
141
  - lib/shopify_api_bruv/version.rb
138
142
  - sig/shopify_api_bruv.rbs
139
143
  homepage: https://github.com/Idjent/shopify_api_bruv