@@ -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