Class: ChalkRuby::GrpcClient
- Inherits:
-
Object
- Object
- ChalkRuby::GrpcClient
- Defined in:
- lib/chalk_ruby/grpc_client.rb
Class Method Summary collapse
-
.create(client_id = nil, client_secret = nil, environment = nil, query_server = nil, api_server = nil, additional_headers = {}) ⇒ Object
Create a new client.
-
.create_with_config(config) ⇒ Object
Create a new client providing only a ChalkRuby::Config object.
Instance Method Summary collapse
- #convert_to_proto_values(options_hash) ⇒ Object
- #get_token ⇒ Object
- #get_unauthenticated_headers ⇒ Object
-
#initialize(chalk_config, opts = {}) ⇒ GrpcClient
constructor
Initializes the ChalkRuby client.
-
#ping ⇒ Hash[Symbol, String]
Compute features values using online resolvers.
- #query(input:, output:, now: nil, staleness: nil, tags: nil, branch: nil, correlation_id: nil, query_name: nil, query_name_version: nil, meta: nil, explain: nil, include_meta: nil, store_plan_stages: nil, timeout: nil, planner_options: nil) ⇒ Object
- #query_bulk(input:, output:, now: nil, staleness: nil, context: nil, response_options: nil, body_type: nil, timeout: nil, query_name: nil, query_name_version: nil, correlation_id: nil, planner_options: nil) ⇒ Object
Constructor Details
#initialize(chalk_config, opts = {}) ⇒ GrpcClient
Initializes the ChalkRuby client. Generally, you should not need to call this directly. Instead, use ChalkRuby::Client.create or ChalkRuby::Client.create_with_config.
283 284 285 286 287 288 289 290 |
# File 'lib/chalk_ruby/grpc_client.rb', line 283 def initialize(chalk_config, opts = {}) @token = nil @config = chalk_config adapter = opts[:adapter] || Defaults::ADAPTER logger = opts[:logger] || LoggerHelper.create requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter: adapter, logger: logger) @transporter = Http::HttpRequesterChalk.new(requester: requester) end |
Class Method Details
.create(client_id = nil, client_secret = nil, environment = nil, query_server = nil, api_server = nil, additional_headers = {}) ⇒ Object
Create a new client.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/chalk_ruby/grpc_client.rb', line 40 def self.create( client_id = nil, client_secret = nil, environment = nil, query_server = nil, api_server = nil, additional_headers = {} ) config = Config.new( client_id: client_id, client_secret: client_secret, environment: environment, query_server: query_server, api_server: api_server, additional_headers: additional_headers ) create_with_config(config) end |
.create_with_config(config) ⇒ Object
Create a new client providing only a ChalkRuby::Config object
65 66 67 |
# File 'lib/chalk_ruby/grpc_client.rb', line 65 def self.create_with_config(config) new(config) end |
Instance Method Details
#convert_to_proto_values(options_hash) ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/chalk_ruby/grpc_client.rb', line 149 def convert_to_proto_values() return {} if .nil? .transform_values do |value| convert_to_protobuf_value(value) end end |
#get_token ⇒ Object
250 251 252 253 254 255 256 257 258 259 |
# File 'lib/chalk_ruby/grpc_client.rb', line 250 def get_token response = auth_service.get_token( Chalk::Server::V1::GetTokenRequest.new( client_id: @config.client_id, client_secret: @config.client_secret ) ) response.access_token end |
#get_unauthenticated_headers ⇒ Object
261 262 263 264 265 266 267 |
# File 'lib/chalk_ruby/grpc_client.rb', line 261 def get_unauthenticated_headers { 'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Chalk-Env-Id': @config.environment }.merge(@config.additional_headers) end |
#ping ⇒ Hash[Symbol, String]
Compute features values using online resolvers. See docs.chalk.ai/docs/query-basics for more information.
145 146 147 |
# File 'lib/chalk_ruby/grpc_client.rb', line 145 def ping query_service.ping(Chalk::Engine::V1::PingRequest.new(num: 1)) end |
#query(input:, output:, now: nil, staleness: nil, tags: nil, branch: nil, correlation_id: nil, query_name: nil, query_name_version: nil, meta: nil, explain: nil, include_meta: nil, store_plan_stages: nil, timeout: nil, planner_options: nil) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/chalk_ruby/grpc_client.rb', line 211 def query( input:, output:, now: nil, staleness: nil, tags: nil, branch: nil, correlation_id: nil, query_name: nil, query_name_version: nil, meta: nil, explain: nil, include_meta: nil, store_plan_stages: nil, timeout: nil, planner_options: nil ) formatted_inputs = input.transform_values { |value| self.convert_to_protobuf_value(value) } context = Chalk::Common::V1::OnlineQueryContext.new( query_name: query_name, query_name_version: query_name_version, correlation_id: correlation_id, options: || {} ) r = Chalk::Common::V1::OnlineQueryRequest.new( inputs: formatted_inputs, outputs: output.map { |o| Chalk::Common::V1::OutputExpr.new(feature_fqn: o) }, context: context ) if timeout.nil? query_service.online_query(r) else query_service.online_query(r, deadline: Time.now + timeout) end end |
#query_bulk(input:, output:, now: nil, staleness: nil, context: nil, response_options: nil, body_type: nil, timeout: nil, query_name: nil, query_name_version: nil, correlation_id: nil, planner_options: nil) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/chalk_ruby/grpc_client.rb', line 157 def query_bulk( input:, output:, now: nil, staleness: nil, context: nil, response_options: nil, body_type: nil, timeout: nil, query_name: nil, query_name_version: nil, correlation_id: nil, planner_options: nil ) unless red_arrow_available? raise NotImplementedError, "query_bulk requires the 'red-arrow' gem. Please add it to your Gemfile: gem 'red-arrow', '~> 18.0.0'" end # Convert input to feather format inputs_feather = to_feather(input) r = Chalk::Common::V1::OnlineQueryBulkRequest.new( inputs_feather: inputs_feather, outputs: output.map { |o| Chalk::Common::V1::OutputExpr.new(feature_fqn: o) }, staleness: staleness || {}, context: context || Chalk::Common::V1::OnlineQueryContext.new( query_name: query_name, query_name_version: query_name_version, correlation_id: correlation_id, options: convert_to_proto_values() ), response_options: || Chalk::Common::V1::OnlineQueryResponseOptions.new, body_type: body_type || :FEATHER_BODY_TYPE_UNSPECIFIED ) if timeout.nil? response = query_service.online_query_bulk(r) else response = query_service.online_query_bulk(r, deadline: Time.now + timeout) end output_data = nil if (!response.scalars_data.nil?) and response.scalars_data.length > 0 output_data = arrow_table_to_array(response.scalars_data) end { data: output_data, errors: response.errors, meta: response. } end |