Class: MemoriClient::Proxy::Client

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

Overview

Initialize a DB Proxy Client, which connects to the AIsuru proxy client to interact with backend and engine databases, in read-only mode. Usage:

client = AisuruProxyClient.new(base_url: “https://…”, api_key: “XXX”)

Status: client.status

Tables list and table information client.tables(id: ‘backend’, schema: ‘memoriai’) client.table_schema(id: ‘backend’, schema: ‘memoriai’, table: ‘ActionLog’)

SQL Query sql = <<~SQL SELECT * FROM memoriai.“ActionLog” LIMIT 10 OFFSET 0 SQL client.query(id: ‘backend’, sql: sql)

Constant Summary collapse

CONNECTIONS =
['backend', 'engine'].freeze

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, api_key:) ⇒ Client

Returns a new instance of Client.

Parameters:

  • base_url (String)

    The base URL for the API

  • api_key (String)

    The API key for authentication



30
31
32
33
# File 'lib/memori_client/proxy/client.rb', line 30

def initialize(base_url:, api_key:)
  @base_url = base_url
  @api_key = api_key
end

Instance Method Details

#query(id:, sql:) ⇒ Object

Execute a SQL query

Parameters:

  • id (String)

    The connection ID

  • sql (String)

    The SQL query



77
78
79
80
81
82
83
84
85
86
# File 'lib/memori_client/proxy/client.rb', line 77

def query(id:, sql:)
  unless CONNECTIONS.include?(id)
    raise ArgumentError, "Invalid connection id: #{id}"
  end
  
  path = "api/v1/#{id}/query"
  uri = URI.join(@base_url, path)
  response = post_raw_content(uri, sql)
  [response.code, response.body]
end

#statusObject

Get the status of the API



36
37
38
39
40
41
# File 'lib/memori_client/proxy/client.rb', line 36

def status
  path = "api/v1/status"
  uri = URI.join(@base_url, path)
  response = get(uri)
  [response.code, response.body]
end

#table_schema(id:, schema: nil, table:) ⇒ Object

Get the schema of a specific table

Parameters:

  • id (String)

    The connection ID

  • schema (String) (defaults to: nil)

    The schema name (optional)

  • table (String)

    The table name



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

def table_schema(id:, schema: nil, table:)
  unless CONNECTIONS.include?(id)
    raise ArgumentError, "Invalid connection id: #{id}"
  end
  
  path = "api/v1/#{id}/tables/#{table}/schema"
  path += "?schema=#{schema}" if schema
  uri = URI.join(@base_url, path)
  response = get(uri)
  [response.code, response.body]
end

#tables(id:, schema: nil) ⇒ Object

Get a list of tables for a specific connection

Parameters:

  • id (String)

    The connection ID

  • schema (String) (defaults to: nil)

    The schema name (optional)



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/memori_client/proxy/client.rb', line 46

def tables(id:, schema: nil)
  unless CONNECTIONS.include?(id)
    raise ArgumentError, "Invalid connection id: #{id}"
  end

  path = "api/v1/#{id}/tables"
  path += "?schema=#{schema}" if schema
  uri = URI.join(@base_url, path)
  response = get(uri)
  [response.code, response.body]
end