Class: Tina4Ultipa::Client
- Inherits:
-
Object
- Object
- Tina4Ultipa::Client
- Defined in:
- lib/tina4_ultipa/client.rb
Instance Attribute Summary collapse
-
#connect_timeout ⇒ Object
readonly
Returns the value of attribute connect_timeout.
-
#graph ⇒ Object
Returns the value of attribute graph.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server_version ⇒ Object
readonly
Returns the value of attribute server_version.
-
#use_tls ⇒ Object
readonly
Returns the value of attribute use_tls.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.from_url(url, **kw) ⇒ Object
ultipa://user:pass@host:port/graph?connect_timeout=5&tls=1.
Instance Method Summary collapse
- #close ⇒ Object
-
#connect ⇒ Object
-- connection ---------------------------------------------------------.
-
#execute(gql, params: nil, graph: nil, timeout: nil) ⇒ Object
Run a writing GQL statement (read_only = false).
-
#initialize(host:, port: DEFAULT_PORT, username: nil, password: nil, graph: nil, connect_timeout: 10, use_tls: false) ⇒ Client
constructor
A new instance of Client.
-
#query(gql, params: nil, graph: nil, read_only: true, timeout: nil) ⇒ Object
Run a GQL statement and return a Result.
Constructor Details
#initialize(host:, port: DEFAULT_PORT, username: nil, password: nil, graph: nil, connect_timeout: 10, use_tls: false) ⇒ Client
Returns a new instance of Client.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/tina4_ultipa/client.rb', line 78 def initialize(host:, port: DEFAULT_PORT, username: nil, password: nil, graph: nil, connect_timeout: 10, use_tls: false) @host = host @port = port.to_i @username = username @password = password @graph = graph || "" @connect_timeout = connect_timeout.to_f @use_tls = use_tls @channel = nil @session = nil @query = nil @session_id = nil @server_version = nil end |
Instance Attribute Details
#connect_timeout ⇒ Object (readonly)
Returns the value of attribute connect_timeout.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def connect_timeout @connect_timeout end |
#graph ⇒ Object
Returns the value of attribute graph.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def graph @graph end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def port @port end |
#server_version ⇒ Object (readonly)
Returns the value of attribute server_version.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def server_version @server_version end |
#use_tls ⇒ Object (readonly)
Returns the value of attribute use_tls.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def use_tls @use_tls end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
74 75 76 |
# File 'lib/tina4_ultipa/client.rb', line 74 def username @username end |
Class Method Details
.from_url(url, **kw) ⇒ Object
ultipa://user:pass@host:port/graph?connect_timeout=5&tls=1
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/tina4_ultipa/client.rb', line 95 def self.from_url(url, **kw) u = URI.parse(url) q = URI.decode_www_form(u.query || "").to_h path_graph = (u.path || "").sub(%r{\A/}, "") new( host: u.host, port: (u.port || DEFAULT_PORT), username: (u.user || q["user"] || kw[:username]), password: (u.password || q["password"] || kw[:password]), graph: (path_graph.empty? ? kw[:graph] : path_graph), connect_timeout: (q["connect_timeout"] || kw[:connect_timeout] || 10).to_f, use_tls: (%w[1 true].include?(q["tls"]) || url.start_with?("ultipas://") || kw[:use_tls] || false), ) end |
Instance Method Details
#close ⇒ Object
167 168 169 170 171 172 173 |
# File 'lib/tina4_ultipa/client.rb', line 167 def close @channel.close if @channel.respond_to?(:close) rescue StandardError # best effort - the channel is being torn down regardless ensure @channel = @session = @query = @session_id = nil end |
#connect ⇒ Object
-- connection ---------------------------------------------------------
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/tina4_ultipa/client.rb', line 112 def connect return self unless @session_id.nil? target = "#{@host}:#{@port}" creds = @use_tls ? GRPC::Core::ChannelCredentials.new : :this_channel_is_insecure @channel = GRPC::Core::Channel.new(target, nil, creds) started = monotonic wait_for_ready(@channel, started) @session = Gqldb::SessionService::Stub.new(target, creds, channel_override: @channel) @query = Gqldb::QueryService::Stub.new(target, creds, channel_override: @channel) begin resp = @session.login( Gqldb::LoginRequest.new( username: @username || "", password: @password || "", default_graph: @graph, ), deadline: Time.now + @connect_timeout, ) rescue GRPC::BadStatus => e close raise wrap(e, "login failed") end @session_id = resp.session_id @server_version = resp.server_version self end |
#execute(gql, params: nil, graph: nil, timeout: nil) ⇒ Object
Run a writing GQL statement (read_only = false).
163 164 165 |
# File 'lib/tina4_ultipa/client.rb', line 163 def execute(gql, params: nil, graph: nil, timeout: nil) query(gql, params: params, graph: graph, read_only: false, timeout: timeout) end |
#query(gql, params: nil, graph: nil, read_only: true, timeout: nil) ⇒ Object
Run a GQL statement and return a Result. Raises on error.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/tina4_ultipa/client.rb', line 142 def query(gql, params: nil, graph: nil, read_only: true, timeout: nil) connect req = Gqldb::GqlRequest.new( gql: gql, graph_name: graph.nil? ? @graph : graph, parameters: (params || {}).map do |k, v| Gqldb::Parameter.new(name: k.to_s, value: Codec.encode(v)) end, session_id: @session_id || 0, read_only: read_only, timeout: timeout ? timeout.to_i : 0, ) begin resp = @query.gql(req, metadata: { "session-id" => @session_id.to_s }) rescue GRPC::BadStatus => e raise wrap(e, "gql failed") end Result.new(resp) end |