openlayer 0.0.2 → 0.1.1
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/.env +0 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/LICENSE +21 -0
- data/README.md +19 -294
- data/Rakefile +12 -0
- data/lib/openlayer/client.rb +21 -71
- data/lib/openlayer/error.rb +5 -0
- data/lib/openlayer/inference_pipeline.rb +36 -0
- data/lib/openlayer/version.rb +1 -1
- data/lib/openlayer.rb +7 -96
- data/openlayer.gemspec +42 -0
- data/sig/openlayer.rbs +4 -0
- metadata +44 -227
- data/.ignore +0 -2
- data/CHANGELOG.md +0 -15
- data/SECURITY.md +0 -27
- data/lib/openlayer/errors.rb +0 -228
- data/lib/openlayer/file_part.rb +0 -58
- data/lib/openlayer/internal/transport/base_client.rb +0 -567
- data/lib/openlayer/internal/transport/pooled_net_requester.rb +0 -198
- data/lib/openlayer/internal/type/array_of.rb +0 -168
- data/lib/openlayer/internal/type/base_model.rb +0 -531
- data/lib/openlayer/internal/type/base_page.rb +0 -55
- data/lib/openlayer/internal/type/boolean.rb +0 -77
- data/lib/openlayer/internal/type/converter.rb +0 -327
- data/lib/openlayer/internal/type/enum.rb +0 -131
- data/lib/openlayer/internal/type/file_input.rb +0 -111
- data/lib/openlayer/internal/type/hash_of.rb +0 -188
- data/lib/openlayer/internal/type/request_parameters.rb +0 -42
- data/lib/openlayer/internal/type/union.rb +0 -237
- data/lib/openlayer/internal/type/unknown.rb +0 -81
- data/lib/openlayer/internal/util.rb +0 -915
- data/lib/openlayer/internal.rb +0 -20
- data/lib/openlayer/models/commit_retrieve_params.rb +0 -14
- data/lib/openlayer/models/commit_retrieve_response.rb +0 -275
- data/lib/openlayer/models/commits/test_result_list_params.rb +0 -91
- data/lib/openlayer/models/commits/test_result_list_response.rb +0 -566
- data/lib/openlayer/models/inference_pipeline_delete_params.rb +0 -14
- data/lib/openlayer/models/inference_pipeline_retrieve_params.rb +0 -33
- data/lib/openlayer/models/inference_pipeline_retrieve_response.rb +0 -605
- data/lib/openlayer/models/inference_pipeline_update_params.rb +0 -42
- data/lib/openlayer/models/inference_pipeline_update_response.rb +0 -605
- data/lib/openlayer/models/inference_pipelines/data_stream_params.rb +0 -463
- data/lib/openlayer/models/inference_pipelines/data_stream_response.rb +0 -28
- data/lib/openlayer/models/inference_pipelines/row_update_params.rb +0 -90
- data/lib/openlayer/models/inference_pipelines/row_update_response.rb +0 -28
- data/lib/openlayer/models/inference_pipelines/test_result_list_params.rb +0 -83
- data/lib/openlayer/models/inference_pipelines/test_result_list_response.rb +0 -568
- data/lib/openlayer/models/project_create_params.rb +0 -51
- data/lib/openlayer/models/project_create_response.rb +0 -262
- data/lib/openlayer/models/project_list_params.rb +0 -59
- data/lib/openlayer/models/project_list_response.rb +0 -274
- data/lib/openlayer/models/projects/commit_create_params.rb +0 -61
- data/lib/openlayer/models/projects/commit_create_response.rb +0 -277
- data/lib/openlayer/models/projects/commit_list_params.rb +0 -32
- data/lib/openlayer/models/projects/commit_list_response.rb +0 -288
- data/lib/openlayer/models/projects/inference_pipeline_create_params.rb +0 -229
- data/lib/openlayer/models/projects/inference_pipeline_create_response.rb +0 -612
- data/lib/openlayer/models/projects/inference_pipeline_list_params.rb +0 -40
- data/lib/openlayer/models/projects/inference_pipeline_list_response.rb +0 -623
- data/lib/openlayer/models/projects/test_create_params.rb +0 -367
- data/lib/openlayer/models/projects/test_create_response.rb +0 -438
- data/lib/openlayer/models/projects/test_list_params.rb +0 -91
- data/lib/openlayer/models/projects/test_list_response.rb +0 -452
- data/lib/openlayer/models/projects/test_update_params.rb +0 -262
- data/lib/openlayer/models/projects/test_update_response.rb +0 -24
- data/lib/openlayer/models/storage/presigned_url_create_params.rb +0 -24
- data/lib/openlayer/models/storage/presigned_url_create_response.rb +0 -35
- data/lib/openlayer/models.rb +0 -61
- data/lib/openlayer/request_options.rb +0 -77
- data/lib/openlayer/resources/commits/test_results.rb +0 -51
- data/lib/openlayer/resources/commits.rb +0 -38
- data/lib/openlayer/resources/inference_pipelines/data.rb +0 -46
- data/lib/openlayer/resources/inference_pipelines/rows.rb +0 -46
- data/lib/openlayer/resources/inference_pipelines/test_results.rb +0 -49
- data/lib/openlayer/resources/inference_pipelines.rb +0 -101
- data/lib/openlayer/resources/projects/commits.rb +0 -72
- data/lib/openlayer/resources/projects/inference_pipelines.rb +0 -74
- data/lib/openlayer/resources/projects/tests.rb +0 -135
- data/lib/openlayer/resources/projects.rb +0 -80
- data/lib/openlayer/resources/storage/presigned_url.rb +0 -38
- data/lib/openlayer/resources/storage.rb +0 -18
- data/manifest.yaml +0 -15
- data/rbi/openlayer/client.rbi +0 -58
- data/rbi/openlayer/errors.rbi +0 -205
- data/rbi/openlayer/file_part.rbi +0 -37
- data/rbi/openlayer/internal/transport/base_client.rbi +0 -293
- data/rbi/openlayer/internal/transport/pooled_net_requester.rbi +0 -79
- data/rbi/openlayer/internal/type/array_of.rbi +0 -104
- data/rbi/openlayer/internal/type/base_model.rbi +0 -304
- data/rbi/openlayer/internal/type/base_page.rbi +0 -42
- data/rbi/openlayer/internal/type/boolean.rbi +0 -58
- data/rbi/openlayer/internal/type/converter.rbi +0 -216
- data/rbi/openlayer/internal/type/enum.rbi +0 -82
- data/rbi/openlayer/internal/type/file_input.rbi +0 -59
- data/rbi/openlayer/internal/type/hash_of.rbi +0 -104
- data/rbi/openlayer/internal/type/request_parameters.rbi +0 -29
- data/rbi/openlayer/internal/type/union.rbi +0 -128
- data/rbi/openlayer/internal/type/unknown.rbi +0 -58
- data/rbi/openlayer/internal/util.rbi +0 -487
- data/rbi/openlayer/internal.rbi +0 -18
- data/rbi/openlayer/models/commit_retrieve_params.rbi +0 -27
- data/rbi/openlayer/models/commit_retrieve_response.rbi +0 -393
- data/rbi/openlayer/models/commits/test_result_list_params.rbi +0 -214
- data/rbi/openlayer/models/commits/test_result_list_response.rbi +0 -1256
- data/rbi/openlayer/models/inference_pipeline_delete_params.rbi +0 -30
- data/rbi/openlayer/models/inference_pipeline_retrieve_params.rbi +0 -101
- data/rbi/openlayer/models/inference_pipeline_retrieve_response.rbi +0 -1022
- data/rbi/openlayer/models/inference_pipeline_update_params.rbi +0 -67
- data/rbi/openlayer/models/inference_pipeline_update_response.rbi +0 -1018
- data/rbi/openlayer/models/inference_pipelines/data_stream_params.rbi +0 -750
- data/rbi/openlayer/models/inference_pipelines/data_stream_response.rbi +0 -73
- data/rbi/openlayer/models/inference_pipelines/row_update_params.rbi +0 -153
- data/rbi/openlayer/models/inference_pipelines/row_update_response.rbi +0 -73
- data/rbi/openlayer/models/inference_pipelines/test_result_list_params.rbi +0 -216
- data/rbi/openlayer/models/inference_pipelines/test_result_list_response.rbi +0 -1258
- data/rbi/openlayer/models/project_create_params.rbi +0 -99
- data/rbi/openlayer/models/project_create_response.rbi +0 -401
- data/rbi/openlayer/models/project_list_params.rbi +0 -121
- data/rbi/openlayer/models/project_list_response.rbi +0 -442
- data/rbi/openlayer/models/projects/commit_create_params.rbi +0 -125
- data/rbi/openlayer/models/projects/commit_create_response.rbi +0 -409
- data/rbi/openlayer/models/projects/commit_list_params.rbi +0 -62
- data/rbi/openlayer/models/projects/commit_list_response.rbi +0 -454
- data/rbi/openlayer/models/projects/inference_pipeline_create_params.rbi +0 -560
- data/rbi/openlayer/models/projects/inference_pipeline_create_response.rbi +0 -1027
- data/rbi/openlayer/models/projects/inference_pipeline_list_params.rbi +0 -73
- data/rbi/openlayer/models/projects/inference_pipeline_list_response.rbi +0 -1070
- data/rbi/openlayer/models/projects/test_create_params.rbi +0 -940
- data/rbi/openlayer/models/projects/test_create_response.rbi +0 -1026
- data/rbi/openlayer/models/projects/test_list_params.rbi +0 -165
- data/rbi/openlayer/models/projects/test_list_response.rbi +0 -1067
- data/rbi/openlayer/models/projects/test_update_params.rbi +0 -699
- data/rbi/openlayer/models/projects/test_update_response.rbi +0 -43
- data/rbi/openlayer/models/storage/presigned_url_create_params.rbi +0 -45
- data/rbi/openlayer/models/storage/presigned_url_create_response.rbi +0 -55
- data/rbi/openlayer/models.rbi +0 -26
- data/rbi/openlayer/request_options.rbi +0 -59
- data/rbi/openlayer/resources/commits/test_results.rbi +0 -45
- data/rbi/openlayer/resources/commits.rbi +0 -29
- data/rbi/openlayer/resources/inference_pipelines/data.rbi +0 -41
- data/rbi/openlayer/resources/inference_pipelines/rows.rbi +0 -40
- data/rbi/openlayer/resources/inference_pipelines/test_results.rbi +0 -46
- data/rbi/openlayer/resources/inference_pipelines.rbi +0 -79
- data/rbi/openlayer/resources/projects/commits.rbi +0 -60
- data/rbi/openlayer/resources/projects/inference_pipelines.rbi +0 -69
- data/rbi/openlayer/resources/projects/tests.rbi +0 -123
- data/rbi/openlayer/resources/projects.rbi +0 -64
- data/rbi/openlayer/resources/storage/presigned_url.rbi +0 -28
- data/rbi/openlayer/resources/storage.rbi +0 -15
- data/rbi/openlayer/version.rbi +0 -5
- data/sig/openlayer/client.rbs +0 -32
- data/sig/openlayer/errors.rbs +0 -117
- data/sig/openlayer/file_part.rbs +0 -21
- data/sig/openlayer/internal/transport/base_client.rbs +0 -131
- data/sig/openlayer/internal/transport/pooled_net_requester.rbs +0 -45
- data/sig/openlayer/internal/type/array_of.rbs +0 -48
- data/sig/openlayer/internal/type/base_model.rbs +0 -102
- data/sig/openlayer/internal/type/base_page.rbs +0 -24
- data/sig/openlayer/internal/type/boolean.rbs +0 -26
- data/sig/openlayer/internal/type/converter.rbs +0 -79
- data/sig/openlayer/internal/type/enum.rbs +0 -32
- data/sig/openlayer/internal/type/file_input.rbs +0 -25
- data/sig/openlayer/internal/type/hash_of.rbs +0 -48
- data/sig/openlayer/internal/type/request_parameters.rbs +0 -19
- data/sig/openlayer/internal/type/union.rbs +0 -52
- data/sig/openlayer/internal/type/unknown.rbs +0 -26
- data/sig/openlayer/internal/util.rbs +0 -185
- data/sig/openlayer/internal.rbs +0 -9
- data/sig/openlayer/models/commit_retrieve_params.rbs +0 -15
- data/sig/openlayer/models/commit_retrieve_response.rbs +0 -209
- data/sig/openlayer/models/commits/test_result_list_params.rbs +0 -91
- data/sig/openlayer/models/commits/test_result_list_response.rbs +0 -529
- data/sig/openlayer/models/inference_pipeline_delete_params.rbs +0 -15
- data/sig/openlayer/models/inference_pipeline_retrieve_params.rbs +0 -41
- data/sig/openlayer/models/inference_pipeline_retrieve_response.rbs +0 -493
- data/sig/openlayer/models/inference_pipeline_update_params.rbs +0 -34
- data/sig/openlayer/models/inference_pipeline_update_response.rbs +0 -493
- data/sig/openlayer/models/inference_pipelines/data_stream_params.rbs +0 -386
- data/sig/openlayer/models/inference_pipelines/data_stream_response.rbs +0 -32
- data/sig/openlayer/models/inference_pipelines/row_update_params.rbs +0 -75
- data/sig/openlayer/models/inference_pipelines/row_update_response.rbs +0 -32
- data/sig/openlayer/models/inference_pipelines/test_result_list_params.rbs +0 -84
- data/sig/openlayer/models/inference_pipelines/test_result_list_response.rbs +0 -529
- data/sig/openlayer/models/project_create_params.rbs +0 -53
- data/sig/openlayer/models/project_create_response.rbs +0 -210
- data/sig/openlayer/models/project_list_params.rbs +0 -68
- data/sig/openlayer/models/project_list_response.rbs +0 -225
- data/sig/openlayer/models/projects/commit_create_params.rbs +0 -82
- data/sig/openlayer/models/projects/commit_create_response.rbs +0 -211
- data/sig/openlayer/models/projects/commit_list_params.rbs +0 -34
- data/sig/openlayer/models/projects/commit_list_response.rbs +0 -228
- data/sig/openlayer/models/projects/inference_pipeline_create_params.rbs +0 -308
- data/sig/openlayer/models/projects/inference_pipeline_create_response.rbs +0 -495
- data/sig/openlayer/models/projects/inference_pipeline_list_params.rbs +0 -40
- data/sig/openlayer/models/projects/inference_pipeline_list_response.rbs +0 -512
- data/sig/openlayer/models/projects/test_create_params.rbs +0 -388
- data/sig/openlayer/models/projects/test_create_response.rbs +0 -427
- data/sig/openlayer/models/projects/test_list_params.rbs +0 -85
- data/sig/openlayer/models/projects/test_list_response.rbs +0 -442
- data/sig/openlayer/models/projects/test_update_params.rbs +0 -275
- data/sig/openlayer/models/projects/test_update_response.rbs +0 -25
- data/sig/openlayer/models/storage/presigned_url_create_params.rbs +0 -25
- data/sig/openlayer/models/storage/presigned_url_create_response.rbs +0 -22
- data/sig/openlayer/models.rbs +0 -21
- data/sig/openlayer/request_options.rbs +0 -34
- data/sig/openlayer/resources/commits/test_results.rbs +0 -19
- data/sig/openlayer/resources/commits.rbs +0 -14
- data/sig/openlayer/resources/inference_pipelines/data.rbs +0 -16
- data/sig/openlayer/resources/inference_pipelines/rows.rbs +0 -17
- data/sig/openlayer/resources/inference_pipelines/test_results.rbs +0 -18
- data/sig/openlayer/resources/inference_pipelines.rbs +0 -32
- data/sig/openlayer/resources/projects/commits.rbs +0 -25
- data/sig/openlayer/resources/projects/inference_pipelines.rbs +0 -26
- data/sig/openlayer/resources/projects/tests.rbs +0 -45
- data/sig/openlayer/resources/projects.rbs +0 -28
- data/sig/openlayer/resources/storage/presigned_url.rbs +0 -14
- data/sig/openlayer/resources/storage.rbs +0 -9
- data/sig/openlayer/version.rbs +0 -3
data/lib/openlayer/file_part.rb
DELETED
@@ -1,58 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Openlayer
|
4
|
-
class FilePart
|
5
|
-
# @return [Pathname, StringIO, IO, String]
|
6
|
-
attr_reader :content
|
7
|
-
|
8
|
-
# @return [String, nil]
|
9
|
-
attr_reader :content_type
|
10
|
-
|
11
|
-
# @return [String, nil]
|
12
|
-
attr_reader :filename
|
13
|
-
|
14
|
-
# @api private
|
15
|
-
#
|
16
|
-
# @return [String]
|
17
|
-
private def read
|
18
|
-
case content
|
19
|
-
in Pathname
|
20
|
-
content.read(binmode: true)
|
21
|
-
in StringIO
|
22
|
-
content.string
|
23
|
-
in IO
|
24
|
-
content.read
|
25
|
-
in String
|
26
|
-
content
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
# @param a [Object]
|
31
|
-
#
|
32
|
-
# @return [String]
|
33
|
-
def to_json(*a) = read.to_json(*a)
|
34
|
-
|
35
|
-
# @param a [Object]
|
36
|
-
#
|
37
|
-
# @return [String]
|
38
|
-
def to_yaml(*a) = read.to_yaml(*a)
|
39
|
-
|
40
|
-
# @param content [Pathname, StringIO, IO, String]
|
41
|
-
# @param filename [Pathname, String, nil]
|
42
|
-
# @param content_type [String, nil]
|
43
|
-
def initialize(content, filename: nil, content_type: nil)
|
44
|
-
@content_type = content_type
|
45
|
-
@filename =
|
46
|
-
case [filename, (@content = content)]
|
47
|
-
in [String | Pathname, _]
|
48
|
-
::File.basename(filename)
|
49
|
-
in [nil, Pathname]
|
50
|
-
content.basename.to_path
|
51
|
-
in [nil, IO]
|
52
|
-
content.to_path
|
53
|
-
else
|
54
|
-
filename
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
58
|
-
end
|
@@ -1,567 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Openlayer
|
4
|
-
module Internal
|
5
|
-
module Transport
|
6
|
-
# @api private
|
7
|
-
#
|
8
|
-
# @abstract
|
9
|
-
class BaseClient
|
10
|
-
extend Openlayer::Internal::Util::SorbetRuntimeSupport
|
11
|
-
|
12
|
-
# from whatwg fetch spec
|
13
|
-
MAX_REDIRECTS = 20
|
14
|
-
|
15
|
-
# rubocop:disable Style/MutableConstant
|
16
|
-
PLATFORM_HEADERS =
|
17
|
-
{
|
18
|
-
"x-stainless-arch" => Openlayer::Internal::Util.arch,
|
19
|
-
"x-stainless-lang" => "ruby",
|
20
|
-
"x-stainless-os" => Openlayer::Internal::Util.os,
|
21
|
-
"x-stainless-package-version" => Openlayer::VERSION,
|
22
|
-
"x-stainless-runtime" => ::RUBY_ENGINE,
|
23
|
-
"x-stainless-runtime-version" => ::RUBY_ENGINE_VERSION
|
24
|
-
}
|
25
|
-
# rubocop:enable Style/MutableConstant
|
26
|
-
|
27
|
-
class << self
|
28
|
-
# @api private
|
29
|
-
#
|
30
|
-
# @param req [Hash{Symbol=>Object}]
|
31
|
-
#
|
32
|
-
# @raise [ArgumentError]
|
33
|
-
def validate!(req)
|
34
|
-
keys = [:method, :path, :query, :headers, :body, :unwrap, :page, :stream, :model, :options]
|
35
|
-
case req
|
36
|
-
in Hash
|
37
|
-
req.each_key do |k|
|
38
|
-
unless keys.include?(k)
|
39
|
-
raise ArgumentError.new("Request `req` keys must be one of #{keys}, got #{k.inspect}")
|
40
|
-
end
|
41
|
-
end
|
42
|
-
else
|
43
|
-
raise ArgumentError.new("Request `req` must be a Hash or RequestOptions, got #{req.inspect}")
|
44
|
-
end
|
45
|
-
end
|
46
|
-
|
47
|
-
# @api private
|
48
|
-
#
|
49
|
-
# @param status [Integer]
|
50
|
-
# @param headers [Hash{String=>String}]
|
51
|
-
#
|
52
|
-
# @return [Boolean]
|
53
|
-
def should_retry?(status, headers:)
|
54
|
-
coerced = Openlayer::Internal::Util.coerce_boolean(headers["x-should-retry"])
|
55
|
-
case [coerced, status]
|
56
|
-
in [true | false, _]
|
57
|
-
coerced
|
58
|
-
in [_, 408 | 409 | 429 | (500..)]
|
59
|
-
# retry on:
|
60
|
-
# 408: timeouts
|
61
|
-
# 409: locks
|
62
|
-
# 429: rate limits
|
63
|
-
# 500+: unknown errors
|
64
|
-
true
|
65
|
-
else
|
66
|
-
false
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
# @api private
|
71
|
-
#
|
72
|
-
# @param request [Hash{Symbol=>Object}] .
|
73
|
-
#
|
74
|
-
# @option request [Symbol] :method
|
75
|
-
#
|
76
|
-
# @option request [URI::Generic] :url
|
77
|
-
#
|
78
|
-
# @option request [Hash{String=>String}] :headers
|
79
|
-
#
|
80
|
-
# @option request [Object] :body
|
81
|
-
#
|
82
|
-
# @option request [Integer] :max_retries
|
83
|
-
#
|
84
|
-
# @option request [Float] :timeout
|
85
|
-
#
|
86
|
-
# @param status [Integer]
|
87
|
-
#
|
88
|
-
# @param response_headers [Hash{String=>String}]
|
89
|
-
#
|
90
|
-
# @return [Hash{Symbol=>Object}]
|
91
|
-
def follow_redirect(request, status:, response_headers:)
|
92
|
-
method, url, headers = request.fetch_values(:method, :url, :headers)
|
93
|
-
location =
|
94
|
-
Kernel.then do
|
95
|
-
URI.join(url, response_headers["location"])
|
96
|
-
rescue ArgumentError
|
97
|
-
message = "Server responded with status #{status} but no valid location header."
|
98
|
-
raise Openlayer::Errors::APIConnectionError.new(
|
99
|
-
url: url,
|
100
|
-
response: response_headers,
|
101
|
-
message: message
|
102
|
-
)
|
103
|
-
end
|
104
|
-
|
105
|
-
request = {**request, url: location}
|
106
|
-
|
107
|
-
case [url.scheme, location.scheme]
|
108
|
-
in ["https", "http"]
|
109
|
-
message = "Tried to redirect to a insecure URL"
|
110
|
-
raise Openlayer::Errors::APIConnectionError.new(
|
111
|
-
url: url,
|
112
|
-
response: response_headers,
|
113
|
-
message: message
|
114
|
-
)
|
115
|
-
else
|
116
|
-
nil
|
117
|
-
end
|
118
|
-
|
119
|
-
# from whatwg fetch spec
|
120
|
-
case [status, method]
|
121
|
-
in [301 | 302, :post] | [303, _]
|
122
|
-
drop = %w[content-encoding content-language content-length content-location content-type]
|
123
|
-
request = {
|
124
|
-
**request,
|
125
|
-
method: method == :head ? :head : :get,
|
126
|
-
headers: headers.except(*drop),
|
127
|
-
body: nil
|
128
|
-
}
|
129
|
-
else
|
130
|
-
end
|
131
|
-
|
132
|
-
# from undici
|
133
|
-
if Openlayer::Internal::Util.uri_origin(url) != Openlayer::Internal::Util.uri_origin(location)
|
134
|
-
drop = %w[authorization cookie host proxy-authorization]
|
135
|
-
request = {**request, headers: request.fetch(:headers).except(*drop)}
|
136
|
-
end
|
137
|
-
|
138
|
-
request
|
139
|
-
end
|
140
|
-
|
141
|
-
# @api private
|
142
|
-
#
|
143
|
-
# @param status [Integer, Openlayer::Errors::APIConnectionError]
|
144
|
-
# @param stream [Enumerable<String>, nil]
|
145
|
-
def reap_connection!(status, stream:)
|
146
|
-
case status
|
147
|
-
in (..199) | (300..499)
|
148
|
-
stream&.each { next }
|
149
|
-
in Openlayer::Errors::APIConnectionError | (500..)
|
150
|
-
Openlayer::Internal::Util.close_fused!(stream)
|
151
|
-
else
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
# @return [URI::Generic]
|
157
|
-
attr_reader :base_url
|
158
|
-
|
159
|
-
# @return [Float]
|
160
|
-
attr_reader :timeout
|
161
|
-
|
162
|
-
# @return [Integer]
|
163
|
-
attr_reader :max_retries
|
164
|
-
|
165
|
-
# @return [Float]
|
166
|
-
attr_reader :initial_retry_delay
|
167
|
-
|
168
|
-
# @return [Float]
|
169
|
-
attr_reader :max_retry_delay
|
170
|
-
|
171
|
-
# @return [Hash{String=>String}]
|
172
|
-
attr_reader :headers
|
173
|
-
|
174
|
-
# @return [String, nil]
|
175
|
-
attr_reader :idempotency_header
|
176
|
-
|
177
|
-
# @api private
|
178
|
-
# @return [Openlayer::Internal::Transport::PooledNetRequester]
|
179
|
-
attr_reader :requester
|
180
|
-
|
181
|
-
# @api private
|
182
|
-
#
|
183
|
-
# @param base_url [String]
|
184
|
-
# @param timeout [Float]
|
185
|
-
# @param max_retries [Integer]
|
186
|
-
# @param initial_retry_delay [Float]
|
187
|
-
# @param max_retry_delay [Float]
|
188
|
-
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}]
|
189
|
-
# @param idempotency_header [String, nil]
|
190
|
-
def initialize(
|
191
|
-
base_url:,
|
192
|
-
timeout: 0.0,
|
193
|
-
max_retries: 0,
|
194
|
-
initial_retry_delay: 0.0,
|
195
|
-
max_retry_delay: 0.0,
|
196
|
-
headers: {},
|
197
|
-
idempotency_header: nil
|
198
|
-
)
|
199
|
-
@requester = Openlayer::Internal::Transport::PooledNetRequester.new
|
200
|
-
@headers = Openlayer::Internal::Util.normalized_headers(
|
201
|
-
self.class::PLATFORM_HEADERS,
|
202
|
-
{
|
203
|
-
"accept" => "application/json",
|
204
|
-
"content-type" => "application/json"
|
205
|
-
},
|
206
|
-
headers
|
207
|
-
)
|
208
|
-
@base_url_components = Openlayer::Internal::Util.parse_uri(base_url)
|
209
|
-
@base_url = Openlayer::Internal::Util.unparse_uri(@base_url_components)
|
210
|
-
@idempotency_header = idempotency_header&.to_s&.downcase
|
211
|
-
@timeout = timeout
|
212
|
-
@max_retries = max_retries
|
213
|
-
@initial_retry_delay = initial_retry_delay
|
214
|
-
@max_retry_delay = max_retry_delay
|
215
|
-
end
|
216
|
-
|
217
|
-
# @api private
|
218
|
-
#
|
219
|
-
# @return [Hash{String=>String}]
|
220
|
-
private def auth_headers = {}
|
221
|
-
|
222
|
-
# @api private
|
223
|
-
#
|
224
|
-
# @return [String]
|
225
|
-
private def generate_idempotency_key = "stainless-ruby-retry-#{SecureRandom.uuid}"
|
226
|
-
|
227
|
-
# @api private
|
228
|
-
#
|
229
|
-
# @param req [Hash{Symbol=>Object}] .
|
230
|
-
#
|
231
|
-
# @option req [Symbol] :method
|
232
|
-
#
|
233
|
-
# @option req [String, Array<String>] :path
|
234
|
-
#
|
235
|
-
# @option req [Hash{String=>Array<String>, String, nil}, nil] :query
|
236
|
-
#
|
237
|
-
# @option req [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil] :headers
|
238
|
-
#
|
239
|
-
# @option req [Object, nil] :body
|
240
|
-
#
|
241
|
-
# @option req [Symbol, Integer, Array<Symbol, Integer>, Proc, nil] :unwrap
|
242
|
-
#
|
243
|
-
# @option req [Class<Openlayer::Internal::Type::BasePage>, nil] :page
|
244
|
-
#
|
245
|
-
# @option req [Class<Openlayer::Internal::Type::BaseStream>, nil] :stream
|
246
|
-
#
|
247
|
-
# @option req [Openlayer::Internal::Type::Converter, Class, nil] :model
|
248
|
-
#
|
249
|
-
# @param opts [Hash{Symbol=>Object}] .
|
250
|
-
#
|
251
|
-
# @option opts [String, nil] :idempotency_key
|
252
|
-
#
|
253
|
-
# @option opts [Hash{String=>Array<String>, String, nil}, nil] :extra_query
|
254
|
-
#
|
255
|
-
# @option opts [Hash{String=>String, nil}, nil] :extra_headers
|
256
|
-
#
|
257
|
-
# @option opts [Object, nil] :extra_body
|
258
|
-
#
|
259
|
-
# @option opts [Integer, nil] :max_retries
|
260
|
-
#
|
261
|
-
# @option opts [Float, nil] :timeout
|
262
|
-
#
|
263
|
-
# @return [Hash{Symbol=>Object}]
|
264
|
-
private def build_request(req, opts)
|
265
|
-
method, uninterpolated_path = req.fetch_values(:method, :path)
|
266
|
-
|
267
|
-
path = Openlayer::Internal::Util.interpolate_path(uninterpolated_path)
|
268
|
-
|
269
|
-
query = Openlayer::Internal::Util.deep_merge(req[:query].to_h, opts[:extra_query].to_h)
|
270
|
-
|
271
|
-
headers = Openlayer::Internal::Util.normalized_headers(
|
272
|
-
@headers,
|
273
|
-
auth_headers,
|
274
|
-
req[:headers].to_h,
|
275
|
-
opts[:extra_headers].to_h
|
276
|
-
)
|
277
|
-
|
278
|
-
if @idempotency_header &&
|
279
|
-
!headers.key?(@idempotency_header) &&
|
280
|
-
(!Net::HTTP::IDEMPOTENT_METHODS_.include?(method.to_s.upcase) || opts.key?(:idempotency_key))
|
281
|
-
headers[@idempotency_header] = opts.fetch(:idempotency_key) { generate_idempotency_key }
|
282
|
-
end
|
283
|
-
|
284
|
-
unless headers.key?("x-stainless-retry-count")
|
285
|
-
headers["x-stainless-retry-count"] = "0"
|
286
|
-
end
|
287
|
-
|
288
|
-
timeout = opts.fetch(:timeout, @timeout).to_f.clamp(0..)
|
289
|
-
unless headers.key?("x-stainless-timeout") || timeout.zero?
|
290
|
-
headers["x-stainless-timeout"] = timeout.to_s
|
291
|
-
end
|
292
|
-
|
293
|
-
headers.reject! { |_, v| v.to_s.empty? }
|
294
|
-
|
295
|
-
body =
|
296
|
-
case method
|
297
|
-
in :get | :head | :options | :trace
|
298
|
-
nil
|
299
|
-
else
|
300
|
-
Openlayer::Internal::Util.deep_merge(*[req[:body], opts[:extra_body]].compact)
|
301
|
-
end
|
302
|
-
|
303
|
-
url = Openlayer::Internal::Util.join_parsed_uri(
|
304
|
-
@base_url_components,
|
305
|
-
{**req, path: path, query: query}
|
306
|
-
)
|
307
|
-
headers, encoded = Openlayer::Internal::Util.encode_content(headers, body)
|
308
|
-
{
|
309
|
-
method: method,
|
310
|
-
url: url,
|
311
|
-
headers: headers,
|
312
|
-
body: encoded,
|
313
|
-
max_retries: opts.fetch(:max_retries, @max_retries),
|
314
|
-
timeout: timeout
|
315
|
-
}
|
316
|
-
end
|
317
|
-
|
318
|
-
# @api private
|
319
|
-
#
|
320
|
-
# @param headers [Hash{String=>String}]
|
321
|
-
# @param retry_count [Integer]
|
322
|
-
#
|
323
|
-
# @return [Float]
|
324
|
-
private def retry_delay(headers, retry_count:)
|
325
|
-
# Non-standard extension
|
326
|
-
span = Float(headers["retry-after-ms"], exception: false)&.then { _1 / 1000 }
|
327
|
-
return span if span
|
328
|
-
|
329
|
-
retry_header = headers["retry-after"]
|
330
|
-
return span if (span = Float(retry_header, exception: false))
|
331
|
-
|
332
|
-
span = retry_header&.then do
|
333
|
-
Time.httpdate(_1) - Time.now
|
334
|
-
rescue ArgumentError
|
335
|
-
nil
|
336
|
-
end
|
337
|
-
return span if span
|
338
|
-
|
339
|
-
scale = retry_count**2
|
340
|
-
jitter = 1 - (0.25 * rand)
|
341
|
-
(@initial_retry_delay * scale * jitter).clamp(0, @max_retry_delay)
|
342
|
-
end
|
343
|
-
|
344
|
-
# @api private
|
345
|
-
#
|
346
|
-
# @param request [Hash{Symbol=>Object}] .
|
347
|
-
#
|
348
|
-
# @option request [Symbol] :method
|
349
|
-
#
|
350
|
-
# @option request [URI::Generic] :url
|
351
|
-
#
|
352
|
-
# @option request [Hash{String=>String}] :headers
|
353
|
-
#
|
354
|
-
# @option request [Object] :body
|
355
|
-
#
|
356
|
-
# @option request [Integer] :max_retries
|
357
|
-
#
|
358
|
-
# @option request [Float] :timeout
|
359
|
-
#
|
360
|
-
# @param redirect_count [Integer]
|
361
|
-
#
|
362
|
-
# @param retry_count [Integer]
|
363
|
-
#
|
364
|
-
# @param send_retry_header [Boolean]
|
365
|
-
#
|
366
|
-
# @raise [Openlayer::Errors::APIError]
|
367
|
-
# @return [Array(Integer, Net::HTTPResponse, Enumerable<String>)]
|
368
|
-
def send_request(request, redirect_count:, retry_count:, send_retry_header:)
|
369
|
-
url, headers, max_retries, timeout = request.fetch_values(:url, :headers, :max_retries, :timeout)
|
370
|
-
input = {**request.except(:timeout), deadline: Openlayer::Internal::Util.monotonic_secs + timeout}
|
371
|
-
|
372
|
-
if send_retry_header
|
373
|
-
headers["x-stainless-retry-count"] = retry_count.to_s
|
374
|
-
end
|
375
|
-
|
376
|
-
begin
|
377
|
-
status, response, stream = @requester.execute(input)
|
378
|
-
rescue Openlayer::Errors::APIConnectionError => e
|
379
|
-
status = e
|
380
|
-
end
|
381
|
-
headers = Openlayer::Internal::Util.normalized_headers(response&.each_header&.to_h)
|
382
|
-
|
383
|
-
case status
|
384
|
-
in ..299
|
385
|
-
[status, response, stream]
|
386
|
-
in 300..399 if redirect_count >= self.class::MAX_REDIRECTS
|
387
|
-
self.class.reap_connection!(status, stream: stream)
|
388
|
-
|
389
|
-
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
|
390
|
-
raise Openlayer::Errors::APIConnectionError.new(url: url, response: response, message: message)
|
391
|
-
in 300..399
|
392
|
-
self.class.reap_connection!(status, stream: stream)
|
393
|
-
|
394
|
-
request = self.class.follow_redirect(request, status: status, response_headers: headers)
|
395
|
-
send_request(
|
396
|
-
request,
|
397
|
-
redirect_count: redirect_count + 1,
|
398
|
-
retry_count: retry_count,
|
399
|
-
send_retry_header: send_retry_header
|
400
|
-
)
|
401
|
-
in Openlayer::Errors::APIConnectionError if retry_count >= max_retries
|
402
|
-
raise status
|
403
|
-
in (400..) if retry_count >= max_retries || !self.class.should_retry?(status, headers: headers)
|
404
|
-
decoded = Kernel.then do
|
405
|
-
Openlayer::Internal::Util.decode_content(headers, stream: stream, suppress_error: true)
|
406
|
-
ensure
|
407
|
-
self.class.reap_connection!(status, stream: stream)
|
408
|
-
end
|
409
|
-
|
410
|
-
raise Openlayer::Errors::APIStatusError.for(
|
411
|
-
url: url,
|
412
|
-
status: status,
|
413
|
-
headers: headers,
|
414
|
-
body: decoded,
|
415
|
-
request: nil,
|
416
|
-
response: response
|
417
|
-
)
|
418
|
-
in (400..) | Openlayer::Errors::APIConnectionError
|
419
|
-
self.class.reap_connection!(status, stream: stream)
|
420
|
-
|
421
|
-
delay = retry_delay(response || {}, retry_count: retry_count)
|
422
|
-
sleep(delay)
|
423
|
-
|
424
|
-
send_request(
|
425
|
-
request,
|
426
|
-
redirect_count: redirect_count,
|
427
|
-
retry_count: retry_count + 1,
|
428
|
-
send_retry_header: send_retry_header
|
429
|
-
)
|
430
|
-
end
|
431
|
-
end
|
432
|
-
|
433
|
-
# Execute the request specified by `req`. This is the method that all resource
|
434
|
-
# methods call into.
|
435
|
-
#
|
436
|
-
# @overload request(method, path, query: {}, headers: {}, body: nil, unwrap: nil, page: nil, stream: nil, model: Openlayer::Internal::Type::Unknown, options: {})
|
437
|
-
#
|
438
|
-
# @param method [Symbol]
|
439
|
-
#
|
440
|
-
# @param path [String, Array<String>]
|
441
|
-
#
|
442
|
-
# @param query [Hash{String=>Array<String>, String, nil}, nil]
|
443
|
-
#
|
444
|
-
# @param headers [Hash{String=>String, Integer, Array<String, Integer, nil>, nil}, nil]
|
445
|
-
#
|
446
|
-
# @param body [Object, nil]
|
447
|
-
#
|
448
|
-
# @param unwrap [Symbol, Integer, Array<Symbol, Integer>, Proc, nil]
|
449
|
-
#
|
450
|
-
# @param page [Class<Openlayer::Internal::Type::BasePage>, nil]
|
451
|
-
#
|
452
|
-
# @param stream [Class<Openlayer::Internal::Type::BaseStream>, nil]
|
453
|
-
#
|
454
|
-
# @param model [Openlayer::Internal::Type::Converter, Class, nil]
|
455
|
-
#
|
456
|
-
# @param options [Openlayer::RequestOptions, Hash{Symbol=>Object}, nil] .
|
457
|
-
#
|
458
|
-
# @option options [String, nil] :idempotency_key
|
459
|
-
#
|
460
|
-
# @option options [Hash{String=>Array<String>, String, nil}, nil] :extra_query
|
461
|
-
#
|
462
|
-
# @option options [Hash{String=>String, nil}, nil] :extra_headers
|
463
|
-
#
|
464
|
-
# @option options [Object, nil] :extra_body
|
465
|
-
#
|
466
|
-
# @option options [Integer, nil] :max_retries
|
467
|
-
#
|
468
|
-
# @option options [Float, nil] :timeout
|
469
|
-
#
|
470
|
-
# @raise [Openlayer::Errors::APIError]
|
471
|
-
# @return [Object]
|
472
|
-
def request(req)
|
473
|
-
self.class.validate!(req)
|
474
|
-
model = req.fetch(:model) { Openlayer::Internal::Type::Unknown }
|
475
|
-
opts = req[:options].to_h
|
476
|
-
unwrap = req[:unwrap]
|
477
|
-
Openlayer::RequestOptions.validate!(opts)
|
478
|
-
request = build_request(req.except(:options), opts)
|
479
|
-
url = request.fetch(:url)
|
480
|
-
|
481
|
-
# Don't send the current retry count in the headers if the caller modified the header defaults.
|
482
|
-
send_retry_header = request.fetch(:headers)["x-stainless-retry-count"] == "0"
|
483
|
-
status, response, stream = send_request(
|
484
|
-
request,
|
485
|
-
redirect_count: 0,
|
486
|
-
retry_count: 0,
|
487
|
-
send_retry_header: send_retry_header
|
488
|
-
)
|
489
|
-
|
490
|
-
headers = Openlayer::Internal::Util.normalized_headers(response.each_header.to_h)
|
491
|
-
decoded = Openlayer::Internal::Util.decode_content(headers, stream: stream)
|
492
|
-
case req
|
493
|
-
in {stream: Class => st}
|
494
|
-
st.new(
|
495
|
-
model: model,
|
496
|
-
url: url,
|
497
|
-
status: status,
|
498
|
-
headers: headers,
|
499
|
-
response: response,
|
500
|
-
unwrap: unwrap,
|
501
|
-
stream: decoded
|
502
|
-
)
|
503
|
-
in {page: Class => page}
|
504
|
-
page.new(client: self, req: req, headers: headers, page_data: decoded)
|
505
|
-
else
|
506
|
-
unwrapped = Openlayer::Internal::Util.dig(decoded, unwrap)
|
507
|
-
Openlayer::Internal::Type::Converter.coerce(model, unwrapped)
|
508
|
-
end
|
509
|
-
end
|
510
|
-
|
511
|
-
# @api private
|
512
|
-
#
|
513
|
-
# @return [String]
|
514
|
-
def inspect
|
515
|
-
# rubocop:disable Layout/LineLength
|
516
|
-
"#<#{self.class.name}:0x#{object_id.to_s(16)} base_url=#{@base_url} max_retries=#{@max_retries} timeout=#{@timeout}>"
|
517
|
-
# rubocop:enable Layout/LineLength
|
518
|
-
end
|
519
|
-
|
520
|
-
define_sorbet_constant!(:RequestComponents) do
|
521
|
-
T.type_alias do
|
522
|
-
{
|
523
|
-
method: Symbol,
|
524
|
-
path: T.any(String, T::Array[String]),
|
525
|
-
query: T.nilable(T::Hash[String, T.nilable(T.any(T::Array[String], String))]),
|
526
|
-
headers: T.nilable(
|
527
|
-
T::Hash[String,
|
528
|
-
T.nilable(
|
529
|
-
T.any(
|
530
|
-
String,
|
531
|
-
Integer,
|
532
|
-
T::Array[T.nilable(T.any(String, Integer))]
|
533
|
-
)
|
534
|
-
)]
|
535
|
-
),
|
536
|
-
body: T.nilable(T.anything),
|
537
|
-
unwrap: T.nilable(
|
538
|
-
T.any(
|
539
|
-
Symbol,
|
540
|
-
Integer,
|
541
|
-
T::Array[T.any(Symbol, Integer)],
|
542
|
-
T.proc.params(arg0: T.anything).returns(T.anything)
|
543
|
-
)
|
544
|
-
),
|
545
|
-
page: T.nilable(T::Class[Openlayer::Internal::Type::BasePage[Openlayer::Internal::Type::BaseModel]]),
|
546
|
-
stream: T.nilable(T::Class[T.anything]),
|
547
|
-
model: T.nilable(Openlayer::Internal::Type::Converter::Input),
|
548
|
-
options: T.nilable(Openlayer::RequestOptions::OrHash)
|
549
|
-
}
|
550
|
-
end
|
551
|
-
end
|
552
|
-
define_sorbet_constant!(:RequestInput) do
|
553
|
-
T.type_alias do
|
554
|
-
{
|
555
|
-
method: Symbol,
|
556
|
-
url: URI::Generic,
|
557
|
-
headers: T::Hash[String, String],
|
558
|
-
body: T.anything,
|
559
|
-
max_retries: Integer,
|
560
|
-
timeout: Float
|
561
|
-
}
|
562
|
-
end
|
563
|
-
end
|
564
|
-
end
|
565
|
-
end
|
566
|
-
end
|
567
|
-
end
|