Class: DatabasusRuby::Client

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

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, url: nil, workspace_id: nil, adapter: Faraday.default_adapter) ⇒ Client

Returns a new instance of Client.

Raises:



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

#adapterObject (readonly)

Returns the value of attribute adapter.

Returns:



18
19
20
# File 'lib/databasus_ruby/client.rb', line 18

def adapter
  @adapter
end

#agent_idString?

Returns the value of attribute agent_id.

Returns:

  • (String, nil)


131
132
133
# File 'sig/databasus_ruby.rbs', line 131

def agent_id
  @agent_id
end

#api_keyString?

Returns the value of attribute api_key.

Returns:

  • (String, nil)


19
20
21
# File 'lib/databasus_ruby/client.rb', line 19

def api_key
  @api_key
end

#urlString (readonly)

Returns the value of attribute url.

Returns:

  • (String)


18
19
20
# File 'lib/databasus_ruby/client.rb', line 18

def url
  @url
end

#workspace_idString?

Returns the value of attribute workspace_id.

Returns:

  • (String, nil)


19
20
21
# File 'lib/databasus_ruby/client.rb', line 19

def workspace_id
  @workspace_id
end

Instance Method Details

#agent_id!String

Returns:

  • (String)


151
# File 'sig/databasus_ruby.rbs', line 151

def agent_id!: () -> String

#authAuth

Returns:



39
40
41
# File 'lib/databasus_ruby/client.rb', line 39

def auth
  @auth ||= Auth.new(self)
end

#backupsBackups

Returns:



43
44
45
# File 'lib/databasus_ruby/client.rb', line 43

def backups
  @backups ||= Backups.new(self)
end

#connectionObject

Returns:



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

#databasesDatabases

Returns:



47
48
49
# File 'lib/databasus_ruby/client.rb', line 47

def databases
  @databases ||= Databases.new(self)
end

#inspectString

Returns:

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

#notifiersNotifiers

Returns:



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

#storagesStorages

Returns:



55
56
57
# File 'lib/databasus_ruby/client.rb', line 55

def storages
  @storages ||= Storages.new(self)
end

#usersUsers

Returns:



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.

Returns:

  • (String)

Raises:



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

#workspacesWorkspaces

Returns:



63
64
65
# File 'lib/databasus_ruby/client.rb', line 63

def workspaces
  @workspaces ||= Workspaces.new(self)
end