shopcircle-orbit 1.1.0 → 1.2.1
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 +38 -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: e3113ae248462ed96c7df563092cd89e421a4517c0ed6420c2abd5ab18ac9c34
|
|
4
|
+
data.tar.gz: f07313a22f1ab5c037f135b42576ee08db436c1e47c72fb4c35e96f33fed276b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6227c6ba47b0bd858fae94ce5a745b41e4b0277defe81cfd5c4b789401cc9e2316f1b9343051ec8fe0a5d88a7f588bcee91f80f7ced013260a4688c55fc6e002
|
|
7
|
+
data.tar.gz: 828d277ae8d56cf8742dd9648223dea7153917454175ddd07dae3465f3c24ac4f3e7a64222afe948db5d524a509ef012a11a9a83d9ec0697df05a190f58c4bb1
|
|
@@ -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,16 @@ 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
|
+
# Always attach an ingest token — required for public clients,
|
|
82
|
+
# accepted by secret-authenticated clients too.
|
|
83
|
+
token = fetch_ingest_token
|
|
84
|
+
request["shopcircle-ingest-token"] = token if token
|
|
85
|
+
|
|
75
86
|
request.body = body
|
|
76
87
|
|
|
77
88
|
response = @http.request(request)
|
|
@@ -120,6 +131,32 @@ module ShopCircle
|
|
|
120
131
|
end
|
|
121
132
|
end
|
|
122
133
|
|
|
134
|
+
# Fetch a short-lived ingest token for public clients.
|
|
135
|
+
# Tokens are cached and refreshed 60 seconds before expiry.
|
|
136
|
+
def fetch_ingest_token
|
|
137
|
+
now_ms = (Time.now.to_f * 1000).to_i
|
|
138
|
+
return @ingest_token if @ingest_token && now_ms < (@ingest_token_expires_at - 60_000)
|
|
139
|
+
|
|
140
|
+
ensure_connection
|
|
141
|
+
req = Net::HTTP::Post.new("/api/track/token")
|
|
142
|
+
req["Content-Type"] = "application/json"
|
|
143
|
+
req.body = JSON.generate({ clientId: @config.client_id })
|
|
144
|
+
|
|
145
|
+
resp = @http.request(req)
|
|
146
|
+
if resp.code.to_i == 200
|
|
147
|
+
data = JSON.parse(resp.body)
|
|
148
|
+
@ingest_token = data["token"]
|
|
149
|
+
@ingest_token_expires_at = data["expiresAt"].to_i
|
|
150
|
+
@ingest_token
|
|
151
|
+
else
|
|
152
|
+
log_error("Failed to fetch ingest token (#{resp.code})", resp.body)
|
|
153
|
+
nil
|
|
154
|
+
end
|
|
155
|
+
rescue StandardError => e
|
|
156
|
+
log_error("Ingest token fetch error", e.message)
|
|
157
|
+
nil
|
|
158
|
+
end
|
|
159
|
+
|
|
123
160
|
# Exponential backoff with jitter.
|
|
124
161
|
# Respects Retry-After header when present (429 responses).
|
|
125
162
|
def retry_delay(attempt, response = nil)
|