rager 0.3.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5e1955c552055b99969da67b3e85df748ee3077b5f02c8dc46fe418801acbc24
4
- data.tar.gz: 5bba65ff81cda2e4dc48711973107402379255e36d80e421d85951694df6edea
3
+ metadata.gz: 34c1c60d29be37b8a1f9ebb76d975257b26d89aa0345f3bdfb58290e61c82971
4
+ data.tar.gz: 47d3f41d9b7f9b08701c2b4b82e15cf7f86a7e3e4ffe9dced6e343ea28304597
5
5
  SHA512:
6
- metadata.gz: 671cc3bc70834f3c5b476dad2dc8b6cd0413f0ec2269c7cc7a75d01244f4f511f3dcf18b6b1a208653e0dd0ee67af6ce0082603173564a103722b284e2da5c43
7
- data.tar.gz: f945dd32c95854d2fe6febe850e619bc8a5c4a65b75c67a0e38e9a36219e48cf6b09a55265d9cdb499d2dbfabed8c2dfd356c836eaa88f65d7fe8747dc980bb2
6
+ metadata.gz: 6dfa77b1500bc0a2693141bff0c6551b72448d100f53f0e612b572d3b894ad5acc09f2cf3bb727d12141511bf902d28b3d43b2e359afc5d3fc9f172aae7caa06
7
+ data.tar.gz: d9522f23cd419a3b747f5e97e6097fc751008aedc3111954194de0253a24813a1919e0c5720cb653e51407e41173d80080461dadadb4590b716f638ea755444f
data/README.md CHANGED
@@ -18,7 +18,7 @@ bundle add rager
18
18
  Otherwise you can add it to your Gemfile directly:
19
19
 
20
20
  ```Ruby
21
- gem "rager", "~> 0.3.0"
21
+ gem "rager", "~> 0.4.0"
22
22
  ```
23
23
 
24
24
  ## License
data/lib/rager/config.rb CHANGED
@@ -7,9 +7,6 @@ module Rager
7
7
  class Config
8
8
  extend T::Sig
9
9
 
10
- sig { returns(Rager::Http::Adapters::Abstract) }
11
- attr_accessor :http_adapter
12
-
13
10
  sig { returns(T.nilable(Rager::Logger)) }
14
11
  attr_accessor :logger_type
15
12
 
@@ -24,11 +21,26 @@ module Rager
24
21
 
25
22
  sig { void }
26
23
  def initialize
27
- @http_adapter = T.let(Rager::Http::Adapters::AsyncHttp.new, Rager::Http::Adapters::Abstract)
24
+ @http_adapter = T.let(nil, T.nilable(Rager::Http::Adapters::Abstract))
28
25
  @logger_type = T.let(nil, T.nilable(Rager::Logger))
29
26
  @logger = T.let(::Logger.new($stdout), ::Logger)
30
27
  @url = T.let(nil, T.nilable(String))
31
28
  @api_key = T.let(nil, T.nilable(String))
32
29
  end
30
+
31
+ sig { returns(Rager::Http::Adapters::Abstract) }
32
+ def http_adapter
33
+ @http_adapter ||= default_http_adapter
34
+ end
35
+
36
+ sig { params(http_adapter: Rager::Http::Adapters::Abstract).void }
37
+ attr_writer :http_adapter
38
+
39
+ private
40
+
41
+ sig { returns(Rager::Http::Adapters::Abstract) }
42
+ def default_http_adapter
43
+ Rager::Http::Adapters::AsyncHttp.new
44
+ end
33
45
  end
34
46
  end
data/lib/rager/context.rb CHANGED
@@ -12,12 +12,12 @@ module Rager
12
12
  attr_reader :id
13
13
 
14
14
  sig { returns(T.nilable(String)) }
15
- attr_reader :hash
15
+ attr_reader :name
16
16
 
17
- sig { params(id: T.nilable(String)).void }
18
- def initialize(id: nil)
17
+ sig { params(id: T.nilable(String), name: T.nilable(String)).void }
18
+ def initialize(id: nil, name: nil)
19
19
  @id = T.let(id || SecureRandom.uuid, String)
