samcart_api 0.1.2 → 0.2.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 +4 -4
- data/lib/samcart_api/api_request.rb +34 -1
- data/lib/samcart_api/order.rb +9 -16
- data/lib/samcart_api/paginator.rb +29 -0
- data/lib/samcart_api/product.rb +9 -15
- data/lib/samcart_api.rb +1 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1bdda072456e66df0b1d954e208a19fe4046ed013c35507ea8d17a91a9919e4
|
4
|
+
data.tar.gz: 58417c4bc3cda196ca59dc8d99d5ac6ca39f6719037386aa777c1d07c9ea1a68
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9cc6675ff58a9ffa7dff671991e321d60261d4106c5fbf840fac0a349d2d69d372e4f797ca1a1da608affc349347601d765a29f3e61a2b24164984c148bc2588
|
7
|
+
data.tar.gz: ceb28e4b6a09859b3863f08d45bf680c2045fec382bde8a93a6f87b04bf075928591248f5404e9d4a3c0da24f1e65399378c4985a5b7d67ac4078e40d1cfc826
|
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
module SamcartApi
|
4
4
|
class ApiRequest
|
5
|
+
RETRY_ATTEMPTS = 3
|
6
|
+
|
5
7
|
def initialize(method, path, params, api_key)
|
6
8
|
@method = method
|
7
9
|
@path = path
|
@@ -11,6 +13,12 @@ module SamcartApi
|
|
11
13
|
end
|
12
14
|
|
13
15
|
def perform
|
16
|
+
safe_request { make_request }
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def make_request
|
14
22
|
response = connection.send(@method) do |req|
|
15
23
|
req.url "/#{@version}#{@path}"
|
16
24
|
req.params = @params if @params
|
@@ -22,7 +30,30 @@ module SamcartApi
|
|
22
30
|
handle_response(response)
|
23
31
|
end
|
24
32
|
|
25
|
-
|
33
|
+
def safe_request
|
34
|
+
retries = 0
|
35
|
+
|
36
|
+
begin
|
37
|
+
yield
|
38
|
+
rescue Faraday::TimeoutError, Faraday::ConnectionFailed => e
|
39
|
+
puts "Network error: #{e.message}. Retrying..."
|
40
|
+
sleep 2
|
41
|
+
retries += 1
|
42
|
+
retry if retries < RETRY_ATTEMPTS
|
43
|
+
raise 'Too many retries: Network issues'
|
44
|
+
rescue ApiError => e
|
45
|
+
if e.message.include?('Too Many Requests') # Custom handling for 429
|
46
|
+
wait_time = 5 # Default wait
|
47
|
+
puts "Rate limited! Retrying in #{wait_time} seconds..."
|
48
|
+
sleep wait_time
|
49
|
+
retries += 1
|
50
|
+
retry if retries < RETRY_ATTEMPTS
|
51
|
+
raise 'Too many retries: Rate limited'
|
52
|
+
else
|
53
|
+
raise
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
26
57
|
|
27
58
|
def connection
|
28
59
|
@connection ||= Faraday.new(url: SamcartApi.configuration.api_url) do |conn|
|
@@ -41,6 +72,8 @@ module SamcartApi
|
|
41
72
|
raise AuthenticationError, 'Access denied'
|
42
73
|
when 404
|
43
74
|
raise ApiError, 'Resource not found'
|
75
|
+
when 429
|
76
|
+
raise ApiError, 'Too Many Requests'
|
44
77
|
else
|
45
78
|
raise ApiError, "API request failed: #{response.body["message"] || response.body}"
|
46
79
|
end
|
data/lib/samcart_api/order.rb
CHANGED
@@ -6,25 +6,18 @@ module SamcartApi
|
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def find(id)
|
9
|
-
|
9
|
+
order = client.get("#{RESOURCE_PATH}/#{id}")
|
10
|
+
SamcartApi::SamcartObject.new(order)
|
10
11
|
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(id)
|
14
|
-
@id = id
|
15
|
-
end
|
16
|
-
|
17
|
-
def find
|
18
|
-
order = client.get("#{RESOURCE_PATH}/#{id}")
|
19
|
-
SamcartApi::SamcartObject.new(order)
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
12
|
|
24
|
-
|
13
|
+
def client
|
14
|
+
SamcartApi::Client.new
|
15
|
+
end
|
25
16
|
|
26
|
-
|
27
|
-
|
17
|
+
def all(filters: {}, limit: 100)
|
18
|
+
params = filters.merge(limit:)
|
19
|
+
SamcartApi::Paginator.new(client, RESOURCE_PATH, params)
|
20
|
+
end
|
28
21
|
end
|
29
22
|
end
|
30
23
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SamcartApi
|
4
|
+
class Paginator
|
5
|
+
def initialize(client, path, params)
|
6
|
+
@client = client
|
7
|
+
@next_url = "#{path}?#{params.to_query}"
|
8
|
+
@retry_attempts = 3
|
9
|
+
end
|
10
|
+
|
11
|
+
def next_page?
|
12
|
+
!@next_url.nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
def next_page
|
16
|
+
return unless next_page?
|
17
|
+
|
18
|
+
response = client.get(@next_url)
|
19
|
+
@next_url = response.dig('pagination', 'next')
|
20
|
+
response['data']
|
21
|
+
end
|
22
|
+
|
23
|
+
def each_page
|
24
|
+
yield next_page while next_page?
|
25
|
+
end
|
26
|
+
|
27
|
+
attr_reader :client
|
28
|
+
end
|
29
|
+
end
|
data/lib/samcart_api/product.rb
CHANGED
@@ -6,25 +6,19 @@ module SamcartApi
|
|
6
6
|
|
7
7
|
class << self
|
8
8
|
def find(id)
|
9
|
-
|
9
|
+
product = client.get("#{RESOURCE_PATH}/#{id}")
|
10
|
+
SamcartApi::SamcartObject.new(product)
|
10
11
|
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def initialize(id)
|
14
|
-
@id = id
|
15
|
-
end
|
16
12
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
13
|
+
def client
|
14
|
+
SamcartApi::Client.new
|
15
|
+
end
|
23
16
|
|
24
|
-
|
17
|
+
def all(filters: {}, limit: 100)
|
18
|
+
params = filters.merge(limit:)
|
25
19
|
|
26
|
-
|
27
|
-
|
20
|
+
SamcartApi::Paginator.new(client, RESOURCE_PATH, params)
|
21
|
+
end
|
28
22
|
end
|
29
23
|
end
|
30
24
|
end
|
data/lib/samcart_api.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: samcart_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Olumuyiwa Osiname
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -120,6 +120,7 @@ files:
|
|
120
120
|
- lib/samcart_api/api_request.rb
|
121
121
|
- lib/samcart_api/client.rb
|
122
122
|
- lib/samcart_api/order.rb
|
123
|
+
- lib/samcart_api/paginator.rb
|
123
124
|
- lib/samcart_api/product.rb
|
124
125
|
- lib/samcart_api/samcart_object.rb
|
125
126
|
homepage: https://github.com/oluosiname/samcart_api
|
@@ -137,7 +138,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
137
138
|
requirements:
|
138
139
|
- - ">="
|
139
140
|
- !ruby/object:Gem::Version
|
140
|
-
version:
|
141
|
+
version: 3.3.0
|
141
142
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
143
|
requirements:
|
143
144
|
- - ">="
|