Class: Supabase::Postgrest::RequestConfig

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(session:, path:, http_method:, headers: {}, params: {}, json: nil, retry_enabled: true) ⇒ RequestConfig

Returns a new instance of RequestConfig.

Parameters:

  • session (Faraday::Connection)


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

#headersObject

Returns the value of attribute headers.



16
17
18
# File 'lib/supabase/postgrest/request_builder.rb', line 16

def headers
  @headers
end

#http_methodObject

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

#jsonObject

Returns the value of attribute json.



16
17
18
# File 'lib/supabase/postgrest/request_builder.rb', line 16

def json
  @json
end

#paramsObject

Returns the value of attribute params.



16
17
18
# File 'lib/supabase/postgrest/request_builder.rb', line 16

def params
  @params
end

#pathObject

Returns the value of attribute path.



16
17
18
# File 'lib/supabase/postgrest/request_builder.rb', line 16

def path
  @path
end

#retry_enabledObject

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

#sessionObject

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

Returns:

  • (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