20
- @hash = T.let(lookup_git_hash, T.nilable(String))
20
+ @name = T.let(name, T.nilable(String))
21
21
  end
22
22
 
23
23
  sig do
@@ -162,7 +162,7 @@ module Rager
162
162
  output = yield(options)
163
163
 
164
164
  Result.new(
165
- id: @id,
165
+ id: SecureRandom.uuid,
166
166
  context_id: @id,
167
167
  operation: operation,
168
168
  input: input,
@@ -171,12 +171,13 @@ module Rager
171
171
  start_time: start_time.to_i,
172
172
  end_time: Time.now.to_i,
173
173
  name: name,
174
+ context_name: @name,
174
175
  iids: iids,
175
176
  error: nil
176
177
  ).tap(&:log)
177
178
  rescue => e
178
179
  Result.new(
179
- id: @id,
180
+ id: SecureRandom.uuid,
180
181
  context_id: @id,
181
182
  operation: operation,
182
183
  input: input,
@@ -185,6 +186,7 @@ module Rager
185
186
  start_time: start_time.to_i,
186
187
  end_time: Time.now.to_i,
187
188
  name: name,
189
+ context_name: @name,
188
190
  iids: iids,
189
191
  error: e.message
190
192
  ).tap(&:log)
@@ -192,11 +194,5 @@ module Rager
192
194
  raise e
193
195
  end
194
196
  end
195
-
196
- sig { returns(T.nilable(String)) }
197
- def lookup_git_hash
198
- result = `git rev-parse HEAD`
199
- $?.success? ? result.strip : nil
200
- end
201
197
  end
202
198
  end
data/lib/rager/result.rb CHANGED
@@ -19,15 +19,15 @@ module Rager
19
19
  sig { returns(Rager::Operation) }
20
20
  attr_reader :operation
21
21
 
22
- sig { returns(Rager::Options) }
23
- attr_reader :options
24
-
25
22
  sig { returns(Rager::Types::Input) }
26
23
  attr_reader :input
27
24
 
28
25
  sig { returns(T.nilable(Rager::Types::Output)) }
29
26
  attr_reader :output
30
27
 
28
+ sig { returns(Rager::Options) }
29
+ attr_reader :options
30
+
31
31
  sig { returns(Integer) }
32
32
  attr_reader :start_time
33
33
 
@@ -37,6 +37,9 @@ module Rager
37
37
  sig { returns(T.nilable(String)) }
38
38
  attr_reader :name
39
39
 
40
+ sig { returns(T.nilable(String)) }
41
+ attr_reader :context_name
42
+
40
43
  sig { returns(T.nilable(T::Array[String])) }
41
44
  attr_reader :iids
42
45
 
@@ -54,6 +57,7 @@ module Rager
54
57
  start_time: Integer,
55
58
  end_time: Integer,
56
59
  name: T.nilable(String),
60
+ context_name: T.nilable(String),
57
61
  iids: T.nilable(T::Array[String]),
58
62
  error: T.nilable(String)
59
63
  ).void
@@ -68,6 +72,7 @@ module Rager
68
72
  start_time:,
69
73
  end_time:,
70
74
  name: nil,
75
+ context_name: nil,
71
76
  iids: nil,
72
77
  error: nil
73
78
  )
@@ -80,6 +85,7 @@ module Rager
80
85
  @start_time = start_time
81
86
  @end_time = end_time
82
87
  @name = T.let(name, T.nilable(String))
88
+ @context_name = T.let(context_name, T.nilable(String))
83
89
  @iids = T.let(iids, T.nilable(T::Array[String]))
84
90
  @error = T.let(error, T.nilable(String))
85
91
 
@@ -177,17 +183,21 @@ module Rager
177
183
  sig { returns(T::Hash[String, T.untyped]) }
178
184
  def to_h
