Class: OctaSpace::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/octaspace/client.rb

Overview

Main entry point for the OctaSpace SDK

Aggregates all resource groups and constructs the appropriate HTTP transport based on configuration.

Examples:

Standard mode (default)

client = OctaSpace::Client.new(api_key: ENV["OCTA_API_KEY"])
client.nodes.list
client.accounts.balance

Keep-alive mode (persistent connections + pool)

client = OctaSpace::Client.new(
  api_key:    ENV["OCTA_API_KEY"],
  keep_alive: true,
  pool_size:  ENV.fetch("RAILS_MAX_THREADS", 5).to_i
)

Multiple API endpoints with failover

client = OctaSpace::Client.new(
  api_key:   ENV["OCTA_API_KEY"],
  base_urls: ["https://api.octa.space", "https://api2.octa.space"]
)

Without API key (public endpoints only)

client = OctaSpace::Client.new
client.network.info

With hooks

client = OctaSpace::Client.new(
  api_key:     ENV["OCTA_API_KEY"],
  on_request:  ->(req)  { puts "#{req[:method].upcase} #{req[:path]}" },
  on_response: ->(resp) { puts "#{resp.status}" }
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, transport: nil, **opts) ⇒ Client

Returns a new instance of Client.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    API key for authentication (optional for public endpoints)

  • opts (Hash)

    Per-instance configuration overrides. Any attribute from OctaSpace::Configuration can be passed here.



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/octaspace/client.rb', line 44

def initialize(api_key: nil, transport: nil, **opts)
  @config = build_config(api_key, opts)
  @transport = transport || build_transport
  @accounts = Resources::Accounts.new(@transport)
  @nodes = Resources::Nodes.new(@transport)
  @sessions = Resources::Sessions.new(@transport)
  @apps = Resources::Apps.new(@transport)
  @network = Resources::Network.new(@transport)
  @services = Resources::Services.new(@transport)
  @idle_jobs = Resources::IdleJobs.new(@transport)
end

Instance Attribute Details

#accountsObject (readonly)

Returns the value of attribute accounts.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def accounts
  @accounts
end

#appsObject (readonly)

Returns the value of attribute apps.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def apps
  @apps
end

#idle_jobsObject (readonly)

Returns the value of attribute idle_jobs.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def idle_jobs
  @idle_jobs
end

#networkObject (readonly)

Returns the value of attribute network.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def network
  @network
end

#nodesObject (readonly)

Returns the value of attribute nodes.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def nodes
  @nodes
end

#servicesObject (readonly)

Returns the value of attribute services.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def services
  @services
end

#sessionsObject (readonly)

Returns the value of attribute sessions.



38
39
40
# File 'lib/octaspace/client.rb', line 38

def sessions
  @sessions
end

Instance Method Details

#shutdownObject

Shut down persistent connections (only relevant in keep_alive mode)



57
58
59
# File 'lib/octaspace/client.rb', line 57

def shutdown
  @transport.respond_to?(:shutdown) && @transport.shutdown
end

#transport_statsHash

Transport diagnostics (pool stats when in keep_alive mode)

Returns:

  • (Hash)


63
64
65
66
67
68
69
70
71
72
73
# File 'lib/octaspace/client.rb', line 63

def transport_stats
  if @transport.respond_to?(:transport_stats)
    @transport.transport_stats
  elsif @transport.respond_to?(:pool_stats)
    {mode: :persistent, pools: @transport.pool_stats}
  elsif @config.urls.size > 1
    {mode: :standard, rotator: @transport.instance_variable_get(:@rotator)&.stats}
  else
    {mode: :standard, url: @config.urls.first}
  end
end