Class: Supabase::Auth::Api
- Inherits:
-
Object
- Object
- Supabase::Auth::Api
- Defined in:
- lib/supabase/auth/api.rb
Direct Known Subclasses
Constant Summary collapse
- CONTENT_TYPE =
"application/json;charset=UTF-8"- UUID_REGEX =
/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/i
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#_request(method, path, jwt: nil, body: nil, params: {}, headers: {}, redirect_to: nil, xform: nil, no_resolve_json: false) ⇒ Hash, Object
Central HTTP dispatch method.
- #_validate_uuid(id) ⇒ Object
- #delete(path, headers: {}, params: {}) ⇒ Object
-
#get(path, headers: {}, params: {}) ⇒ Object
Convenience methods that delegate to _request.
-
#initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Api
constructor
A new instance of Api.
- #post(path, body: {}, headers: {}, params: {}) ⇒ Object
- #put(path, body: {}, headers: {}, params: {}) ⇒ Object
Constructor Details
#initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) ⇒ Api
Returns a new instance of Api.
20 21 22 23 24 25 26 27 |
# File 'lib/supabase/auth/api.rb', line 20 def initialize(url:, headers: {}, http_client: nil, verify: true, proxy: nil, timeout: nil) @url = url @headers = headers @http_client = http_client @verify = verify @proxy = proxy @timeout = timeout end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
12 13 14 |
# File 'lib/supabase/auth/api.rb', line 12 def headers @headers end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
12 13 14 |
# File 'lib/supabase/auth/api.rb', line 12 def url @url end |
Instance Method Details
#_request(method, path, jwt: nil, body: nil, params: {}, headers: {}, redirect_to: nil, xform: nil, no_resolve_json: false) ⇒ Hash, Object
Central HTTP dispatch method. Builds URL, merges headers (including API version and Authorization), handles redirect_to as query param, parses JSON, applies optional transform, and maps errors via Helpers.handle_exception.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/supabase/auth/api.rb', line 43 def _request(method, path, jwt: nil, body: nil, params: {}, headers: {}, redirect_to: nil, xform: nil, no_resolve_json: false) merged_headers = @headers.merge(headers) merged_headers["Content-Type"] ||= CONTENT_TYPE merged_headers[Constants::API_VERSION_HEADER_NAME] ||= Constants::API_VERSIONS.keys.last merged_headers["Authorization"] = "Bearer #{jwt}" if jwt query = params.dup query["redirect_to"] = redirect_to if redirect_to full_path = build_path(path) json_body = body ? JSON.generate(body) : nil response = connection.run_request(method.to_s.downcase.to_sym, full_path, json_body, merged_headers) do |req| req.params.update(query) unless query.empty? end result = no_resolve_json ? response : parse_response(response) xform ? xform.call(result) : result rescue Faraday::Error => e raise Helpers.handle_exception(e) rescue StandardError => e raise Helpers.handle_exception(e) end |
#_validate_uuid(id) ⇒ Object
70 71 72 73 74 |
# File 'lib/supabase/auth/api.rb', line 70 def _validate_uuid(id) unless id.is_a?(String) && id.match?(UUID_REGEX) raise ArgumentError, "Invalid id, '#{id}' is not a valid uuid" end end |
#delete(path, headers: {}, params: {}) ⇒ Object
90 91 92 |
# File 'lib/supabase/auth/api.rb', line 90 def delete(path, headers: {}, params: {}) _request(:delete, path, headers: headers, params: params) end |
#get(path, headers: {}, params: {}) ⇒ Object
Convenience methods that delegate to _request
78 79 80 |
# File 'lib/supabase/auth/api.rb', line 78 def get(path, headers: {}, params: {}) _request(:get, path, headers: headers, params: params) end |
#post(path, body: {}, headers: {}, params: {}) ⇒ Object
82 83 84 |
# File 'lib/supabase/auth/api.rb', line 82 def post(path, body: {}, headers: {}, params: {}) _request(:post, path, body: body, headers: headers, params: params) end |
#put(path, body: {}, headers: {}, params: {}) ⇒ Object
86 87 88 |
# File 'lib/supabase/auth/api.rb', line 86 def put(path, body: {}, headers: {}, params: {}) _request(:put, path, body: body, headers: headers, params: params) end |