179
185
  {
180
- id: @id,
181
- context_id: @context_id,
182
- operation: @operation.serialize,
183
- input: serialize_input,
184
- output: serialize_output,
185
- options: @options.serialize_safe,
186
- start_time: @start_time,
187
- end_time: @end_time,
188
- name: @name,
189
- iids: @iids,
190
- error: @error
186
+ version: "1",
187
+ data: {
188
+ id: @id,
189
+ context_id: @context_id,
190
+ operation: @operation.serialize,
191
+ input: serialize_input,
192
+ output: serialize_output,
193
+ options: @options.serialize_safe,
194
+ start_time: @start_time,
195
+ end_time: @end_time,
196
+ name: @name,
197
+ context_name: @context_name,
198
+ iids: @iids,
199
+ error: @error
200
+ }
191
201
  }
192
202
  end
193
203
 
@@ -9,6 +9,21 @@ module Rager
9
9
  module Http
10
10
  extend T::Sig
11
11
 
12
+ sig { params(url: String, headers: T.nilable(T::Hash[String, String])).returns(T.nilable(String)) }
13
+ def self.download_binary(url, headers = nil)
14
+ response = Rager.config.http_adapter.make_request(
15
+ Rager::Http::Request.new(url: url, headers: headers || {})
16
+ )
17
+
18
+ if response.success? && response.body
19
+ binary_data = T.cast(response.body, String)
20
+ binary_data.force_encoding(Encoding::BINARY)
21
+ binary_data
22
+ end
23
+ rescue
24
+ nil
25
+ end
26
+
12
27
  sig { params(url: String, path: T.nilable(String), headers: T.nilable(T::Hash[String, String])).returns(String) }
13
28
  def self.download_file(url, path = nil, headers = nil)
14
29
  path ||= begin
@@ -21,16 +36,11 @@ module Rager
21
36
  end
22
37
  end
23
38
 
24
- response = Rager.config.http_adapter.make_request(
25
- Rager::Http::Request.new(url: url, headers: headers || {})
26
- )
27
-
28
- raise Rager::Errors::HttpError.new(Rager.config.http_adapter, response.status, T.cast(response.body, T.nilable(String))) unless response.success?
39
+ binary_data = download_binary(url, headers)
40
+ raise Rager::Errors::HttpError.new(Rager.config.http_adapter, 500, "Download failed") if binary_data.nil?
29
41
 
30
- File.binwrite(path, T.cast(T.must(response.body), String))
42
+ File.binwrite(path, binary_data)
31
43
  path
32
- rescue Rager::Errors::HttpError
33
- raise
34
44
  rescue => e
35
45
  raise Rager::Errors::HttpError.new(Rager.config.http_adapter, 500, "Download failed: #{e.message}")
36
46
  end
@@ -9,26 +9,47 @@ module Rager
9
9
  module Replicate
10
10
  extend T::Sig
11
11
 
12
- sig { params(prediction_url: String, key: T.nilable(String), path: T.nilable(String)).returns(T.nilable(String)) }
13
- def self.download_prediction(prediction_url, key = nil, path = nil)
12
+ sig { params(prediction_url: String, key: T.nilable(String), http_adapter: T.nilable(Rager::Http::Adapters::Abstract)).returns(T.nilable(String)) }
13
+ def self.download_prediction(prediction_url, key = nil, http_adapter: nil)
14
+ download_url = get_download_url(prediction_url, key, http_adapter: http_adapter)
15
+ return nil if download_url.nil?
16
+
17
+ Rager::Utils::Http.download_binary(download_url)
18
+ rescue
19
+ nil
20
+ end
21
+
22
+ sig { params(prediction_url: String, key: T.nilable(String), path: String, http_adapter: T.nilable(Rager::Http::Adapters::Abstract)).returns(T.nilable(String)) }
23
+ def self.download_prediction_to_file(prediction_url, key = nil, path:, http_adapter: nil)
24
+ download_url = get_download_url(prediction_url, key, http_adapter: http_adapter)
25
+ return nil if download_url.nil?
26
+
27
+ Rager::Utils::Http.download_file(download_url, path)
28
+ rescue
29
+ nil
30
+ end
31
+
32
+ sig { params(prediction_url: String, key: T.nilable(String), http_adapter: T.nilable(Rager::Http::Adapters::Abstract)).returns(T.nilable(String)) }
33
+ def self.get_download_url(prediction_url, key = nil, http_adapter: nil)
14
34
  api_key = ENV["REPLICATE_API_KEY"]
