Class: Supabase::Postgrest::RequestConfig
- Inherits:
-
Object
- Object
- Supabase::Postgrest::RequestConfig
- Defined in:
- lib/supabase/postgrest/request_builder.rb
Overview
Internal: groups method, query params, headers, and JSON body for one PostgREST request.
Constant Summary collapse
- MAX_RETRIES =
3
Instance Attribute Summary collapse
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#http_method ⇒ Object
Returns the value of attribute http_method.
-
#json ⇒ Object
Returns the value of attribute json.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#retry_enabled ⇒ Object
Returns the value of attribute retry_enabled.
-
#session ⇒ Object
Returns the value of attribute session.
Instance Method Summary collapse
-
#initialize(session:, path:, http_method:, headers: {}, params: {}, json: nil, retry_enabled: true) ⇒ RequestConfig
constructor
A new instance of RequestConfig.
- #send_request(additional_headers = {}) ⇒ Object
- #should_retry?(response, attempt_count) ⇒ Boolean
Constructor Details
#initialize(session:, path:, http_method:, headers: {}, params: {}, json: nil, retry_enabled: true) ⇒ RequestConfig
Returns a new instance of RequestConfig.
19 20 21 22 23 24 25 26 27 |
# File 'lib/supabase/postgrest/request_builder.rb', line 19 def initialize(session:, path:, http_method:, headers: {}, params: {}, json: nil, retry_enabled: true) @session = session @path = path @http_method = http_method.to_s.upcase @headers = headers || {} @params = params || {} @json = %w[GET HEAD].include?(@http_method) ? nil : json @retry_enabled = retry_enabled end |
Instance Attribute Details
#headers ⇒ Object
Returns the value of attribute headers.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def headers @headers end |
#http_method ⇒ Object
Returns the value of attribute http_method.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def http_method @http_method end |
#json ⇒ Object
Returns the value of attribute json.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def json @json end |
#params ⇒ Object
Returns the value of attribute params.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def path @path end |
#retry_enabled ⇒ Object
Returns the value of attribute retry_enabled.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def retry_enabled @retry_enabled end |
#session ⇒ Object
Returns the value of attribute session.
16 17 18 |
# File 'lib/supabase/postgrest/request_builder.rb', line 16 def session @session end |
Instance Method Details
#send_request(additional_headers = {}) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/supabase/postgrest/request_builder.rb', line 29 def send_request(additional_headers = {}) merged_headers = @headers.merge(additional_headers) body = @json ? JSON.generate(@json) : nil @session.run_request(@http_method.downcase.to_sym, @path, body, merged_headers) do |req| req.params.update(@params) unless @params.empty? req.headers["Content-Type"] ||= "application/json" if body end end |
#should_retry?(response, attempt_count) ⇒ Boolean
39 40 41 42 43 44 45 |
# File 'lib/supabase/postgrest/request_builder.rb', line 39 def should_retry?(response, attempt_count) return false unless @retry_enabled return false if attempt_count >= MAX_RETRIES return false unless %w[GET HEAD].include?(@http_method) [503, 520].include?(response.status) end |