Class: DatabasusRuby::Client
- Inherits:
-
Object
- Object
- DatabasusRuby::Client
- Defined in:
- lib/databasus_ruby/client.rb,
sig/databasus_ruby.rbs
Overview
Entry point for the gem. Holds the connection and hands out one endpoint object per API domain.
client = DatabasusRuby::Client.new(
api_key: ENV["DATABASUS_API_KEY"],
url: "https://databasus.example.com/api/v1/"
)
client.databases.list(workspace_id: id)
api_key is sent as Authorization: Bearer <api_key>. Databasus accepts
two kinds of bearer token there: a user JWT (from Auth#signin, used by
every regular endpoint) and a verification-agent token (used by the
/agent/* endpoints, which also need agent_id).
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#agent_id ⇒ String?
Returns the value of attribute agent_id.
-
#api_key ⇒ String?
Returns the value of attribute api_key.
-
#url ⇒ String
readonly
Returns the value of attribute url.
-
#workspace_id ⇒ String?
Returns the value of attribute workspace_id.
Instance Method Summary collapse
- #agent_id! ⇒ String
- #auth ⇒ Auth
- #backups ⇒ Backups
- #connection ⇒ Object
- #databases ⇒ Databases
-
#initialize(api_key: nil, url: nil, workspace_id: nil, adapter: Faraday.default_adapter) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ String
- #notifiers ⇒ Notifiers
-
#request(method, path, params: nil, body: nil, headers: nil) ⇒ Object
Performs the request and raises the matching APIError subclass on a non-successful status.
- #storages ⇒ Storages
- #users ⇒ Users
-
#workspace_id! ⇒ String
The workspace-scoped list endpoints all require a workspace id; callers can either pass one per call or set a default on the client.
- #workspaces ⇒ Workspaces
Constructor Details
#initialize(api_key: nil, url: nil, workspace_id: nil, adapter: Faraday.default_adapter) ⇒ Client
Returns a new instance of Client.
21 22 23 24 25 26 27 28 |
# File 'lib/databasus_ruby/client.rb', line 21 def initialize(api_key: nil, url: nil, workspace_id: nil, adapter: Faraday.default_adapter) raise ConfigurationError, "url is required" if url.nil? || url.to_s.empty? @api_key = api_key @adapter = adapter @url = normalize_url(url) @workspace_id = workspace_id end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
18 19 20 |
# File 'lib/databasus_ruby/client.rb', line 18 def adapter @adapter end |
#agent_id ⇒ String?
Returns the value of attribute agent_id.
131 132 133 |
# File 'sig/databasus_ruby.rbs', line 131 def agent_id @agent_id end |
#api_key ⇒ String?
Returns the value of attribute api_key.
19 20 21 |
# File 'lib/databasus_ruby/client.rb', line 19 def api_key @api_key end |
#url ⇒ String (readonly)
Returns the value of attribute url.
18 19 20 |
# File 'lib/databasus_ruby/client.rb', line 18 def url @url end |
#workspace_id ⇒ String?
Returns the value of attribute workspace_id.
19 20 21 |
# File 'lib/databasus_ruby/client.rb', line 19 def workspace_id @workspace_id end |
Instance Method Details
#agent_id! ⇒ String
151 |
# File 'sig/databasus_ruby.rbs', line 151
def agent_id!: () -> String
|
#auth ⇒ Auth
39 40 41 |
# File 'lib/databasus_ruby/client.rb', line 39 def auth @auth ||= Auth.new(self) end |
#backups ⇒ Backups
43 44 45 |
# File 'lib/databasus_ruby/client.rb', line 43 def backups @backups ||= Backups.new(self) end |
#connection ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/databasus_ruby/client.rb', line 30 def connection @connection ||= Faraday.new do |conn| conn.url_prefix = url conn.request :json conn.response :json, content_type: "application/json" conn.adapter adapter end end |
#databases ⇒ Databases
47 48 49 |
# File 'lib/databasus_ruby/client.rb', line 47 def databases @databases ||= Databases.new(self) end |
#inspect ⇒ String
91 92 93 94 |
# File 'lib/databasus_ruby/client.rb', line 91 def inspect "#<#{self.class.name} url=#{url.inspect} workspace_id=#{workspace_id.inspect}\ api_key=#{api_key.nil? ? "nil" : "[FILTERED]"}>" end |
#notifiers ⇒ Notifiers
51 52 53 |
# File 'lib/databasus_ruby/client.rb', line 51 def notifiers @notifiers ||= Notifiers.new(self) end |
#request(method, path, params: nil, body: nil, headers: nil) ⇒ Object
Performs the request and raises the matching APIError subclass on a non-successful status. Returns the raw Faraday::Response so callers can decide how to interpret the body.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/databasus_ruby/client.rb', line 79 def request(method, path, params: nil, body: nil, headers: nil) response = connection.public_send(method, path) do |req| req.params.update(stringify(params)) if params req.body = body unless body.nil? req.headers.update(request_headers(headers)) end raise APIError.from_response(response) unless response.success? response end |
#storages ⇒ Storages
55 56 57 |
# File 'lib/databasus_ruby/client.rb', line 55 def storages @storages ||= Storages.new(self) end |
#users ⇒ Users
59 60 61 |
# File 'lib/databasus_ruby/client.rb', line 59 def users @users ||= Users.new(self) end |
#workspace_id! ⇒ String
The workspace-scoped list endpoints all require a workspace id; callers can either pass one per call or set a default on the client.
69 70 71 72 73 74 |
# File 'lib/databasus_ruby/client.rb', line 69 def workspace_id! return workspace_id unless workspace_id.nil? || workspace_id.to_s.empty? raise ConfigurationError, "workspace_id is required: pass workspace_id: to this call or to Client.new" end |
#workspaces ⇒ Workspaces
63 64 65 |
# File 'lib/databasus_ruby/client.rb', line 63 def workspaces @workspaces ||= Workspaces.new(self) end |