shopify_api_bruv 0.3.0 → 0.3.2
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 +4 -4
- data/.env.template +0 -3
- data/CHANGELOG.md +10 -2
- data/Gemfile.lock +1 -1
- data/docs/README.md +0 -1
- data/lib/shopify_api_bruv/clients/http_request.rb +23 -0
- data/lib/shopify_api_bruv/clients/http_response.rb +2 -2
- data/lib/shopify_api_bruv/errors/http_request_error.rb +8 -0
- data/lib/shopify_api_bruv/errors/resource_response_error.rb +8 -0
- data/lib/shopify_api_bruv/resources/graphql/resource.rb +4 -6
- data/lib/shopify_api_bruv/resources/graphql/resource_response.rb +30 -0
- data/lib/shopify_api_bruv/resources/rest/pagination.rb +1 -1
- data/lib/shopify_api_bruv/resources/rest/resource.rb +4 -20
- data/lib/shopify_api_bruv/resources/rest/resource_response.rb +40 -0
- data/lib/shopify_api_bruv/version.rb +1 -1
- metadata +5 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 19a848bdcf14ee4042a43849b80b920783836c6db174bd32354e139201fe159b
|
4
|
+
data.tar.gz: c19e896c3e1aae699c8dd27b04b3c7dd21b08480aa70b3841e5c9c2c1f6d5f2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9dbdcedfe8ab7682ee69d54ee0454bd81077ddbae6186b5f436620f4093ed8b7f00e2496e2371f57ee5e730f760563a240f9b4a3b76654a4009d50117f4725d5
|
7
|
+
data.tar.gz: 01174bf39bed2c243f24b38e217d5b32693e6b37883ee783a68ca0cd7fb841630cdd3dda2f021fa12a6036e4b0bfd22a64c58d3c767f991f912d871de66aec65
|
data/.env.template
CHANGED
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
|
-
-
|
6
|
-
-
|
13
|
+
- Refactors resource classes
|
14
|
+
- Updates docs
|
7
15
|
|
8
16
|
## [0.2.7] - 2023-12-09
|
9
17
|
|
data/Gemfile.lock
CHANGED
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
|
@@ -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
|
27
|
+
handle_response_errors(body: response.body, query:, variables:, tries:)
|
29
28
|
|
30
|
-
|
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::
|
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::
|
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
|
@@ -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
|
-
|
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.
|
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
|
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
|
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.
|
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
|