15
35
  raise Rager::Errors::MissingCredentialsError.new("Replicate", "REPLICATE_API_KEY") if api_key.nil?
16
36
 
17
- response = Rager.config.http_adapter.make_request(
37
+ adapter = http_adapter || Rager.config.http_adapter
38
+ response = adapter.make_request(
18
39
  Rager::Http::Request.new(
19
40
  url: prediction_url,
20
41
  headers: {"Authorization" => "Bearer #{api_key}", "Content-Type" => "application/json"}
21
42
  )
22
43
  )
23
44
 
24
- raise Rager::Errors::HttpError.new(Rager.config.http_adapter, response.status, T.cast(response.body, T.nilable(String))) unless response.success?
45
+ raise Rager::Errors::HttpError.new(adapter, response.status, T.cast(response.body, T.nilable(String))) unless response.success?
25
46
 
26
47
  data = JSON.parse(T.cast(T.must(response.body), String))
27
48
  return nil if ["starting", "processing"].include?(data["status"])
28
49
 
29
50
  if data["status"] == "failed"
30
51
  error_msg = data["error"] || "Prediction failed"
31
- raise Rager::Errors::HttpError.new(Rager.config.http_adapter, 422, "Prediction failed: #{error_msg}")
52
+ raise Rager::Errors::HttpError.new(adapter, 422, "Prediction failed: #{error_msg}")
32
53
  end
33
54
 
34
55
  return nil unless data["status"] == "succeeded"
@@ -47,8 +68,7 @@ module Rager
47
68
  end
48
69
 
49
70
  return nil if download_url.nil? || download_url.empty?
50
-
51
- Rager::Utils::Http.download_file(download_url, path)
71
+ download_url
52
72
  rescue JSON::ParserError => e
53
73
  raise Rager::Errors::ParseError.new("Failed to parse prediction response", e.message)
54
74
  rescue Rager::Errors::HttpError, Rager::Errors::ParseError, Rager::Errors::MissingCredentialsError
@@ -57,15 +77,20 @@ module Rager
57
77
  nil
58
78
  end
59
79
 
60
- sig { params(prediction_url: String, key: T.nilable(String), path: T.nilable(String), max_attempts: Integer, sleep_interval: Integer).returns(T.nilable(String)) }
61
- def self.poll_prediction(prediction_url, key: nil, path: nil, max_attempts: 30, sleep_interval: 10)
80
+ sig { params(prediction_url: String, key: T.nilable(String), path: T.nilable(String), max_attempts: Integer, sleep_interval: Integer, http_adapter: T.nilable(Rager::Http::Adapters::Abstract)).returns(T.nilable(String)) }
81
+ def self.poll_prediction(prediction_url, key: nil, path: nil, max_attempts: 30, sleep_interval: 10, http_adapter: nil)
62
82
  max_attempts.times do
63
- result = download_prediction(prediction_url, key, path)
83
+ result = if path
84
+ download_prediction_to_file(prediction_url, key, path: path, http_adapter: http_adapter)
85
+ else
86
+ download_prediction(prediction_url, key, http_adapter: http_adapter)
87
+ end
64
88
  return result unless result.nil?
65
89
  sleep(sleep_interval)
66
90
  end
67
91
 
68
- raise Rager::Errors::HttpError.new(Rager.config.http_adapter, 408, "Prediction polling timed out after #{max_attempts} attempts")
92
+ adapter = http_adapter || Rager.config.http_adapter
93
+ raise Rager::Errors::HttpError.new(adapter, 408, "Prediction polling timed out after #{max_attempts} attempts")
69
94
  end
70
95
  end
71
96
  end
data/lib/rager/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Rager
5
- VERSION = "0.3.0"
5
+ VERSION = "0.5.0"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mvkvc
@@ -148,7 +148,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - ">="
150
150
  - !ruby/object:Gem::Version
151
- version: '3.1'
151
+ version: '3.2'
152
152
  required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  requirements:
154
154
  - - ">="