shopify_api_bruv 0.1.0
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 +7 -0
- data/.env.template +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +30 -0
- data/.ruby-version +1 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +80 -0
- data/LICENSE.txt +21 -0
- data/README.md +34 -0
- data/Rakefile +8 -0
- data/docs/README.md +101 -0
- data/lib/shopify_api_bruv/auth/config.rb +15 -0
- data/lib/shopify_api_bruv/clients/graphql/client.rb +28 -0
- data/lib/shopify_api_bruv/clients/http_client.rb +38 -0
- data/lib/shopify_api_bruv/clients/http_request.rb +23 -0
- data/lib/shopify_api_bruv/clients/http_response.rb +34 -0
- data/lib/shopify_api_bruv/clients/rest/client.rb +46 -0
- data/lib/shopify_api_bruv/errors/http_client_error.rb +8 -0
- data/lib/shopify_api_bruv/errors/http_response_error.rb +8 -0
- data/lib/shopify_api_bruv/errors/resource_error.rb +8 -0
- data/lib/shopify_api_bruv/logger.rb +12 -0
- data/lib/shopify_api_bruv/resources/base.rb +11 -0
- data/lib/shopify_api_bruv/resources/graphql/resource.rb +53 -0
- data/lib/shopify_api_bruv/resources/rest/resource.rb +55 -0
- data/lib/shopify_api_bruv/version.rb +5 -0
- data/lib/shopify_api_bruv.rb +12 -0
- data/sig/shopify_api_bruv.rbs +4 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c9a8788c8cc1ec6374a0731412c5e1c408748dfe46713c5cd8c1c30bd9c99886
|
4
|
+
data.tar.gz: 193240048cc58515fff187fb7ac2df8dc69cd3276857d1cbe592596e383c0e31
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d6839799269d3b4292ac2a2bd554fe08ee9f6fb0771644ade268d14b1e75980b26d8e1eae7cd62e08a6d0cc5d22572b7da8cb1054f9d28947a80f16e1e70d8fb
|
7
|
+
data.tar.gz: 5d012fb371d797871b31ac67000d7dc4ca7410b11b4eb72fd71e43adce5331fb963ad0ccb3fefaeb353e3aa840608e91f20227430bbae55e3f9c172b7252a4bf
|
data/.env.template
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
API_DOMAIN="{API_DOMAIN}"
|
2
|
+
API_VERSION="{API_VERSION}"
|
3
|
+
API_ACCESS_TOKEN="{API_ACCESS_TOKEN}"
|
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/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
inherit_gem:
|
2
|
+
rubocop-shopify: rubocop.yml
|
3
|
+
|
4
|
+
require:
|
5
|
+
- rubocop-performance
|
6
|
+
|
7
|
+
AllCops:
|
8
|
+
Exclude:
|
9
|
+
- 'bin/**/*'
|
10
|
+
- 'db/**/*'
|
11
|
+
SuggestExtensions: false
|
12
|
+
NewCops: enable
|
13
|
+
|
14
|
+
Style/StringLiterals:
|
15
|
+
EnforcedStyle: single_quotes
|
16
|
+
|
17
|
+
Style/TrailingCommaInHashLiteral:
|
18
|
+
EnforcedStyleForMultiline: no_comma
|
19
|
+
|
20
|
+
Style/TrailingCommaInArrayLiteral:
|
21
|
+
EnforcedStyleForMultiline: no_comma
|
22
|
+
|
23
|
+
Style/TrailingCommaInArguments:
|
24
|
+
EnforcedStyleForMultiline: no_comma
|
25
|
+
|
26
|
+
Style/ClassMethodsDefinitions:
|
27
|
+
EnforcedStyle: def_self
|
28
|
+
|
29
|
+
Lint/MissingSuper:
|
30
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
3.2.2
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
shopify_api_bruv (0.1.0)
|
5
|
+
httparty
|
6
|
+
logger
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
ast (2.4.2)
|
12
|
+
base64 (0.1.1)
|
13
|
+
diff-lcs (1.5.0)
|
14
|
+
dotenv (2.8.1)
|
15
|
+
httparty (0.21.0)
|
16
|
+
mini_mime (>= 1.0.0)
|
17
|
+
multi_xml (>= 0.5.2)
|
18
|
+
json (2.6.3)
|
19
|
+
language_server-protocol (3.17.0.3)
|
20
|
+
logger (1.5.3)
|
21
|
+
mini_mime (1.1.5)
|
22
|
+
multi_xml (0.6.0)
|
23
|
+
parallel (1.23.0)
|
24
|
+
parser (3.2.2.3)
|
25
|
+
ast (~> 2.4.1)
|
26
|
+
racc
|
27
|
+
racc (1.7.1)
|
28
|
+
rainbow (3.1.1)
|
29
|
+
rake (13.0.6)
|
30
|
+
regexp_parser (2.8.1)
|
31
|
+
rexml (3.2.6)
|
32
|
+
rspec (3.12.0)
|
33
|
+
rspec-core (~> 3.12.0)
|
34
|
+
rspec-expectations (~> 3.12.0)
|
35
|
+
rspec-mocks (~> 3.12.0)
|
36
|
+
rspec-core (3.12.2)
|
37
|
+
rspec-support (~> 3.12.0)
|
38
|
+
rspec-expectations (3.12.3)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.12.0)
|
41
|
+
rspec-mocks (3.12.6)
|
42
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
43
|
+
rspec-support (~> 3.12.0)
|
44
|
+
rspec-support (3.12.1)
|
45
|
+
rubocop (1.56.3)
|
46
|
+
base64 (~> 0.1.1)
|
47
|
+
json (~> 2.3)
|
48
|
+
language_server-protocol (>= 3.17.0)
|
49
|
+
parallel (~> 1.10)
|
50
|
+
parser (>= 3.2.2.3)
|
51
|
+
rainbow (>= 2.2.2, < 4.0)
|
52
|
+
regexp_parser (>= 1.8, < 3.0)
|
53
|
+
rexml (>= 3.2.5, < 4.0)
|
54
|
+
rubocop-ast (>= 1.28.1, < 2.0)
|
55
|
+
ruby-progressbar (~> 1.7)
|
56
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
57
|
+
rubocop-ast (1.29.0)
|
58
|
+
parser (>= 3.2.1.0)
|
59
|
+
rubocop-performance (1.19.0)
|
60
|
+
rubocop (>= 1.7.0, < 2.0)
|
61
|
+
rubocop-ast (>= 0.4.0)
|
62
|
+
rubocop-shopify (2.14.0)
|
63
|
+
rubocop (~> 1.51)
|
64
|
+
ruby-progressbar (1.13.0)
|
65
|
+
unicode-display_width (2.4.2)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
x86_64-linux
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
dotenv
|
72
|
+
rake (~> 13.0)
|
73
|
+
rspec (~> 3.0)
|
74
|
+
rubocop
|
75
|
+
rubocop-performance
|
76
|
+
rubocop-shopify
|
77
|
+
shopify_api_bruv!
|
78
|
+
|
79
|
+
BUNDLED WITH
|
80
|
+
2.4.22
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 ident
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# shopify_api_bruv
|
2
|
+
[](https://github.com/Idjent/shopify_api_bruv/actions/workflows/build.yml)
|
3
|
+
|
4
|
+
Bruv, how about simple configuration based API calls to Shopify ✌️
|
5
|
+
|
6
|
+
Note: Most of this is based on the official and maintained [Shopify API Library](https://github.com/Shopify/shopify-api-ruby) gem by Shopify and was created mostly to make my life easier and for personal use and education.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Install the gem and add to the application's Gemfile by executing:
|
11
|
+
|
12
|
+
$ bundle add shopify_api_bruv
|
13
|
+
|
14
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
15
|
+
|
16
|
+
$ gem install shopify_api_bruv
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
See [/docs](docs/README.md)
|
21
|
+
|
22
|
+
## Development
|
23
|
+
|
24
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
25
|
+
|
26
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
27
|
+
|
28
|
+
## Contributing
|
29
|
+
|
30
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Idjent/shopify_api_bruv.
|
31
|
+
|
32
|
+
## License
|
33
|
+
|
34
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/docs/README.md
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
# Documentation
|
2
|
+
|
3
|
+
## Environment variables
|
4
|
+
|
5
|
+
```bash
|
6
|
+
SHOPIFY_API_BRUV_LOGGER_ENABLED="true" # Enables logger, remove or set to false to disable
|
7
|
+
SHOPIFY_API_BRUV_REQUEST_MAX_TRIES="3" # Max tries for resource class based requests
|
8
|
+
SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER="4" # Sleep timer for resource class based requests
|
9
|
+
SHOPIFY_API_BRUV_REQUEST_MINIMUM_CREDIT_LEFT="4" # Sleeps if credit left reaches this amount
|
10
|
+
```
|
11
|
+
|
12
|
+
## Auth
|
13
|
+
```ruby
|
14
|
+
# Initialize a config
|
15
|
+
config = ShopifyApiBruv::Auth::Config.new(
|
16
|
+
api_domain: "shop.myshopify.com",
|
17
|
+
api_version: "2023-10",
|
18
|
+
api_access_token: "ACCESS_TOKEN"
|
19
|
+
)
|
20
|
+
```
|
21
|
+
|
22
|
+
## Usage (GraphQL)
|
23
|
+
|
24
|
+
### Simple request
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
# Initialize a graphql client and provide the config
|
28
|
+
client = ShopifyApiBruv::Clients::Graphql::Client.new(config:)
|
29
|
+
|
30
|
+
# Define a query
|
31
|
+
query = <<~GRAPHQL
|
32
|
+
query {
|
33
|
+
products(first: 250) {
|
34
|
+
edges {
|
35
|
+
node {
|
36
|
+
id
|
37
|
+
title
|
38
|
+
handle
|
39
|
+
}
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
GRAPHQL
|
44
|
+
|
45
|
+
# Make the request
|
46
|
+
client.request(query:)
|
47
|
+
```
|
48
|
+
|
49
|
+
### Using the resource class
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
class GetProducts < ShopifyApiBruv::Resources::Graphql::Resource
|
53
|
+
def query
|
54
|
+
<<~GRAPHQL
|
55
|
+
query {
|
56
|
+
products(first: 250) {
|
57
|
+
edges {
|
58
|
+
node {
|
59
|
+
id
|
60
|
+
title
|
61
|
+
handle
|
62
|
+
}
|
63
|
+
}
|
64
|
+
}
|
65
|
+
}
|
66
|
+
GRAPHQL
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Usage
|
71
|
+
GetProducts.request(config:)
|
72
|
+
```
|
73
|
+
|
74
|
+
## Usage (Rest)
|
75
|
+
|
76
|
+
### Simple request
|
77
|
+
|
78
|
+
```ruby
|
79
|
+
# Initialize a graphql client and provide the config
|
80
|
+
client = ShopifyApiBruv::Clients::Rest::Client.new(config:)
|
81
|
+
|
82
|
+
# Make the request
|
83
|
+
client.get(path: '/products')
|
84
|
+
```
|
85
|
+
|
86
|
+
### Using the resource class
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
class GetProducts < ShopifyApiBruv::Resources::Rest::Resource
|
90
|
+
def initialize(config:)
|
91
|
+
super(
|
92
|
+
config:,
|
93
|
+
method: :get,
|
94
|
+
path: '/products'
|
95
|
+
)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Usage
|
100
|
+
GetProducts.request(config:)
|
101
|
+
```
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Auth
|
5
|
+
class Config
|
6
|
+
attr_reader :api_domain, :api_version, :api_access_token
|
7
|
+
|
8
|
+
def initialize(api_domain:, api_version:, api_access_token:)
|
9
|
+
@api_domain = api_domain
|
10
|
+
@api_version = api_version
|
11
|
+
@api_access_token = api_access_token
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
module Graphql
|
6
|
+
class Client
|
7
|
+
attr_reader :config, :http_client
|
8
|
+
|
9
|
+
def initialize(config:)
|
10
|
+
@config = config
|
11
|
+
@http_client = HttpClient.new(config:, path: "admin/api/#{config.api_version}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def request(query:, variables: nil, headers: nil)
|
15
|
+
http_client.request(
|
16
|
+
http_request: HttpRequest.new(
|
17
|
+
method: :post,
|
18
|
+
path: '/graphql.json',
|
19
|
+
body: { query:, variables: },
|
20
|
+
content_type: 'application/json',
|
21
|
+
headers:
|
22
|
+
)
|
23
|
+
)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
class HttpClient
|
6
|
+
attr_reader :config, :endpoint, :headers
|
7
|
+
|
8
|
+
def initialize(config:, path:)
|
9
|
+
raise Errors::HttpClientError, 'Missing or invalid config.' unless config.is_a?(Auth::Config)
|
10
|
+
|
11
|
+
@config = config
|
12
|
+
@endpoint = "#{config.api_domain}/#{path}"
|
13
|
+
@headers = {
|
14
|
+
'User-Agent': "Bruv #{VERSION}",
|
15
|
+
'Accept': 'application/json',
|
16
|
+
'X-Shopify-Access-Token': config.api_access_token
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def request(http_request:)
|
21
|
+
raise Errors::HttpClientError, 'Missing or invalid http request.' unless http_request.is_a?(HttpRequest)
|
22
|
+
|
23
|
+
headers.merge(http_request.headers) unless http_request.headers.nil?
|
24
|
+
headers['Content-Type'] = http_request.content_type
|
25
|
+
|
26
|
+
response = HTTParty.public_send(
|
27
|
+
http_request.method,
|
28
|
+
"https://#{endpoint}/#{http_request.path}",
|
29
|
+
headers:,
|
30
|
+
query: http_request.query,
|
31
|
+
body: http_request.body
|
32
|
+
)
|
33
|
+
|
34
|
+
HttpResponse.new(code: response.code, headers: response.headers, body: response.parsed_response)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
class HttpRequest
|
6
|
+
attr_reader :method, :path, :body, :content_type, :query, :headers
|
7
|
+
|
8
|
+
def initialize(method:, path:, body:, content_type:, query: nil, headers:)
|
9
|
+
@method = method
|
10
|
+
@path = path
|
11
|
+
@body = body.is_a?(Hash) ? body.to_json : body
|
12
|
+
@content_type = content_type
|
13
|
+
@query = query
|
14
|
+
@headers = headers
|
15
|
+
|
16
|
+
ShopifyApiBruv.logger(
|
17
|
+
method: :info,
|
18
|
+
message: 'Shopify API request initiated...'
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
class HttpResponse
|
6
|
+
attr_reader :code, :headers, :body
|
7
|
+
|
8
|
+
def initialize(code:, headers:, body:)
|
9
|
+
@code = code
|
10
|
+
@headers = headers
|
11
|
+
@body = body
|
12
|
+
|
13
|
+
ShopifyApiBruv.logger(
|
14
|
+
method: :info,
|
15
|
+
message: "Shopify API Response (Code: #{code}):\nHeaders:\n#{headers}\n\nBody:\n#{body}"
|
16
|
+
)
|
17
|
+
|
18
|
+
validate!
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def ok?
|
24
|
+
(200..299).cover?(code)
|
25
|
+
end
|
26
|
+
|
27
|
+
def validate!
|
28
|
+
return if ok?
|
29
|
+
|
30
|
+
raise Errors::HttpResponseError, self
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Clients
|
5
|
+
module Rest
|
6
|
+
class Client
|
7
|
+
attr_reader :config, :http_client
|
8
|
+
|
9
|
+
def initialize(config:)
|
10
|
+
@config = config
|
11
|
+
@http_client = HttpClient.new(config:, path: "admin/api/#{config.api_version}")
|
12
|
+
end
|
13
|
+
|
14
|
+
def get(path:, body: nil, query: nil, headers: nil)
|
15
|
+
request(method: :get, path:, body:, query:, headers:)
|
16
|
+
end
|
17
|
+
|
18
|
+
def delete(path:, body: nil, query: nil, headers: nil)
|
19
|
+
request(method: :delete, path:, body:, query:, headers:)
|
20
|
+
end
|
21
|
+
|
22
|
+
def put(path:, body:, query: nil, headers: nil)
|
23
|
+
request(method: :put, path:, body:, query:, headers:)
|
24
|
+
end
|
25
|
+
|
26
|
+
def post(path:, body:, query: nil, headers: nil)
|
27
|
+
request(method: :post, path:, body:, query:, headers:)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def request(method:, path:, body:, query:, headers:)
|
33
|
+
http_client.request(
|
34
|
+
http_request: HttpRequest.new(
|
35
|
+
method:,
|
36
|
+
path:,
|
37
|
+
body:,
|
38
|
+
content_type: body.nil? ? nil : 'application/json',
|
39
|
+
headers:
|
40
|
+
)
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module ShopifyApiBruv
|
6
|
+
def self.logger(method:, message:)
|
7
|
+
logger = defined?(Rails) ? Rails.logger : Logger.new($stdout)
|
8
|
+
enabled = ENV.fetch('SHOPIFY_API_BRUV_LOGGER_ENABLED', false) == 'true'
|
9
|
+
|
10
|
+
logger.public_send(method, message) if enabled
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Resources
|
5
|
+
module Graphql
|
6
|
+
class Resource < Base
|
7
|
+
attr_reader :client, :variables, :mutation_object_name
|
8
|
+
attr_accessor :query
|
9
|
+
|
10
|
+
MAX_TRIES = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_MAX_TRIES', 3).to_i
|
11
|
+
SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER', 4).to_i
|
12
|
+
|
13
|
+
def initialize(config:, variables: nil, mutation_object_name: nil)
|
14
|
+
@client = Clients::Graphql::Client.new(config:)
|
15
|
+
@variables = variables
|
16
|
+
@mutation_object_name = mutation_object_name
|
17
|
+
end
|
18
|
+
|
19
|
+
def request(tries: 0)
|
20
|
+
raise NotImplementedError, 'Please set a query in the derived class' if query.nil?
|
21
|
+
|
22
|
+
response = client.request(query:, variables:)
|
23
|
+
|
24
|
+
handle_response(response:, query:, variables:, tries:)
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def handle_response(response:, query:, variables:, tries:)
|
30
|
+
body = response.body
|
31
|
+
|
32
|
+
handle_response_errors(body:, query:, variables:, tries:)
|
33
|
+
|
34
|
+
mutation_object_name.nil? ? body['data'] : body.dig('data', mutation_object_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
def handle_response_errors(body:, query:, variables:, tries:)
|
38
|
+
errors = body['errors'] || body.dig('data', 'errors')
|
39
|
+
|
40
|
+
raise Errors::ResourceError, errors if tries == MAX_TRIES
|
41
|
+
|
42
|
+
if errors&.any? { |error| error['message'] == 'Throttled' }
|
43
|
+
sleep(SLEEP_TIMER)
|
44
|
+
|
45
|
+
request(tries: + 1)
|
46
|
+
end
|
47
|
+
|
48
|
+
raise Errors::ResourceError, errors unless errors.nil?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ShopifyApiBruv
|
4
|
+
module Resources
|
5
|
+
module Rest
|
6
|
+
class Resource < Base
|
7
|
+
attr_reader :client, :method, :path, :body, :query
|
8
|
+
|
9
|
+
SLEEP_TIMER = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_SLEEP_TIMER', 4).to_i
|
10
|
+
MINIMUM_CREDIT_LEFT = ENV.fetch('SHOPIFY_API_BRUV_REQUEST_MINIMUM_CREDIT_LEFT', 4).to_i
|
11
|
+
|
12
|
+
def initialize(config:, method:, path:, body: nil, query: nil)
|
13
|
+
@client = Clients::Rest::Client.new(config:)
|
14
|
+
@method = method
|
15
|
+
@path = path
|
16
|
+
@body = body
|
17
|
+
@query = query
|
18
|
+
|
19
|
+
validate_arguments!
|
20
|
+
end
|
21
|
+
|
22
|
+
def request
|
23
|
+
response = client.public_send(method, path:, body:, query:)
|
24
|
+
|
25
|
+
handle_response(response:)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def validate_arguments!
|
31
|
+
if [:put, :post].include?(method)
|
32
|
+
raise Errors::ResourceError, "Argument 'body' is required for method: #{method}" if body.nil?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def handle_response(response:)
|
37
|
+
handle_response_api_limits(headers: response.headers)
|
38
|
+
|
39
|
+
response.body
|
40
|
+
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) <= MINIMUM_CREDIT_LEFT
|
49
|
+
sleep(SLEEP_TIMER)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shopify_api_bruv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Idjent
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-12-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: httparty
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: logger
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dotenv
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rubocop
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop-performance
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop-shopify
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
executables: []
|
100
|
+
extensions: []
|
101
|
+
extra_rdoc_files: []
|
102
|
+
files:
|
103
|
+
- ".env.template"
|
104
|
+
- ".rspec"
|
105
|
+
- ".rubocop.yml"
|
106
|
+
- ".ruby-version"
|
107
|
+
- CHANGELOG.md
|
108
|
+
- Gemfile
|
109
|
+
- Gemfile.lock
|
110
|
+
- LICENSE.txt
|
111
|
+
- README.md
|
112
|
+
- Rakefile
|
113
|
+
- docs/README.md
|
114
|
+
- lib/shopify_api_bruv.rb
|
115
|
+
- lib/shopify_api_bruv/auth/config.rb
|
116
|
+
- lib/shopify_api_bruv/clients/graphql/client.rb
|
117
|
+
- lib/shopify_api_bruv/clients/http_client.rb
|
118
|
+
- lib/shopify_api_bruv/clients/http_request.rb
|
119
|
+
- lib/shopify_api_bruv/clients/http_response.rb
|
120
|
+
- lib/shopify_api_bruv/clients/rest/client.rb
|
121
|
+
- lib/shopify_api_bruv/errors/http_client_error.rb
|
122
|
+
- lib/shopify_api_bruv/errors/http_response_error.rb
|
123
|
+
- lib/shopify_api_bruv/errors/resource_error.rb
|
124
|
+
- lib/shopify_api_bruv/logger.rb
|
125
|
+
- lib/shopify_api_bruv/resources/base.rb
|
126
|
+
- lib/shopify_api_bruv/resources/graphql/resource.rb
|
127
|
+
- lib/shopify_api_bruv/resources/rest/resource.rb
|
128
|
+
- lib/shopify_api_bruv/version.rb
|
129
|
+
- sig/shopify_api_bruv.rbs
|
130
|
+
homepage: https://github.com/Idjent/shopify_api_bruv
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata:
|
134
|
+
allowed_push_host: https://rubygems.org
|
135
|
+
homepage_uri: https://github.com/Idjent/shopify_api_bruv
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: 3.0.0
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubygems_version: 3.4.22
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: ''
|
155
|
+
test_files: []
|