chalk_ruby 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +0 -11
- data/README.md +3 -1
- data/lib/chalk_ruby/client.rb +146 -67
- data/lib/chalk_ruby/config.rb +61 -0
- data/lib/chalk_ruby/defaults.rb +7 -7
- data/lib/chalk_ruby/helpers.rb +0 -1
- data/lib/chalk_ruby/http/http_requester.rb +5 -7
- data/lib/chalk_ruby/{transport/transport.rb → http/http_requester_chalk.rb} +3 -4
- data/lib/chalk_ruby/token.rb +18 -0
- data/lib/chalk_ruby/version.rb +1 -1
- data/lib/chalk_ruby.rb +2 -3
- data/sig/chalk_ruby/client.rbs +22 -6
- data/sig/chalk_ruby/{config/config.rbs → config.rbs} +11 -9
- data/sig/chalk_ruby/defaults.rbs +1 -1
- data/sig/chalk_ruby/{transport/transport.rbs → http/http_requester_chalk.rbs} +3 -3
- data/test/chalk_ai/integration/client_test.rb +2 -3
- metadata +8 -7
- data/lib/chalk_ruby/config/config.rb +0 -39
- /data/sig/chalk_ruby/{interfaces → http}/_connection.rbs +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: be35291268749664bfdee468de9a997bd421f3b16683ffb847e12f3089f22e46
|
|
4
|
+
data.tar.gz: 3670d6543e87f11cee499ed8477729364e57a2072437ed798f1a721d7823e93d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 87b9e3a20075fda23a00ba5e38dc91827afcc18922b6b664e1fb31676623ce2d67627cd0d1a56831740fa03eeeaf99439841151073bfd1413b33e9811aa45281
|
|
7
|
+
data.tar.gz: 908b81be15e3c99571a7195f9464496b0f52bb5447f96728d82e2ed57365b5828dfef48c911a42d850ed9262a1bfb4c26b496cd7649e8a1512605c28a10a4c4f
|
data/.rubocop_todo.yml
CHANGED
|
@@ -1,14 +1,3 @@
|
|
|
1
|
-
# This configuration was generated by
|
|
2
|
-
# `rubocop --auto-gen-config`
|
|
3
|
-
# on 2019-12-12 11:07:55 +0100 using RuboCop version 0.77.0.
|
|
4
|
-
# The point is for the user to remove these configuration records
|
|
5
|
-
# one by one as the offenses are removed from the code base.
|
|
6
|
-
# Note that changes in the inspected code, or installation of new
|
|
7
|
-
# versions of RuboCop, may require this file to be generated again.
|
|
8
|
-
|
|
9
|
-
# Offense count: 1
|
|
10
1
|
Style/Documentation:
|
|
11
2
|
Exclude:
|
|
12
|
-
- 'spec/**/*'
|
|
13
3
|
- 'test/**/*'
|
|
14
|
-
- 'lib/rubybundle/search_config.rb'
|
data/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Ruby client for ChalkRuby
|
|
4
4
|
|
|
5
|
+
https://rubygems.org/gems/chalk_ruby
|
|
6
|
+
|
|
5
7
|
## Getting Started
|
|
6
8
|
|
|
7
9
|
First, install ChalkRuby Ruby API Client via the [RubyGems](https://rubygems.org/) package manager:
|
|
@@ -22,4 +24,4 @@ results = client.query(
|
|
|
22
24
|
|
|
23
25
|
## License
|
|
24
26
|
|
|
25
|
-
The
|
|
27
|
+
The Chalk Ruby API Client is an open-sourced software licensed under the [Apache 2 License](LICENSE.md).
|
data/lib/chalk_ruby/client.rb
CHANGED
|
@@ -1,65 +1,51 @@
|
|
|
1
|
-
require 'chalk_ruby/config
|
|
1
|
+
require 'chalk_ruby/config'
|
|
2
2
|
require 'chalk_ruby/logger_helper'
|
|
3
|
-
require 'chalk_ruby/
|
|
3
|
+
require 'chalk_ruby/http/http_requester_chalk'
|
|
4
4
|
require 'chalk_ruby/http/response'
|
|
5
|
+
require 'chalk_ruby/token'
|
|
5
6
|
|
|
6
7
|
module ChalkRuby
|
|
7
|
-
class Token
|
|
8
|
-
attr_accessor :token, :expires_at, :environment, :engines
|
|
9
|
-
|
|
10
|
-
# @param [Hash[String, untyped]] exchange_credentials_response
|
|
11
|
-
def initialize(exchange_credentials_response)
|
|
12
|
-
@token = exchange_credentials_response[:access_token]
|
|
13
|
-
@expires_at = DateTime.parse(exchange_credentials_response[:expires_at])
|
|
14
|
-
@environment = exchange_credentials_response[:primary_environment]
|
|
15
|
-
@engines = exchange_credentials_response[:engines]
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
def expired?
|
|
19
|
-
DateTime.now.advance({ seconds: 60 }) > @expires_at
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
8
|
class Client
|
|
24
|
-
#
|
|
9
|
+
# Create a new client.
|
|
25
10
|
#
|
|
26
|
-
# @param
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
# @option http_requester [Object] http_requester object used for the connection
|
|
11
|
+
# @param client_id [String]
|
|
12
|
+
# Chalk client ID.
|
|
13
|
+
# If not provided, it will be read from the CHALK_CLIENT_ID environment variable.
|
|
30
14
|
#
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Create a new client providing only app ID and API key
|
|
15
|
+
# @param client_secret [String]
|
|
16
|
+
# Chalk client secret.
|
|
17
|
+
# If not provided, it will be read from the CHALK_CLIENT_SECRET environment variable.
|
|
18
|
+
#
|
|
19
|
+
# @param environment [String]
|
|
20
|
+
# The Chalk environment id (not the name of the environment).
|
|
21
|
+
# If not provided, it will be read from the CHALK_ACTIVE_ENVIRONMENT environment variable.
|
|
41
22
|
#
|
|
42
|
-
# @param
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
# @param
|
|
46
|
-
#
|
|
23
|
+
# @param query_server [String]
|
|
24
|
+
# ChalkRuby query server
|
|
25
|
+
#
|
|
26
|
+
# @param api_server [String]
|
|
27
|
+
# ChalkRuby API server
|
|
28
|
+
#
|
|
29
|
+
# @param additional_headers [Hash[String, String]]
|
|
30
|
+
# Additional headers to be sent with every request. Typically not required.
|
|
47
31
|
#
|
|
48
32
|
# @return self
|
|
49
33
|
#
|
|
50
34
|
def self.create(
|
|
51
|
-
client_id,
|
|
52
|
-
client_secret,
|
|
35
|
+
client_id = nil,
|
|
36
|
+
client_secret = nil,
|
|
53
37
|
environment = nil,
|
|
54
38
|
query_server = nil,
|
|
55
|
-
api_server = nil
|
|
39
|
+
api_server = nil,
|
|
40
|
+
additional_headers = {}
|
|
56
41
|
)
|
|
57
42
|
config = Config.new(
|
|
58
43
|
client_id: client_id,
|
|
59
44
|
client_secret: client_secret,
|
|
60
45
|
environment: environment,
|
|
61
46
|
query_server: query_server,
|
|
62
|
-
api_server: api_server
|
|
47
|
+
api_server: api_server,
|
|
48
|
+
additional_headers: additional_headers
|
|
63
49
|
)
|
|
64
50
|
create_with_config(config)
|
|
65
51
|
end
|
|
@@ -87,46 +73,137 @@ module ChalkRuby
|
|
|
87
73
|
# is defined on the `User` namespace, and all features on the `User` namespace
|
|
88
74
|
# (excluding has-one and has-many relationships) will be used as outputs.
|
|
89
75
|
#
|
|
76
|
+
# @param now [DateTime?]
|
|
77
|
+
# The time at which to evaluate the query. If not specified, the current time will be used.
|
|
78
|
+
# This parameter is complex in the context of online_query since the online store
|
|
79
|
+
# only stores the most recent value of an entity's features. If `now` is in the past,
|
|
80
|
+
# it is extremely likely that `None` will be returned for cache-only features.
|
|
81
|
+
#
|
|
82
|
+
# This parameter is primarily provided to support:
|
|
83
|
+
# - controlling the time window for aggregations over cached has-many relationships
|
|
84
|
+
# - controlling the time window for aggregations over has-many relationships loaded from an
|
|
85
|
+
# external database
|
|
86
|
+
#
|
|
87
|
+
# If you are trying to perform an exploratory analysis of past feature values, prefer `offline_query`.
|
|
88
|
+
#
|
|
89
|
+
# @param staleness [Hash[String, String]?]
|
|
90
|
+
# Maximum staleness overrides for any output features or intermediate features.
|
|
91
|
+
# See https://docs.chalk.ai/docs/query-caching for more information.
|
|
92
|
+
#
|
|
93
|
+
# @param tags [Hash[String, String]?]
|
|
94
|
+
# The tags used to scope the resolvers.
|
|
95
|
+
# See https://docs.chalk.ai/docs/resolver-tags for more information.
|
|
96
|
+
#
|
|
97
|
+
# @param branch [String?]
|
|
98
|
+
# If specified, Chalk will route your request to the relevant branch.
|
|
99
|
+
#
|
|
100
|
+
# @param correlation_id [String?]
|
|
101
|
+
# You can specify a correlation ID to be used in logs and web interfaces.
|
|
102
|
+
# This should be globally unique, i.e. a `uuid` or similar. Logs generated
|
|
103
|
+
# during the execution of your query will be tagged with this correlation id.
|
|
104
|
+
#
|
|
105
|
+
# @param query_name [String?]
|
|
106
|
+
# The semantic name for the query you're making, for example, `"loan_application_model"`.
|
|
107
|
+
# Typically, each query that you make from your application should have a name.
|
|
108
|
+
# Chalk will present metrics and dashboard functionality grouped by 'query_name'.
|
|
109
|
+
#
|
|
110
|
+
# @param meta [Hash[String, String]?]
|
|
111
|
+
# Arbitrary `key:value` pairs to associate with a query.
|
|
112
|
+
#
|
|
113
|
+
# @param explain [Boolean?]
|
|
114
|
+
# Log the query execution plan. Requests using `explain=true` will be slower
|
|
115
|
+
# than requests using `explain=false`. If `"only"`, the query will not be executed,
|
|
116
|
+
# and only the query plan will be returned.
|
|
117
|
+
#
|
|
118
|
+
# If true, 'include_meta' will be set to true as well.
|
|
119
|
+
#
|
|
120
|
+
# @param include_meta [Boolean?]
|
|
121
|
+
# Whether to include metadata about the query execution.
|
|
122
|
+
#
|
|
123
|
+
# @param store_plan_stages [Boolean?]
|
|
124
|
+
# If `true`, the output of each of the query plan stages will be stored.
|
|
125
|
+
# This option dramatically impacts the performance of the query,
|
|
126
|
+
# so it should only be used for debugging.
|
|
90
127
|
#
|
|
91
128
|
# @return [Hash[Symbol, String]]
|
|
92
129
|
#
|
|
93
130
|
def query(
|
|
94
131
|
input:,
|
|
95
132
|
output:,
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
133
|
+
now: nil,
|
|
134
|
+
staleness: nil,
|
|
135
|
+
tags: nil,
|
|
136
|
+
branch: nil,
|
|
137
|
+
correlation_id: nil,
|
|
138
|
+
query_name: nil,
|
|
139
|
+
meta: nil,
|
|
140
|
+
explain: nil,
|
|
141
|
+
include_meta: nil,
|
|
142
|
+
store_plan_stages: nil
|
|
99
143
|
)
|
|
100
144
|
query_server_request(
|
|
101
145
|
method: :post,
|
|
102
146
|
path: 'v1/query/online',
|
|
103
147
|
body: {
|
|
104
148
|
inputs: input,
|
|
105
|
-
outputs: output
|
|
106
|
-
|
|
149
|
+
outputs: output,
|
|
150
|
+
now: now,
|
|
151
|
+
staleness: staleness,
|
|
152
|
+
context: tags && { tags: tags },
|
|
153
|
+
branch_id: branch,
|
|
154
|
+
correlation_id: correlation_id,
|
|
155
|
+
query_name: query_name,
|
|
156
|
+
meta: meta,
|
|
157
|
+
explain: explain || false,
|
|
158
|
+
include_meta: include_meta || false,
|
|
159
|
+
store_plan_stages: store_plan_stages || false
|
|
107
160
|
},
|
|
108
|
-
headers:
|
|
161
|
+
headers: get_authenticated_engine_headers(branch: branch)
|
|
109
162
|
)
|
|
110
163
|
end
|
|
111
164
|
|
|
112
165
|
def get_token
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
166
|
+
Token.new(
|
|
167
|
+
api_server_request(
|
|
168
|
+
method: :post,
|
|
169
|
+
path: '/v1/oauth/token',
|
|
170
|
+
body: {
|
|
171
|
+
client_id: @config.client_id,
|
|
172
|
+
client_secret: @config.client_secret,
|
|
173
|
+
grant_type: 'client_credentials'
|
|
174
|
+
},
|
|
175
|
+
headers: get_unauthenticated_headers
|
|
176
|
+
)
|
|
122
177
|
)
|
|
123
|
-
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
# Initializes the ChalkRuby client. Generally, you should not need to call this directly.
|
|
181
|
+
# Instead, use ChalkRuby::Client.create or ChalkRuby::Client.create_with_config.
|
|
182
|
+
#
|
|
183
|
+
# @param chalk_config [ChalkRuby::Config]
|
|
184
|
+
# A ChalkRuby::Config object which contains your CLIENT_ID and CLIENT_SECRET
|
|
185
|
+
#
|
|
186
|
+
# @option adapter [Object]
|
|
187
|
+
# Adapter object used for the connection
|
|
188
|
+
#
|
|
189
|
+
# @option logger [Object]
|
|
190
|
+
#
|
|
191
|
+
# @option http_requester [Object]
|
|
192
|
+
# object used for the connection
|
|
193
|
+
#
|
|
194
|
+
def initialize(chalk_config, opts = {})
|
|
195
|
+
@token = nil
|
|
196
|
+
@config = chalk_config
|
|
197
|
+
adapter = opts[:adapter] || Defaults::ADAPTER
|
|
198
|
+
logger = opts[:logger] || LoggerHelper.create
|
|
199
|
+
requester = opts[:http_requester] || Defaults::REQUESTER_CLASS.new(adapter: adapter, logger: logger)
|
|
200
|
+
@transporter = Http::HttpRequesterChalk.new(requester: requester)
|
|
124
201
|
end
|
|
125
202
|
|
|
126
203
|
private
|
|
127
204
|
|
|
128
205
|
def api_server_request(method:, path:, body:, headers:)
|
|
129
|
-
@transporter.
|
|
206
|
+
@transporter.send_request(
|
|
130
207
|
method: method,
|
|
131
208
|
host: @config.api_server,
|
|
132
209
|
path: path,
|
|
@@ -138,7 +215,7 @@ module ChalkRuby
|
|
|
138
215
|
end
|
|
139
216
|
|
|
140
217
|
def query_server_request(method:, path:, body:, headers:)
|
|
141
|
-
@transporter.
|
|
218
|
+
@transporter.send_request(
|
|
142
219
|
method: method,
|
|
143
220
|
host: query_server_host,
|
|
144
221
|
path: path,
|
|
@@ -149,22 +226,25 @@ module ChalkRuby
|
|
|
149
226
|
)
|
|
150
227
|
end
|
|
151
228
|
|
|
152
|
-
def
|
|
153
|
-
t
|
|
154
|
-
{
|
|
229
|
+
def get_authenticated_engine_headers(branch:)
|
|
230
|
+
t = valid_token
|
|
231
|
+
tokens = {
|
|
155
232
|
'Content-Type': 'application/json',
|
|
156
233
|
'Accept': 'application/json',
|
|
157
234
|
'Authorization': 'Bearer ' + t.token,
|
|
158
|
-
'X-
|
|
235
|
+
'X-Chalk-Env-Id': @config.environment || t.environment,
|
|
236
|
+
'X-Chalk-Branch-Id': branch,
|
|
237
|
+
'X-Chalk-Deployment-Type': branch.nil? ? 'engine' : 'branch'
|
|
159
238
|
}
|
|
239
|
+
tokens.merge(@config.additional_headers)
|
|
160
240
|
end
|
|
161
241
|
|
|
162
242
|
def get_unauthenticated_headers
|
|
163
243
|
{
|
|
164
244
|
'Content-Type': 'application/json',
|
|
165
245
|
'Accept': 'application/json',
|
|
166
|
-
'X-
|
|
167
|
-
}
|
|
246
|
+
'X-Chalk-Env-Id': @config.environment
|
|
247
|
+
}.merge(@config.additional_headers)
|
|
168
248
|
end
|
|
169
249
|
|
|
170
250
|
def query_server_host
|
|
@@ -187,6 +267,5 @@ module ChalkRuby
|
|
|
187
267
|
@token
|
|
188
268
|
end
|
|
189
269
|
end
|
|
190
|
-
|
|
191
270
|
end
|
|
192
271
|
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
require 'faraday'
|
|
2
|
+
require 'chalk_ruby/defaults'
|
|
3
|
+
|
|
4
|
+
module ChalkRuby
|
|
5
|
+
class Config
|
|
6
|
+
attr_accessor :client_id,
|
|
7
|
+
:client_secret,
|
|
8
|
+
:query_server,
|
|
9
|
+
:api_server,
|
|
10
|
+
:environment,
|
|
11
|
+
:query_timeout,
|
|
12
|
+
:api_timeout,
|
|
13
|
+
:connect_timeout,
|
|
14
|
+
:additional_headers
|
|
15
|
+
|
|
16
|
+
# Creates a new ChalkRuby::Config object for use with ChalkRuby::Client.
|
|
17
|
+
#
|
|
18
|
+
# @option options [String?] :client_id
|
|
19
|
+
# The Chalk client ID.
|
|
20
|
+
# If not provided, it will be read from the CHALK_CLIENT_ID environment variable.
|
|
21
|
+
#
|
|
22
|
+
# @option options [String?] :client_secret
|
|
23
|
+
# Chalk client secret.
|
|
24
|
+
# If not provided, it will be read from the CHALK_CLIENT_SECRET environment variable.
|
|
25
|
+
#
|
|
26
|
+
# @option options [String?] :query_host
|
|
27
|
+
# The query server to use.
|
|
28
|
+
# If not provided, it will be read from the CHALK_QUERY_SERVER environment variable.
|
|
29
|
+
#
|
|
30
|
+
# @option options [String?] :api_host
|
|
31
|
+
# The API server to use.
|
|
32
|
+
# If not provided, it will be read from the CHALK_API_SERVER environment variable.
|
|
33
|
+
#
|
|
34
|
+
# @option options [Integer?] :read_timeout
|
|
35
|
+
# The timeout for read operations (in seconds).
|
|
36
|
+
#
|
|
37
|
+
# @option options [Integer?] :write_timeout
|
|
38
|
+
# The timeout for write operations (in seconds).
|
|
39
|
+
#
|
|
40
|
+
# @option options [Integer?] :connect_timeout
|
|
41
|
+
# The timeout for connect operations (in seconds).
|
|
42
|
+
#
|
|
43
|
+
# @option options [Hash<String, String>?] :additional_headers
|
|
44
|
+
# Additional headers to be sent with every request. Typically not required.
|
|
45
|
+
#
|
|
46
|
+
def initialize(opts = {})
|
|
47
|
+
@client_id = opts[:client_id] || ENV['CHALK_CLIENT_ID']
|
|
48
|
+
@client_secret = opts[:client_secret] || ENV['CHALK_CLIENT_SECRET']
|
|
49
|
+
@environment = opts[:environment] || ENV['CHALK_ACTIVE_ENVIRONMENT']
|
|
50
|
+
@query_server = opts[:query_server] || ENV['CHALK_QUERY_SERVER'] || Defaults::QUERY_SERVER
|
|
51
|
+
@api_server = opts[:api_server] || ENV['CHALK_API_SERVER'] || Defaults::API_SERVER
|
|
52
|
+
@query_timeout = opts[:query_timeout] || Defaults::API_TIMEOUT
|
|
53
|
+
@api_timeout = opts[:api_timeout] || Defaults::QUERY_TIMEOUT
|
|
54
|
+
@connect_timeout = opts[:connect_timeout] || Defaults::CONNECT_TIMEOUT
|
|
55
|
+
@additional_headers = opts[:additional_headers] || {}
|
|
56
|
+
|
|
57
|
+
raise ChalkError, 'No Client ID provided, please set :client_id' if @client_id.nil?
|
|
58
|
+
raise ChalkError, 'No Client Secret provided, please set :client_secret' if @client_secret.nil?
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
data/lib/chalk_ruby/defaults.rb
CHANGED
|
@@ -2,14 +2,14 @@ require 'chalk_ruby/http/http_requester'
|
|
|
2
2
|
|
|
3
3
|
module ChalkRuby
|
|
4
4
|
module Defaults
|
|
5
|
-
REQUESTER_CLASS =
|
|
5
|
+
REQUESTER_CLASS = Http::HttpRequester
|
|
6
6
|
ADAPTER = 'net_http_persistent'
|
|
7
7
|
|
|
8
8
|
# HTTP Headers
|
|
9
9
|
# ----------------------------------------
|
|
10
|
-
HEADER_CLIENT_ID = 'X-
|
|
11
|
-
HEADER_CLIENT_SECRET = 'X-
|
|
12
|
-
HEADER_ENVIRONMENT = 'X-
|
|
10
|
+
HEADER_CLIENT_ID = 'X-Chalk-Client-Id'.freeze
|
|
11
|
+
HEADER_CLIENT_SECRET = 'X-Chalk-Client-Secret'.freeze
|
|
12
|
+
HEADER_ENVIRONMENT = 'X-Chalk-Env-Id'.freeze
|
|
13
13
|
AUTHORIZATION_HEADER = 'Authorization'.freeze
|
|
14
14
|
USER_AGENT = "ChalkRuby Ruby (#{ChalkRuby::VERSION}), Ruby (#{RUBY_VERSION})"
|
|
15
15
|
|
|
@@ -27,9 +27,9 @@ module ChalkRuby
|
|
|
27
27
|
|
|
28
28
|
# HTTP TIMEOUTS
|
|
29
29
|
# ----------------------------------------
|
|
30
|
-
CONNECT_TIMEOUT =
|
|
31
|
-
API_TIMEOUT =
|
|
32
|
-
QUERY_TIMEOUT =
|
|
30
|
+
CONNECT_TIMEOUT = 100
|
|
31
|
+
API_TIMEOUT = 300
|
|
32
|
+
QUERY_TIMEOUT = 1200
|
|
33
33
|
|
|
34
34
|
WAIT_TASK_DEFAULT_TIME_BEFORE_RETRY = 100
|
|
35
35
|
end
|
data/lib/chalk_ruby/helpers.rb
CHANGED
|
@@ -27,7 +27,7 @@ module ChalkRuby
|
|
|
27
27
|
# @param timeout [Integer]
|
|
28
28
|
# @param connect_timeout [Integer]
|
|
29
29
|
#
|
|
30
|
-
# @return [
|
|
30
|
+
# @return [Response]
|
|
31
31
|
#
|
|
32
32
|
def send_request(
|
|
33
33
|
host:,
|
|
@@ -57,24 +57,23 @@ module ChalkRuby
|
|
|
57
57
|
if ENV['CHALK_DEBUG']
|
|
58
58
|
@logger.info("Request succeeded. Response status: #{response.status}, body: #{response.body}")
|
|
59
59
|
end
|
|
60
|
-
return
|
|
60
|
+
return Response.new(status: response.status, body: response.body, headers: response.headers)
|
|
61
61
|
end
|
|
62
62
|
|
|
63
63
|
if ENV['CHALK_DEBUG']
|
|
64
64
|
@logger.info("Request failed. Response status: #{response.status}, error: #{response.body}")
|
|
65
65
|
end
|
|
66
|
-
|
|
66
|
+
Response.new(status: response.status, error: response.body, headers: response.headers)
|
|
67
67
|
rescue Faraday::TimeoutError => e
|
|
68
68
|
if ENV['CHALK_DEBUG']
|
|
69
69
|
@logger.info("Request timed out. Error: #{e.message}")
|
|
70
70
|
end
|
|
71
|
-
|
|
71
|
+
Response.new(error: e.message, has_timed_out: true)
|
|
72
72
|
rescue ::StandardError => e
|
|
73
73
|
if ENV['CHALK_DEBUG']
|
|
74
74
|
@logger.info("Request failed. Error: #{e.message}")
|
|
75
75
|
end
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
Response.new(error: e.message, network_failure: true)
|
|
78
77
|
end
|
|
79
78
|
|
|
80
79
|
# Retrieve the connection from the @connections
|
|
@@ -88,7 +87,6 @@ module ChalkRuby
|
|
|
88
87
|
f.adapter @adapter.to_sym
|
|
89
88
|
end
|
|
90
89
|
end
|
|
91
|
-
|
|
92
90
|
end
|
|
93
91
|
end
|
|
94
92
|
end
|
|
@@ -4,8 +4,8 @@ require 'faraday/net_http_persistent' unless Faraday::VERSION < '1'
|
|
|
4
4
|
require 'chalk_ruby/error'
|
|
5
5
|
|
|
6
6
|
module ChalkRuby
|
|
7
|
-
module
|
|
8
|
-
class
|
|
7
|
+
module Http
|
|
8
|
+
class HttpRequesterChalk
|
|
9
9
|
include Helpers
|
|
10
10
|
|
|
11
11
|
# @param requester [ChalkRuby::Http::HttpRequester] requester used for sending requests.
|
|
@@ -14,7 +14,7 @@ module ChalkRuby
|
|
|
14
14
|
@http_requester = requester
|
|
15
15
|
end
|
|
16
16
|
|
|
17
|
-
def
|
|
17
|
+
def send_request(method:, host:, path:, timeout:, connect_timeout:, headers:, body:)
|
|
18
18
|
response = @http_requester.send_request(
|
|
19
19
|
host: host,
|
|
20
20
|
method: method,
|
|
@@ -48,7 +48,6 @@ module ChalkRuby
|
|
|
48
48
|
def success?(http_response_code:)
|
|
49
49
|
!http_response_code.nil? && (http_response_code.to_i / 100).floor == 2
|
|
50
50
|
end
|
|
51
|
-
|
|
52
51
|
end
|
|
53
52
|
end
|
|
54
53
|
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module ChalkRuby
|
|
2
|
+
class Token
|
|
3
|
+
attr_accessor :token, :expires_at, :environment, :engines
|
|
4
|
+
|
|
5
|
+
# @param [Hash[String, untyped]] exchange_credentials_response
|
|
6
|
+
def initialize(exchange_credentials_response)
|
|
7
|
+
@token = exchange_credentials_response[:access_token]
|
|
8
|
+
@expires_at = DateTime.parse(exchange_credentials_response[:expires_at])
|
|
9
|
+
@environment = exchange_credentials_response[:primary_environment]
|
|
10
|
+
@engines = exchange_credentials_response[:engines]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def expired?
|
|
14
|
+
DateTime.now.advance({ seconds: 60 }) > @expires_at
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
data/lib/chalk_ruby/version.rb
CHANGED
data/lib/chalk_ruby.rb
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
# ChalkRuby module
|
|
2
1
|
module ChalkRuby
|
|
3
2
|
require 'chalk_ruby/version'
|
|
4
3
|
require 'chalk_ruby/helpers'
|
|
5
4
|
require 'chalk_ruby/http/http_requester'
|
|
6
5
|
require 'chalk_ruby/defaults'
|
|
7
|
-
require 'chalk_ruby/config
|
|
6
|
+
require 'chalk_ruby/config'
|
|
8
7
|
require 'chalk_ruby/http/response'
|
|
9
|
-
require 'chalk_ruby/
|
|
8
|
+
require 'chalk_ruby/http/http_requester_chalk'
|
|
10
9
|
require 'chalk_ruby/client'
|
|
11
10
|
require 'chalk_ruby/error'
|
|
12
11
|
require 'chalk_ruby/logger_helper'
|
data/sig/chalk_ruby/client.rbs
CHANGED
|
@@ -2,24 +2,40 @@ module ChalkRuby
|
|
|
2
2
|
class Client
|
|
3
3
|
@config: Config
|
|
4
4
|
@token: Token?
|
|
5
|
-
@transporter:
|
|
5
|
+
@transporter: Http::HttpRequesterChalk
|
|
6
6
|
|
|
7
|
-
def self.create:
|
|
7
|
+
def self.create: (
|
|
8
|
+
String? client_id,
|
|
9
|
+
String? client_secret,
|
|
10
|
+
String? environment,
|
|
11
|
+
String? query_server,
|
|
12
|
+
String? api_server,
|
|
13
|
+
Hash[String, String]? additional_headers
|
|
14
|
+
) -> Client
|
|
8
15
|
|
|
9
16
|
def self.create_with_config: -> Client
|
|
10
17
|
|
|
11
18
|
def get_token: -> Token
|
|
12
19
|
|
|
13
20
|
def query: (
|
|
14
|
-
|
|
21
|
+
Hash[Symbol | String, untyped] input,
|
|
15
22
|
[String] output,
|
|
16
|
-
Time
|
|
17
|
-
|
|
23
|
+
now: Time?,
|
|
24
|
+
staleness: Hash[String, String],
|
|
25
|
+
tags: [String]?,
|
|
26
|
+
branch: String?,
|
|
27
|
+
correlation_id: String?,
|
|
28
|
+
query_name: String?,
|
|
29
|
+
meta: Hash[String, String]?,
|
|
30
|
+
explain: bool,
|
|
31
|
+
include_meta: bool,
|
|
32
|
+
store_plan_stages: bool
|
|
18
33
|
) -> Hash[Symbol, untyped]
|
|
19
34
|
|
|
20
35
|
private
|
|
21
36
|
|
|
22
|
-
def
|
|
37
|
+
def get_authenticated_engine_headers: (String? branch) -> Hash[Symbol, String | nil]
|
|
38
|
+
|
|
23
39
|
def get_unauthenticated_headers: -> Hash[Symbol, String | nil]
|
|
24
40
|
|
|
25
41
|
def query_server_host: -> String
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
module ChalkRuby
|
|
2
2
|
class Config
|
|
3
|
+
attr_accessor additional_headers: Hash[String, String]?
|
|
3
4
|
attr_accessor client_id: String
|
|
4
5
|
attr_accessor client_secret: String
|
|
5
6
|
|
|
@@ -14,15 +15,16 @@ module ChalkRuby
|
|
|
14
15
|
attr_accessor connect_timeout: Integer
|
|
15
16
|
|
|
16
17
|
type options = {
|
|
17
|
-
client_id: String
|
|
18
|
-
client_secret: String
|
|
19
|
-
environment: String
|
|
20
|
-
api_server: String
|
|
21
|
-
query_server: String
|
|
22
|
-
query_timeout: Integer
|
|
23
|
-
api_timeout: Integer
|
|
24
|
-
connect_timeout: Integer
|
|
25
|
-
compression_type: String
|
|
18
|
+
client_id: String?,
|
|
19
|
+
client_secret: String?,
|
|
20
|
+
environment: String?,
|
|
21
|
+
api_server: String?,
|
|
22
|
+
query_server: String?,
|
|
23
|
+
query_timeout: Integer?,
|
|
24
|
+
api_timeout: Integer?,
|
|
25
|
+
connect_timeout: Integer?,
|
|
26
|
+
compression_type: String?,
|
|
27
|
+
additional_headers: Hash[String, String]?
|
|
26
28
|
}
|
|
27
29
|
|
|
28
30
|
def initialize: (options opts) -> void
|
data/sig/chalk_ruby/defaults.rbs
CHANGED
|
@@ -3,9 +3,8 @@ require 'rspec/autorun'
|
|
|
3
3
|
require 'chalk_ruby/client'
|
|
4
4
|
require 'chalk_ruby/error'
|
|
5
5
|
|
|
6
|
-
CLIENT_ID = '
|
|
7
|
-
CLIENT_SECRET = '
|
|
8
|
-
|
|
6
|
+
CLIENT_ID = ''
|
|
7
|
+
CLIENT_SECRET = ''
|
|
9
8
|
|
|
10
9
|
RSpec.describe 'Online query' do
|
|
11
10
|
it 'should accept valid queries' do
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: chalk_ruby
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Chalk AI, Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2024-01-
|
|
11
|
+
date: 2024-01-12 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: bundler
|
|
@@ -214,26 +214,27 @@ files:
|
|
|
214
214
|
- chalk_ruby.gemspec
|
|
215
215
|
- lib/chalk_ruby.rb
|
|
216
216
|
- lib/chalk_ruby/client.rb
|
|
217
|
-
- lib/chalk_ruby/config
|
|
217
|
+
- lib/chalk_ruby/config.rb
|
|
218
218
|
- lib/chalk_ruby/defaults.rb
|
|
219
219
|
- lib/chalk_ruby/error.rb
|
|
220
220
|
- lib/chalk_ruby/helpers.rb
|
|
221
221
|
- lib/chalk_ruby/http/http_requester.rb
|
|
222
|
+
- lib/chalk_ruby/http/http_requester_chalk.rb
|
|
222
223
|
- lib/chalk_ruby/http/response.rb
|
|
223
224
|
- lib/chalk_ruby/logger_helper.rb
|
|
224
|
-
- lib/chalk_ruby/
|
|
225
|
+
- lib/chalk_ruby/token.rb
|
|
225
226
|
- lib/chalk_ruby/version.rb
|
|
226
227
|
- renovate.json
|
|
227
228
|
- sig/chalk_ruby/client.rbs
|
|
228
|
-
- sig/chalk_ruby/config
|
|
229
|
+
- sig/chalk_ruby/config.rbs
|
|
229
230
|
- sig/chalk_ruby/defaults.rbs
|
|
230
231
|
- sig/chalk_ruby/error.rbs
|
|
231
232
|
- sig/chalk_ruby/helpers.rbs
|
|
233
|
+
- sig/chalk_ruby/http/_connection.rbs
|
|
232
234
|
- sig/chalk_ruby/http/http_requester.rbs
|
|
235
|
+
- sig/chalk_ruby/http/http_requester_chalk.rbs
|
|
233
236
|
- sig/chalk_ruby/http/response.rbs
|
|
234
|
-
- sig/chalk_ruby/interfaces/_connection.rbs
|
|
235
237
|
- sig/chalk_ruby/token.rbs
|
|
236
|
-
- sig/chalk_ruby/transport/transport.rbs
|
|
237
238
|
- sig/chalk_ruby/versions.rbs
|
|
238
239
|
- test/chalk_ai/integration/client_test.rb
|
|
239
240
|
- test/chalk_ai/test_helper.rb
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
require 'faraday'
|
|
2
|
-
require 'chalk_ruby/defaults'
|
|
3
|
-
|
|
4
|
-
module ChalkRuby
|
|
5
|
-
class Config
|
|
6
|
-
attr_accessor :client_id,
|
|
7
|
-
:client_secret,
|
|
8
|
-
:query_server,
|
|
9
|
-
:api_server,
|
|
10
|
-
:environment,
|
|
11
|
-
:query_timeout,
|
|
12
|
-
:api_timeout,
|
|
13
|
-
:connect_timeout
|
|
14
|
-
|
|
15
|
-
#
|
|
16
|
-
# @option options [String] :client_id
|
|
17
|
-
# @option options [String] :client_secret
|
|
18
|
-
# @option options [String] :query_host
|
|
19
|
-
# @option options [String] :api_host
|
|
20
|
-
# @option options [Integer] :read_timeout
|
|
21
|
-
# @option options [Integer] :write_timeout
|
|
22
|
-
# @option options [Integer] :connect_timeout
|
|
23
|
-
#
|
|
24
|
-
def initialize(opts = {})
|
|
25
|
-
@client_id = opts[:client_id] || ENV['CHALK_CLIENT_ID']
|
|
26
|
-
@client_secret = opts[:client_secret] || ENV['CHALK_CLIENT_SECRET']
|
|
27
|
-
@environment = opts[:environment] || ENV['CHALK_ACTIVE_ENVIRONMENT']
|
|
28
|
-
@query_server = opts[:query_server] || ENV['CHALK_QUERY_SERVER'] || Defaults::QUERY_SERVER
|
|
29
|
-
@api_server = opts[:api_server] || ENV['CHALK_API_SERVER'] || Defaults::API_SERVER
|
|
30
|
-
@query_timeout = opts[:query_timeout] || Defaults::API_TIMEOUT
|
|
31
|
-
@api_timeout = opts[:api_timeout] || Defaults::QUERY_TIMEOUT
|
|
32
|
-
@connect_timeout = opts[:connect_timeout] || Defaults::CONNECT_TIMEOUT
|
|
33
|
-
|
|
34
|
-
raise ChalkError, 'No Client ID provided, please set :client_id' if @client_id.nil?
|
|
35
|
-
raise ChalkError, 'No Client Secret provided, please set :client_secret' if @client_secret.nil?
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
end
|
|
39
|
-
end
|
|
File without changes
|