shopcircle-orbit 1.1.0 → 1.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/shopcircle/orbit/transport.rb +39 -1
- data/lib/shopcircle/orbit/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 13eadce37b88d6b83df93f3df4d4e79bb02c4109ef9200b8b6343c0a6f1404a1
|
|
4
|
+
data.tar.gz: eb467f5a94400e3beaa199c9ff4bcf167a15d1834f2f37da639f92c4dc50665e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 47d718f705390d6a1f204fa3fdf4a826b8bcaac1b4fdb346b733e624d2612bb69e4d21758044b5f66a2867d907a6a8d4628104dab3bd01422291a3b36212101b
|
|
7
|
+
data.tar.gz: 410eadea7c619138940cf1ae5692d1d31061613f29bc737f4542e0ab67dffac09b31da111f465d3ac8c69fd69b93cacec7f24d673924e97a5b6228a9c20ca82c
|
|
@@ -15,6 +15,8 @@ module ShopCircle
|
|
|
15
15
|
@api_uri = URI.parse(config.api_url.to_s.chomp("/"))
|
|
16
16
|
@http = nil
|
|
17
17
|
@mutex = Mutex.new
|
|
18
|
+
@ingest_token = nil
|
|
19
|
+
@ingest_token_expires_at = 0
|
|
18
20
|
end
|
|
19
21
|
|
|
20
22
|
def send_single(event)
|
|
@@ -71,7 +73,17 @@ module ShopCircle
|
|
|
71
73
|
request["shopcircle-client-id"] = @config.client_id
|
|
72
74
|
request["shopcircle-sdk-name"] = SDK_NAME
|
|
73
75
|
request["shopcircle-sdk-version"] = ShopCircle::Orbit::VERSION
|
|
74
|
-
|
|
76
|
+
|
|
77
|
+
if @config.client_secret
|
|
78
|
+
request["shopcircle-client-secret"] = @config.client_secret
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# For public clients (no secret), fetch and attach an ingest token
|
|
82
|
+
if !@config.client_secret
|
|
83
|
+
token = fetch_ingest_token
|
|
84
|
+
request["shopcircle-ingest-token"] = token if token
|
|
85
|
+
end
|
|
86
|
+
|
|
75
87
|
request.body = body
|
|
76
88
|
|
|
77
89
|
response = @http.request(request)
|
|
@@ -120,6 +132,32 @@ module ShopCircle
|
|
|
120
132
|
end
|
|
121
133
|
end
|
|
122
134
|
|
|
135
|
+
# Fetch a short-lived ingest token for public clients.
|
|
136
|
+
# Tokens are cached and refreshed 60 seconds before expiry.
|
|
137
|
+
def fetch_ingest_token
|
|
138
|
+
now_ms = (Time.now.to_f * 1000).to_i
|
|
139
|
+
return @ingest_token if @ingest_token && now_ms < (@ingest_token_expires_at - 60_000)
|
|
140
|
+
|
|
141
|
+
ensure_connection
|
|
142
|
+
req = Net::HTTP::Post.new("/api/track/token")
|
|
143
|
+
req["Content-Type"] = "application/json"
|
|
144
|
+
req.body = JSON.generate({ clientId: @config.client_id })
|
|
145
|
+
|
|
146
|
+
resp = @http.request(req)
|
|
147
|
+
if resp.code.to_i == 200
|
|
148
|
+
data = JSON.parse(resp.body)
|
|
149
|
+
@ingest_token = data["token"]
|
|
150
|
+
@ingest_token_expires_at = data["expiresAt"].to_i
|
|
151
|
+
@ingest_token
|
|
152
|
+
else
|
|
153
|
+
log_error("Failed to fetch ingest token (#{resp.code})", resp.body)
|
|
154
|
+
nil
|
|
155
|
+
end
|
|
156
|
+
rescue StandardError => e
|
|
157
|
+
log_error("Ingest token fetch error", e.message)
|
|
158
|
+
nil
|
|
159
|
+
end
|
|
160
|
+
|
|
123
161
|
# Exponential backoff with jitter.
|
|
124
162
|
# Respects Retry-After header when present (429 responses).
|
|
125
163
|
def retry_delay(attempt, response = nil)
|