shopify_api_bruv 0.2.5 → 0.2.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/lib/shopify_api_bruv/clients/graphql/client.rb +1 -0
- data/lib/shopify_api_bruv/clients/http_client.rb +5 -1
- data/lib/shopify_api_bruv/clients/http_request.rb +3 -2
- data/lib/shopify_api_bruv/clients/http_response.rb +3 -24
- data/lib/shopify_api_bruv/clients/rest/client.rb +2 -0
- data/lib/shopify_api_bruv/clients/rest/http_response.rb +39 -0
- data/lib/shopify_api_bruv/resources/graphql/resource.rb +2 -7
- data/lib/shopify_api_bruv/resources/rest/pagination_resource.rb +42 -0
- data/lib/shopify_api_bruv/resources/rest/resource.rb +10 -12
- data/lib/shopify_api_bruv/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 62561cc5b575e25d08e4fdbab3bb413f0f7d6b1c092ae617021d363fe39949e7
|
4
|
+
data.tar.gz: ede66f0903bca3c0c05cd77baec2049a7a254519815d2bbfd00afb546609e0ac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c30df045b71ae5b922a2faec703eb85c40c3b71e7365c41ba5312def42c9b5f5067484a2ee0c7538212d275fa86fed0de8d016d9e4b8472ebc54c59ed44f6476
|
7
|
+
data.tar.gz: fc3d0fa99d9b63d69c47d0a971c72cf10d0a865b1d3e1807eae1922e5e498d429f764d3780404cf38eafa7d7a07d2bd620ee40c88796fce6aded445a9c07ab2d
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -31,7 +31,11 @@ module ShopifyApiBruv
|
|
31
31
|
body: http_request.body
|
32
32
|
)
|
33
33
|
|
34
|
-
|
34
|
+
if http_request.api == :rest
|
35
|
+
Rest::HttpResponse.new(code: response.code, headers: response.headers, body: response.parsed_response)
|
36
|
+
else
|
37
|
+
HttpResponse.new(code: response.code, headers: response.headers, body: response.parsed_response)
|
38
|
+
end
|
35
39
|
end
|
36
40
|
end
|
37
41
|
end
|
@@ -3,9 +3,10 @@
|
|
3
3
|
module ShopifyApiBruv
|
4
4
|
module Clients
|
5
5
|
class HttpRequest
|
6
|
-
attr_reader :method, :path, :body, :content_type, :query, :headers
|
6
|
+
attr_reader :api, :method, :path, :body, :content_type, :query, :headers
|
7
7
|
|
8
|
-
def initialize(method:, path:, body:, content_type:, query: nil, headers:)
|
8
|
+
def initialize(api:, method:, path:, body:, content_type:, query: nil, headers:)
|
9
|
+
@api = api
|
9
10
|
@method = method
|
10
11
|
@path = path
|
11
12
|
@body = body.is_a?(Hash) ? body.to_json : body
|
@@ -3,49 +3,28 @@
|
|
3
3
|
module ShopifyApiBruv
|
4
4
|
module Clients
|
5
5
|
class HttpResponse
|
6
|
-
attr_reader :code, :headers, :body
|
6
|
+
attr_reader :code, :headers, :body
|
7
7
|
|
8
8
|
def initialize(code:, headers:, body:)
|
9
9
|
@code = code
|
10
10
|
@headers = headers
|
11
11
|
@body = body
|
12
|
-
@page_info = parse_page_info
|
13
12
|
|
14
13
|
ShopifyApiBruv.logger(
|
15
14
|
method: :info,
|
16
15
|
message: "Shopify API Response (Code: #{code}):\nHeaders:\n#{headers}\n\nBody:\n#{body}"
|
17
16
|
)
|
18
17
|
|
19
|
-
validate
|
18
|
+
validate
|
20
19
|
end
|
21
20
|
|
22
21
|
private
|
23
22
|
|
24
|
-
def parse_page_info
|
25
|
-
return { previous: nil, next: nil } if headers['link'].nil?
|
26
|
-
|
27
|
-
page_info = {}
|
28
|
-
|
29
|
-
headers['link'].split(',').each do |link|
|
30
|
-
rel = link.match(/rel="(.*?)"/).captures.first
|
31
|
-
url = link.match(/<(.*?)>/).captures.first
|
32
|
-
|
33
|
-
URI.parse(url).query.split('&').each do |param|
|
34
|
-
if param.split('=').first == 'page_info'
|
35
|
-
page_info[rel] = param.split('=').last
|
36
|
-
break
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
{ previous: page_info['previous'], next: page_info['next'] }
|
42
|
-
end
|
43
|
-
|
44
23
|
def ok?
|
45
24
|
(200..299).cover?(code)
|
46
25
|
end
|
47
26
|
|
48
|
-
def validate
|
27
|
+
def validate
|
49
28
|
return if ok?
|
50
29
|
|
51
30
|
errors = body['errors'] || ''
|
@@ -32,9 +32,11 @@ module ShopifyApiBruv
|
|
32
32
|
def request(method:, path:, body:, query:, headers:)
|
33
33
|
http_client.request(
|
34
34
|
http_request: HttpRequest.new(
|
35
|
+
api: :rest,
|
35
36
|
method:,
|
36
37
|
path:,
|
37
38
|
body:,
|
39
|
+
query:,
|
38
40
|
content_type: body.nil? ? nil : 'application/json',
|
39
41
|
headers:
|
40
42
|
)
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
module Rest
|
6
|
+
class HttpResponse < HttpResponse
|
7
|
+
attr_reader :page_info
|
8
|
+
|
9
|
+
def initialize(code:, headers:, body:)
|
10
|
+
super(code:, headers:, body:)
|
11
|
+
|
12
|
+
@page_info = parse_page_info
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def parse_page_info
|
18
|
+
return { previous: nil, next: nil } if headers['link'].nil?
|
19
|
+
|
20
|
+
page_info = {}
|
21
|
+
|
22
|
+
headers['link'].split(',').each do |link|
|
23
|
+
rel = link.match(/rel="(.*?)"/).captures.first
|
24
|
+
url = link.match(/<(.*?)>/).captures.first
|
25
|
+
|
26
|
+
URI.parse(url).query.split('&').each do |param|
|
27
|
+
if param.split('=').first == 'page_info'
|
28
|
+
page_info[rel] = param.split('=').last
|
29
|
+
break
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
{ previous: page_info['previous'], next: page_info['next'] }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -19,13 +19,6 @@ module ShopifyApiBruv
|
|
19
19
|
raise NotImplementedError, 'Please set a query in the derived class' if query.nil?
|
20
20
|
|
21
21
|
response = client.request(query:, variables:)
|
22
|
-
|
23
|
-
handle_response(response:, query:, variables:, tries:)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def handle_response(response:, query:, variables:, tries:)
|
29
22
|
body = response.body
|
30
23
|
|
31
24
|
handle_response_errors(body:, query:, variables:, tries:)
|
@@ -34,6 +27,8 @@ module ShopifyApiBruv
|
|
34
27
|
mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
|
35
28
|
end
|
36
29
|
|
30
|
+
private
|
31
|
+
|
37
32
|
def handle_response_errors(body:, query:, variables:, tries:)
|
38
33
|
errors = body['errors'] || body.dig('data', 'errors')
|
39
34
|
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Resources
|
5
|
+
module Rest
|
6
|
+
class PaginationResource
|
7
|
+
attr_reader :resource, :page_info
|
8
|
+
|
9
|
+
def initialize(resource:, page_info:)
|
10
|
+
@resource = resource
|
11
|
+
@page_info = page_info
|
12
|
+
end
|
13
|
+
|
14
|
+
def next_page?
|
15
|
+
!page_info[:next].nil?
|
16
|
+
end
|
17
|
+
|
18
|
+
def previous_page?
|
19
|
+
!page_info[:previous].nil?
|
20
|
+
end
|
21
|
+
|
22
|
+
def purpose?
|
23
|
+
next_page? || previous_page?
|
24
|
+
end
|
25
|
+
|
26
|
+
def fetch_next_page
|
27
|
+
raise Errors::ResourceError, 'Next page cursor not found' unless next_page?
|
28
|
+
|
29
|
+
resource.query[:page_info] = page_info[:next]
|
30
|
+
resource.request
|
31
|
+
end
|
32
|
+
|
33
|
+
def fetch_previous_page
|
34
|
+
raise Errors::ResourceError, 'Previous page cursor not found' unless previous_page?
|
35
|
+
|
36
|
+
resource.query[:page_info] = page_info[:previous]
|
37
|
+
resource.request
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -4,7 +4,8 @@ module ShopifyApiBruv
|
|
4
4
|
module Resources
|
5
5
|
module Rest
|
6
6
|
class Resource < Base
|
7
|
-
attr_reader :client, :method, :path, :body
|
7
|
+
attr_reader :client, :method, :path, :body
|
8
|
+
attr_accessor :query, :pagination_resource
|
8
9
|
|
9
10
|
SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER', 4).to_i
|
10
11
|
MINIMUM_CREDIT_LEFT = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_MINIMUM_CREDIT_LEFT', 4).to_i
|
@@ -16,31 +17,28 @@ module ShopifyApiBruv
|
|
16
17
|
@body = body
|
17
18
|
@query = query
|
18
19
|
|
19
|
-
validate_arguments
|
20
|
+
validate_arguments
|
20
21
|
end
|
21
22
|
|
22
23
|
def request
|
23
24
|
response = client.public_send(method, path:, body:, query:)
|
24
25
|
|
25
|
-
|
26
|
+
handle_response_api_limits(headers: response.headers)
|
27
|
+
|
28
|
+
pagination_resource = PaginationResource.new(resource: self, page_info: response.page_info)
|
29
|
+
@pagination_resource = pagination_resource if pagination_resource.purpose?
|
30
|
+
|
31
|
+
response.body
|
26
32
|
end
|
27
33
|
|
28
34
|
private
|
29
35
|
|
30
|
-
def validate_arguments
|
36
|
+
def validate_arguments
|
31
37
|
if [:put, :post].include?(method)
|
32
38
|
raise Errors::ResourceError, "Argument 'body' is required for method: #{method}" if body.nil?
|
33
39
|
end
|
34
40
|
end
|
35
41
|
|
36
|
-
def handle_response(response:)
|
37
|
-
handle_response_api_limits(headers: response.headers)
|
38
|
-
|
39
|
-
body = response.body
|
40
|
-
body['page_info'] = response.page_info if body.is_a?(Hash)
|
41
|
-
body
|
42
|
-
end
|
43
|
-
|
44
42
|
def handle_response_api_limits(headers:)
|
45
43
|
api_call_limit_header = headers['x-shopify-shop-api-call-limit'].split('/')
|
46
44
|
|
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.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Idjent
|
@@ -118,12 +118,14 @@ files:
|
|
118
118
|
- lib/shopify_api_bruv/clients/http_request.rb
|
119
119
|
- lib/shopify_api_bruv/clients/http_response.rb
|
120
120
|
- lib/shopify_api_bruv/clients/rest/client.rb
|
121
|
+
- lib/shopify_api_bruv/clients/rest/http_response.rb
|
121
122
|
- lib/shopify_api_bruv/errors/http_client_error.rb
|
122
123
|
- lib/shopify_api_bruv/errors/http_response_error.rb
|
123
124
|
- lib/shopify_api_bruv/errors/resource_error.rb
|
124
125
|
- lib/shopify_api_bruv/logger.rb
|
125
126
|
- lib/shopify_api_bruv/resources/base.rb
|
126
127
|
- lib/shopify_api_bruv/resources/graphql/resource.rb
|
128
|
+
- lib/shopify_api_bruv/resources/rest/pagination_resource.rb
|
127
129
|
- lib/shopify_api_bruv/resources/rest/resource.rb
|
128
130
|
- lib/shopify_api_bruv/version.rb
|
129
131
|
- sig/shopify_api_bruv.rbs
|