Skip to content

Commit b860396

Browse files
committed
BRD-20938 add tenant creation on 'invalid tenant name'
1 parent b16fef7 commit b860396

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

lib/scorm_engine/client.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ class Client
88
include Faraday::Request
99
include Api::Endpoints
1010

11-
attr_reader :tenant
11+
attr_reader :tenant, :tenant_creator
1212

13-
def initialize(tenant:)
13+
def initialize(tenant:, tenant_creator: nil)
1414
@tenant = tenant
15+
@tenant_creator = tenant_creator
1516
@api_version = 2 # Default to API v2
1617
end
1718

lib/scorm_engine/faraday/request.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,30 @@ def api_v1
3838

3939
def request(method, path, options, body = nil)
4040
api_version = @api_version || current_api_version
41+
@retry_attempted = false unless defined?(@retry_attempted)
4142

43+
begin
44+
make_request(method, path, options, body, api_version)
45+
rescue => e
46+
# Check if this is a tenant not found error and we have a tenant creator
47+
if should_retry_with_tenant_creation?(e) && @tenant_creator && !@retry_attempted
48+
@retry_attempted = true
49+
50+
begin
51+
@tenant_creator.call(@tenant)
52+
# Retry the original request
53+
make_request(method, path, options, body, api_version)
54+
rescue => retry_error
55+
# If tenant creation or retry fails, raise the original error
56+
raise e
57+
end
58+
else
59+
raise e
60+
end
61+
end
62+
end
63+
64+
def make_request(method, path, options, body, api_version)
4265
connection(version: api_version).send(method) do |request|
4366
if api_version == 2
4467
request.headers["engineTenantName"] = tenant unless @without_tenant
@@ -63,6 +86,22 @@ def request(method, path, options, body = nil)
6386
end
6487
end
6588

89+
def should_retry_with_tenant_creation?(error)
90+
return false unless error.respond_to?(:response)
91+
92+
response = error.response
93+
return false unless response
94+
95+
# Check for 400 status with tenant not found message
96+
if response.status == 400
97+
body = response.body
98+
body_text = body.to_s
99+
return body_text.include?("is not a valid tenant name")
100+
end
101+
102+
false
103+
end
104+
66105
def coerce_options(options = {})
67106
options.dup.each do |k, v|
68107
case k

0 commit comments

Comments
 (0)