turbopuffer 0.1.0 → 1.2.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 +4 -4
- data/.ignore +2 -0
- data/CHANGELOG.md +205 -0
- data/README.md +270 -40
- data/SECURITY.md +27 -0
- data/lib/turbopuffer/client.rb +122 -16
- data/lib/turbopuffer/errors.rb +228 -0
- data/lib/turbopuffer/file_part.rb +55 -0
- data/lib/turbopuffer/internal/namespace_page.rb +86 -0
- data/lib/turbopuffer/internal/transport/base_client.rb +567 -0
- data/lib/turbopuffer/internal/transport/pooled_net_requester.rb +201 -0
- data/lib/turbopuffer/internal/type/array_of.rb +168 -0
- data/lib/turbopuffer/internal/type/base_model.rb +529 -0
- data/lib/turbopuffer/internal/type/base_page.rb +55 -0
- data/lib/turbopuffer/internal/type/boolean.rb +77 -0
- data/lib/turbopuffer/internal/type/converter.rb +327 -0
- data/lib/turbopuffer/internal/type/enum.rb +152 -0
- data/lib/turbopuffer/internal/type/file_input.rb +108 -0
- data/lib/turbopuffer/internal/type/hash_of.rb +188 -0
- data/lib/turbopuffer/internal/type/request_parameters.rb +42 -0
- data/lib/turbopuffer/internal/type/union.rb +254 -0
- data/lib/turbopuffer/internal/type/unknown.rb +81 -0
- data/lib/turbopuffer/internal/util.rb +914 -0
- data/lib/turbopuffer/internal.rb +20 -0
- data/lib/turbopuffer/models/aggregation_group.rb +8 -0
- data/lib/turbopuffer/models/attribute_schema.rb +19 -0
- data/lib/turbopuffer/models/attribute_schema_config.rb +56 -0
- data/lib/turbopuffer/models/attribute_type.rb +7 -0
- data/lib/turbopuffer/models/client_namespaces_params.rb +38 -0
- data/lib/turbopuffer/models/columns.rb +64 -0
- data/lib/turbopuffer/models/distance_metric.rb +19 -0
- data/lib/turbopuffer/models/full_text_search.rb +20 -0
- data/lib/turbopuffer/models/full_text_search_config.rb +83 -0
- data/lib/turbopuffer/models/id.rb +19 -0
- data/lib/turbopuffer/models/include_attributes.rb +22 -0
- data/lib/turbopuffer/models/language.rb +32 -0
- data/lib/turbopuffer/models/namespace_delete_all_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_delete_all_response.rb +19 -0
- data/lib/turbopuffer/models/namespace_explain_query_params.rb +137 -0
- data/lib/turbopuffer/models/namespace_explain_query_response.rb +19 -0
- data/lib/turbopuffer/models/namespace_hint_cache_warm_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_hint_cache_warm_response.rb +26 -0
- data/lib/turbopuffer/models/namespace_metadata.rb +43 -0
- data/lib/turbopuffer/models/namespace_metadata_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_multi_query_params.rb +73 -0
- data/lib/turbopuffer/models/namespace_multi_query_response.rb +58 -0
- data/lib/turbopuffer/models/namespace_query_params.rb +137 -0
- data/lib/turbopuffer/models/namespace_query_response.rb +49 -0
- data/lib/turbopuffer/models/namespace_recall_params.rb +66 -0
- data/lib/turbopuffer/models/namespace_recall_response.rb +68 -0
- data/lib/turbopuffer/models/namespace_schema_params.rb +20 -0
- data/lib/turbopuffer/models/namespace_schema_response.rb +8 -0
- data/lib/turbopuffer/models/namespace_summary.rb +19 -0
- data/lib/turbopuffer/models/namespace_update_schema_params.rb +29 -0
- data/lib/turbopuffer/models/namespace_update_schema_response.rb +9 -0
- data/lib/turbopuffer/models/namespace_write_params.rb +158 -0
- data/lib/turbopuffer/models/namespace_write_response.rb +67 -0
- data/lib/turbopuffer/models/query.rb +81 -0
- data/lib/turbopuffer/models/query_billing.rb +26 -0
- data/lib/turbopuffer/models/query_performance.rb +63 -0
- data/lib/turbopuffer/models/row.rb +46 -0
- data/lib/turbopuffer/models/tokenizer.rb +19 -0
- data/lib/turbopuffer/models/vector.rb +22 -0
- data/lib/turbopuffer/models/vector_encoding.rb +16 -0
- data/lib/turbopuffer/models/write_billing.rb +26 -0
- data/lib/turbopuffer/models.rb +106 -0
- data/lib/turbopuffer/namespace.rb +14 -50
- data/lib/turbopuffer/request_options.rb +77 -0
- data/lib/turbopuffer/resources/namespaces.rb +366 -0
- data/lib/turbopuffer/version.rb +1 -1
- data/lib/turbopuffer.rb +92 -4
- data/manifest.yaml +15 -0
- data/rbi/turbopuffer/client.rbi +91 -0
- data/rbi/turbopuffer/errors.rbi +205 -0
- data/rbi/turbopuffer/file_part.rbi +37 -0
- data/rbi/turbopuffer/internal/namespace_page.rbi +22 -0
- data/rbi/turbopuffer/internal/transport/base_client.rbi +297 -0
- data/rbi/turbopuffer/internal/transport/pooled_net_requester.rbi +80 -0
- data/rbi/turbopuffer/internal/type/array_of.rbi +104 -0
- data/rbi/turbopuffer/internal/type/base_model.rbi +304 -0
- data/rbi/turbopuffer/internal/type/base_page.rbi +43 -0
- data/rbi/turbopuffer/internal/type/boolean.rbi +58 -0
- data/rbi/turbopuffer/internal/type/converter.rbi +216 -0
- data/rbi/turbopuffer/internal/type/enum.rbi +82 -0
- data/rbi/turbopuffer/internal/type/file_input.rbi +59 -0
- data/rbi/turbopuffer/internal/type/hash_of.rbi +104 -0
- data/rbi/turbopuffer/internal/type/request_parameters.rbi +31 -0
- data/rbi/turbopuffer/internal/type/union.rbi +128 -0
- data/rbi/turbopuffer/internal/type/unknown.rbi +58 -0
- data/rbi/turbopuffer/internal/util.rbi +487 -0
- data/rbi/turbopuffer/internal.rbi +18 -0
- data/rbi/turbopuffer/models/aggregation_group.rbi +13 -0
- data/rbi/turbopuffer/models/attribute_schema.rbi +17 -0
- data/rbi/turbopuffer/models/attribute_schema_config.rbi +103 -0
- data/rbi/turbopuffer/models/attribute_type.rbi +7 -0
- data/rbi/turbopuffer/models/client_namespaces_params.rbi +71 -0
- data/rbi/turbopuffer/models/columns.rbi +82 -0
- data/rbi/turbopuffer/models/distance_metric.rbi +27 -0
- data/rbi/turbopuffer/models/full_text_search.rbi +19 -0
- data/rbi/turbopuffer/models/full_text_search_config.rbi +133 -0
- data/rbi/turbopuffer/models/id.rbi +16 -0
- data/rbi/turbopuffer/models/include_attributes.rbi +24 -0
- data/rbi/turbopuffer/models/language.rbi +36 -0
- data/rbi/turbopuffer/models/namespace_delete_all_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_delete_all_response.rbi +31 -0
- data/rbi/turbopuffer/models/namespace_explain_query_params.rbi +273 -0
- data/rbi/turbopuffer/models/namespace_explain_query_response.rbi +34 -0
- data/rbi/turbopuffer/models/namespace_hint_cache_warm_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_hint_cache_warm_response.rbi +38 -0
- data/rbi/turbopuffer/models/namespace_metadata.rbi +62 -0
- data/rbi/turbopuffer/models/namespace_metadata_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_multi_query_params.rbi +175 -0
- data/rbi/turbopuffer/models/namespace_multi_query_response.rbi +121 -0
- data/rbi/turbopuffer/models/namespace_query_params.rbi +267 -0
- data/rbi/turbopuffer/models/namespace_query_response.rbi +84 -0
- data/rbi/turbopuffer/models/namespace_recall_params.rbi +106 -0
- data/rbi/turbopuffer/models/namespace_recall_response.rbi +134 -0
- data/rbi/turbopuffer/models/namespace_schema_params.rbi +41 -0
- data/rbi/turbopuffer/models/namespace_schema_response.rbi +11 -0
- data/rbi/turbopuffer/models/namespace_summary.rbi +28 -0
- data/rbi/turbopuffer/models/namespace_update_schema_params.rbi +80 -0
- data/rbi/turbopuffer/models/namespace_update_schema_response.rbi +11 -0
- data/rbi/turbopuffer/models/namespace_write_params.rbi +290 -0
- data/rbi/turbopuffer/models/namespace_write_response.rbi +101 -0
- data/rbi/turbopuffer/models/query.rbi +132 -0
- data/rbi/turbopuffer/models/query_billing.rbi +46 -0
- data/rbi/turbopuffer/models/query_performance.rbi +82 -0
- data/rbi/turbopuffer/models/row.rbi +47 -0
- data/rbi/turbopuffer/models/tokenizer.rbi +24 -0
- data/rbi/turbopuffer/models/vector.rbi +22 -0
- data/rbi/turbopuffer/models/vector_encoding.rbi +22 -0
- data/rbi/turbopuffer/models/write_billing.rbi +49 -0
- data/rbi/turbopuffer/models.rbi +72 -0
- data/rbi/turbopuffer/namespace.rbi +8 -0
- data/rbi/turbopuffer/request_options.rbi +59 -0
- data/rbi/turbopuffer/resources/namespaces.rbi +304 -0
- data/rbi/turbopuffer/version.rbi +5 -0
- data/sig/turbopuffer/client.rbs +41 -0
- data/sig/turbopuffer/errors.rbs +117 -0
- data/sig/turbopuffer/file_part.rbs +21 -0
- data/sig/turbopuffer/internal/namespace_page.rbs +13 -0
- data/sig/turbopuffer/internal/transport/base_client.rbs +131 -0
- data/sig/turbopuffer/internal/transport/pooled_net_requester.rbs +45 -0
- data/sig/turbopuffer/internal/type/array_of.rbs +48 -0
- data/sig/turbopuffer/internal/type/base_model.rbs +102 -0
- data/sig/turbopuffer/internal/type/base_page.rbs +24 -0
- data/sig/turbopuffer/internal/type/boolean.rbs +26 -0
- data/sig/turbopuffer/internal/type/converter.rbs +79 -0
- data/sig/turbopuffer/internal/type/enum.rbs +32 -0
- data/sig/turbopuffer/internal/type/file_input.rbs +25 -0
- data/sig/turbopuffer/internal/type/hash_of.rbs +48 -0
- data/sig/turbopuffer/internal/type/request_parameters.rbs +19 -0
- data/sig/turbopuffer/internal/type/union.rbs +52 -0
- data/sig/turbopuffer/internal/type/unknown.rbs +26 -0
- data/sig/turbopuffer/internal/util.rbs +185 -0
- data/sig/turbopuffer/internal.rbs +9 -0
- data/sig/turbopuffer/models/aggregation_group.rbs +7 -0
- data/sig/turbopuffer/models/attribute_schema.rbs +11 -0
- data/sig/turbopuffer/models/attribute_schema_config.rbs +52 -0
- data/sig/turbopuffer/models/attribute_type.rbs +5 -0
- data/sig/turbopuffer/models/client_namespaces_params.rbs +38 -0
- data/sig/turbopuffer/models/columns.rbs +42 -0
- data/sig/turbopuffer/models/distance_metric.rbs +17 -0
- data/sig/turbopuffer/models/full_text_search.rbs +11 -0
- data/sig/turbopuffer/models/full_text_search_config.rbs +75 -0
- data/sig/turbopuffer/models/id.rbs +11 -0
- data/sig/turbopuffer/models/include_attributes.rbs +13 -0
- data/sig/turbopuffer/models/language.rbs +48 -0
- data/sig/turbopuffer/models/namespace_delete_all_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_delete_all_response.rbs +13 -0
- data/sig/turbopuffer/models/namespace_explain_query_params.rbs +141 -0
- data/sig/turbopuffer/models/namespace_explain_query_response.rbs +15 -0
- data/sig/turbopuffer/models/namespace_hint_cache_warm_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_hint_cache_warm_response.rbs +18 -0
- data/sig/turbopuffer/models/namespace_metadata.rbs +35 -0
- data/sig/turbopuffer/models/namespace_metadata_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_multi_query_params.rbs +86 -0
- data/sig/turbopuffer/models/namespace_multi_query_response.rbs +65 -0
- data/sig/turbopuffer/models/namespace_query_params.rbs +139 -0
- data/sig/turbopuffer/models/namespace_query_response.rbs +48 -0
- data/sig/turbopuffer/models/namespace_recall_params.rbs +63 -0
- data/sig/turbopuffer/models/namespace_recall_response.rbs +61 -0
- data/sig/turbopuffer/models/namespace_schema_params.rbs +25 -0
- data/sig/turbopuffer/models/namespace_schema_response.rbs +8 -0
- data/sig/turbopuffer/models/namespace_summary.rbs +13 -0
- data/sig/turbopuffer/models/namespace_update_schema_params.rbs +37 -0
- data/sig/turbopuffer/models/namespace_update_schema_response.rbs +8 -0
- data/sig/turbopuffer/models/namespace_write_params.rbs +156 -0
- data/sig/turbopuffer/models/namespace_write_response.rbs +56 -0
- data/sig/turbopuffer/models/query.rbs +75 -0
- data/sig/turbopuffer/models/query_billing.rbs +25 -0
- data/sig/turbopuffer/models/query_performance.rbs +45 -0
- data/sig/turbopuffer/models/row.rbs +24 -0
- data/sig/turbopuffer/models/tokenizer.rbs +16 -0
- data/sig/turbopuffer/models/vector.rbs +13 -0
- data/sig/turbopuffer/models/vector_encoding.rbs +14 -0
- data/sig/turbopuffer/models/write_billing.rbs +27 -0
- data/sig/turbopuffer/models.rbs +65 -0
- data/sig/turbopuffer/namespace.rbs +5 -0
- data/sig/turbopuffer/request_options.rbs +36 -0
- data/sig/turbopuffer/resources/namespaces.rbs +99 -0
- data/sig/turbopuffer/version.rbs +3 -0
- metadata +218 -29
- data/.standard.yml +0 -3
- data/LICENSE.txt +0 -21
- data/Rakefile +0 -10
- data/sig/turbopuffer.rbs +0 -4
data/lib/turbopuffer/client.rb
CHANGED
@@ -1,24 +1,130 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "faraday"
|
4
|
-
|
5
3
|
module Turbopuffer
|
6
|
-
class Client
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
4
|
+
class Client < Turbopuffer::Internal::Transport::BaseClient
|
5
|
+
# Default max number of retries to attempt after a failed retryable request.
|
6
|
+
DEFAULT_MAX_RETRIES = 4
|
7
|
+
|
8
|
+
# Default per-request timeout.
|
9
|
+
DEFAULT_TIMEOUT_IN_SECONDS = 60.0
|
10
|
+
|
11
|
+
# Default initial retry delay in seconds.
|
12
|
+
# Overall delay is calculated using exponential backoff + jitter.
|
13
|
+
DEFAULT_INITIAL_RETRY_DELAY = 0.3
|
14
|
+
|
15
|
+
# Default max retry delay in seconds.
|
16
|
+
DEFAULT_MAX_RETRY_DELAY = 8.0
|
17
|
+
|
18
|
+
# API key used for authentication
|
19
|
+
# @return [String]
|
20
|
+
attr_reader :api_key
|
21
|
+
|
22
|
+
# The turbopuffer region to use.
|
23
|
+
# @return [String, nil]
|
24
|
+
attr_reader :region
|
25
|
+
|
26
|
+
# @return [String, nil]
|
27
|
+
attr_reader :default_namespace
|
28
|
+
|
29
|
+
# Creates a new namespace resource.
|
30
|
+
#
|
31
|
+
# @param namespace [String] The ID of the namespace.
|
32
|
+
#
|
33
|
+
# @return [Turbopuffer::Namespace]
|
34
|
+
def namespace(namespace)
|
35
|
+
Turbopuffer::Namespace.new(self, namespace)
|
36
|
+
end
|
37
|
+
|
38
|
+
# List namespaces.
|
39
|
+
#
|
40
|
+
# @overload namespaces(cursor: nil, page_size: nil, prefix: nil, request_options: {})
|
41
|
+
#
|
42
|
+
# @param cursor [String] Retrieve the next page of results.
|
43
|
+
#
|
44
|
+
# @param page_size [Integer] Limit the number of results per page.
|
45
|
+
#
|
46
|
+
# @param prefix [String] Retrieve only the namespaces that match the prefix.
|
47
|
+
#
|
48
|
+
# @param request_options [Turbopuffer::RequestOptions, Hash{Symbol=>Object}, nil]
|
49
|
+
#
|
50
|
+
# @return [Turbopuffer::Internal::NamespacePage<Turbopuffer::Models::NamespaceSummary>]
|
51
|
+
#
|
52
|
+
# @see Turbopuffer::Models::ClientNamespacesParams
|
53
|
+
def namespaces(params = {})
|
54
|
+
parsed, options = Turbopuffer::ClientNamespacesParams.dump_request(params)
|
55
|
+
request(
|
56
|
+
method: :get,
|
57
|
+
path: "v1/namespaces",
|
58
|
+
query: parsed,
|
59
|
+
page: Turbopuffer::Internal::NamespacePage,
|
60
|
+
model: Turbopuffer::NamespaceSummary,
|
61
|
+
options: options
|
62
|
+
)
|
18
63
|
end
|
19
64
|
|
20
|
-
|
21
|
-
|
65
|
+
# @api private
|
66
|
+
#
|
67
|
+
# @return [Hash{String=>String}]
|
68
|
+
private def auth_headers
|
69
|
+
return {} if @api_key.nil?
|
70
|
+
|
71
|
+
{"authorization" => "Bearer #{@api_key}"}
|
72
|
+
end
|
73
|
+
|
74
|
+
# Creates and returns a new client for interacting with the API.
|
75
|
+
#
|
76
|
+
# @param api_key [String, nil] API key used for authentication Defaults to `ENV["TURBOPUFFER_API_KEY"]`
|
77
|
+
#
|
78
|
+
# @param region [String, nil] The turbopuffer region to use. Defaults to `ENV["TURBOPUFFER_REGION"]`
|
79
|
+
#
|
80
|
+
# @param default_namespace [String, nil]
|
81
|
+
#
|
82
|
+
# @param base_url [String, nil] Override the default base URL for the API, e.g.,
|
83
|
+
# `"https://api.example.com/v2/"`. Defaults to `ENV["TURBOPUFFER_BASE_URL"]`
|
84
|
+
#
|
85
|
+
# @param max_retries [Integer] Max number of retries to attempt after a failed retryable request.
|
86
|
+
#
|
87
|
+
# @param timeout [Float]
|
88
|
+
#
|
89
|
+
# @param initial_retry_delay [Float]
|
90
|
+
#
|
91
|
+
# @param max_retry_delay [Float]
|
92
|
+
def initialize(
|
93
|
+
api_key: ENV["TURBOPUFFER_API_KEY"],
|
94
|
+
region: ENV["TURBOPUFFER_REGION"],
|
95
|
+
default_namespace: nil,
|
96
|
+
base_url: ENV["TURBOPUFFER_BASE_URL"],
|
97
|
+
max_retries: self.class::DEFAULT_MAX_RETRIES,
|
98
|
+
timeout: self.class::DEFAULT_TIMEOUT_IN_SECONDS,
|
99
|
+
initial_retry_delay: self.class::DEFAULT_INITIAL_RETRY_DELAY,
|
100
|
+
max_retry_delay: self.class::DEFAULT_MAX_RETRY_DELAY
|
101
|
+
)
|
102
|
+
base_url ||= "https://{region}.turbopuffer.com"
|
103
|
+
|
104
|
+
if api_key.nil?
|
105
|
+
raise ArgumentError.new("api_key is required, and can be set via environ: \"TURBOPUFFER_API_KEY\"")
|
106
|
+
end
|
107
|
+
|
108
|
+
if base_url.include?("{region}")
|
109
|
+
if region.nil?
|
110
|
+
raise ArgumentError.new("region is required when base_url contains {region} placeholder: #{base_url}")
|
111
|
+
end
|
112
|
+
base_url = base_url.gsub("{region}", region)
|
113
|
+
elsif !region.nil?
|
114
|
+
raise ArgumentError.new("region is set, but would be ignored (baseUrl does not contain {region} placeholder: #{base_url})")
|
115
|
+
end
|
116
|
+
|
117
|
+
@default_namespace = default_namespace&.to_s
|
118
|
+
@api_key = api_key.to_s
|
119
|
+
@region = region&.to_s
|
120
|
+
|
121
|
+
super(
|
122
|
+
base_url: base_url,
|
123
|
+
timeout: timeout,
|
124
|
+
max_retries: max_retries,
|
125
|
+
initial_retry_delay: initial_retry_delay,
|
126
|
+
max_retry_delay: max_retry_delay
|
127
|
+
)
|
22
128
|
end
|
23
129
|
end
|
24
130
|
end
|
@@ -0,0 +1,228 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Turbopuffer
|
4
|
+
module Errors
|
5
|
+
class Error < StandardError
|
6
|
+
# @!attribute cause
|
7
|
+
#
|
8
|
+
# @return [StandardError, nil]
|
9
|
+
end
|
10
|
+
|
11
|
+
class ConversionError < Turbopuffer::Errors::Error
|
12
|
+
# @return [StandardError, nil]
|
13
|
+
def cause = @cause.nil? ? super : @cause
|
14
|
+
|
15
|
+
# @api private
|
16
|
+
#
|
17
|
+
# @param on [Class<StandardError>]
|
18
|
+
# @param method [Symbol]
|
19
|
+
# @param target [Object]
|
20
|
+
# @param value [Object]
|
21
|
+
# @param cause [StandardError, nil]
|
22
|
+
def initialize(on:, method:, target:, value:, cause: nil)
|
23
|
+
cls = on.name.split("::").last
|
24
|
+
|
25
|
+
message = [
|
26
|
+
"Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
|
27
|
+
"To get the unparsed API response, use #{cls}[#{method.inspect}].",
|
28
|
+
cause && "Cause: #{cause.message}"
|
29
|
+
].filter(&:itself).join(" ")
|
30
|
+
|
31
|
+
@cause = cause
|
32
|
+
super(message)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class APIError < Turbopuffer::Errors::Error
|
37
|
+
# @return [URI::Generic]
|
38
|
+
attr_accessor :url
|
39
|
+
|
40
|
+
# @return [Integer, nil]
|
41
|
+
attr_accessor :status
|
42
|
+
|
43
|
+
# @return [Hash{String=>String}, nil]
|
44
|
+
attr_accessor :headers
|
45
|
+
|
46
|
+
# @return [Object, nil]
|
47
|
+
attr_accessor :body
|
48
|
+
|
49
|
+
# @api private
|
50
|
+
#
|
51
|
+
# @param url [URI::Generic]
|
52
|
+
# @param status [Integer, nil]
|
53
|
+
# @param headers [Hash{String=>String}, nil]
|
54
|
+
# @param body [Object, nil]
|
55
|
+
# @param request [nil]
|
56
|
+
# @param response [nil]
|
57
|
+
# @param message [String, nil]
|
58
|
+
def initialize(url:, status: nil, headers: nil, body: nil, request: nil, response: nil, message: nil)
|
59
|
+
@url = url
|
60
|
+
@status = status
|
61
|
+
@headers = headers
|
62
|
+
@body = body
|
63
|
+
@request = request
|
64
|
+
@response = response
|
65
|
+
super(message)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
class APIConnectionError < Turbopuffer::Errors::APIError
|
70
|
+
# @!attribute status
|
71
|
+
#
|
72
|
+
# @return [nil]
|
73
|
+
|
74
|
+
# @!attribute body
|
75
|
+
#
|
76
|
+
# @return [nil]
|
77
|
+
|
78
|
+
# @api private
|
79
|
+
#
|
80
|
+
# @param url [URI::Generic]
|
81
|
+
# @param status [nil]
|
82
|
+
# @param headers [Hash{String=>String}, nil]
|
83
|
+
# @param body [nil]
|
84
|
+
# @param request [nil]
|
85
|
+
# @param response [nil]
|
86
|
+
# @param message [String, nil]
|
87
|
+
def initialize(
|
88
|
+
url:,
|
89
|
+
status: nil,
|
90
|
+
headers: nil,
|
91
|
+
body: nil,
|
92
|
+
request: nil,
|
93
|
+
response: nil,
|
94
|
+
message: "Connection error."
|
95
|
+
)
|
96
|
+
super
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class APITimeoutError < Turbopuffer::Errors::APIConnectionError
|
101
|
+
# @api private
|
102
|
+
#
|
103
|
+
# @param url [URI::Generic]
|
104
|
+
# @param status [nil]
|
105
|
+
# @param headers [Hash{String=>String}, nil]
|
106
|
+
# @param body [nil]
|
107
|
+
# @param request [nil]
|
108
|
+
# @param response [nil]
|
109
|
+
# @param message [String, nil]
|
110
|
+
def initialize(
|
111
|
+
url:,
|
112
|
+
status: nil,
|
113
|
+
headers: nil,
|
114
|
+
body: nil,
|
115
|
+
request: nil,
|
116
|
+
response: nil,
|
117
|
+
message: "Request timed out."
|
118
|
+
)
|
119
|
+
super
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
class APIStatusError < Turbopuffer::Errors::APIError
|
124
|
+
# @api private
|
125
|
+
#
|
126
|
+
# @param url [URI::Generic]
|
127
|
+
# @param status [Integer]
|
128
|
+
# @param headers [Hash{String=>String}, nil]
|
129
|
+
# @param body [Object, nil]
|
130
|
+
# @param request [nil]
|
131
|
+
# @param response [nil]
|
132
|
+
# @param message [String, nil]
|
133
|
+
#
|
134
|
+
# @return [self]
|
135
|
+
def self.for(url:, status:, headers:, body:, request:, response:, message: nil)
|
136
|
+
kwargs =
|
137
|
+
{
|
138
|
+
url: url,
|
139
|
+
status: status,
|
140
|
+
headers: headers,
|
141
|
+
body: body,
|
142
|
+
request: request,
|
143
|
+
response: response,
|
144
|
+
message: message
|
145
|
+
}
|
146
|
+
|
147
|
+
case status
|
148
|
+
in 400
|
149
|
+
Turbopuffer::Errors::BadRequestError.new(**kwargs)
|
150
|
+
in 401
|
151
|
+
Turbopuffer::Errors::AuthenticationError.new(**kwargs)
|
152
|
+
in 403
|
153
|
+
Turbopuffer::Errors::PermissionDeniedError.new(**kwargs)
|
154
|
+
in 404
|
155
|
+
Turbopuffer::Errors::NotFoundError.new(**kwargs)
|
156
|
+
in 409
|
157
|
+
Turbopuffer::Errors::ConflictError.new(**kwargs)
|
158
|
+
in 422
|
159
|
+
Turbopuffer::Errors::UnprocessableEntityError.new(**kwargs)
|
160
|
+
in 429
|
161
|
+
Turbopuffer::Errors::RateLimitError.new(**kwargs)
|
162
|
+
in (500..)
|
163
|
+
Turbopuffer::Errors::InternalServerError.new(**kwargs)
|
164
|
+
else
|
165
|
+
Turbopuffer::Errors::APIStatusError.new(**kwargs)
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
# @!parse
|
170
|
+
# # @return [Integer]
|
171
|
+
# attr_accessor :status
|
172
|
+
|
173
|
+
# @api private
|
174
|
+
#
|
175
|
+
# @param url [URI::Generic]
|
176
|
+
# @param status [Integer]
|
177
|
+
# @param headers [Hash{String=>String}, nil]
|
178
|
+
# @param body [Object, nil]
|
179
|
+
# @param request [nil]
|
180
|
+
# @param response [nil]
|
181
|
+
# @param message [String, nil]
|
182
|
+
def initialize(url:, status:, headers:, body:, request:, response:, message: nil)
|
183
|
+
message ||= {url: url.to_s, status: status, body: body}
|
184
|
+
super(
|
185
|
+
url: url,
|
186
|
+
status: status,
|
187
|
+
headers: headers,
|
188
|
+
body: body,
|
189
|
+
request: request,
|
190
|
+
response: response,
|
191
|
+
message: message&.to_s
|
192
|
+
)
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
class BadRequestError < Turbopuffer::Errors::APIStatusError
|
197
|
+
HTTP_STATUS = 400
|
198
|
+
end
|
199
|
+
|
200
|
+
class AuthenticationError < Turbopuffer::Errors::APIStatusError
|
201
|
+
HTTP_STATUS = 401
|
202
|
+
end
|
203
|
+
|
204
|
+
class PermissionDeniedError < Turbopuffer::Errors::APIStatusError
|
205
|
+
HTTP_STATUS = 403
|
206
|
+
end
|
207
|
+
|
208
|
+
class NotFoundError < Turbopuffer::Errors::APIStatusError
|
209
|
+
HTTP_STATUS = 404
|
210
|
+
end
|
211
|
+
|
212
|
+
class ConflictError < Turbopuffer::Errors::APIStatusError
|
213
|
+
HTTP_STATUS = 409
|
214
|
+
end
|
215
|
+
|
216
|
+
class UnprocessableEntityError < Turbopuffer::Errors::APIStatusError
|
217
|
+
HTTP_STATUS = 422
|
218
|
+
end
|
219
|
+
|
220
|
+
class RateLimitError < Turbopuffer::Errors::APIStatusError
|
221
|
+
HTTP_STATUS = 429
|
222
|
+
end
|
223
|
+
|
224
|
+
class InternalServerError < Turbopuffer::Errors::APIStatusError
|
225
|
+
HTTP_STATUS = (500..)
|
226
|
+
end
|
227
|
+
end
|
228
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Turbopuffer
|
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 [String, nil]
|
42
|
+
# @param content_type [String, nil]
|
43
|
+
def initialize(content, filename: nil, content_type: nil)
|
44
|
+
@content = content
|
45
|
+
@filename =
|
46
|
+
case content
|
47
|
+
in Pathname
|
48
|
+
filename.nil? ? content.basename.to_path : ::File.basename(filename)
|
49
|
+
else
|
50
|
+
filename.nil? ? nil : ::File.basename(filename)
|
51
|
+
end
|
52
|
+
@content_type = content_type
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Turbopuffer
|
4
|
+
module Internal
|
5
|
+
# @generic Elem
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# if namespace_page.has_next?
|
9
|
+
# namespace_page = namespace_page.next_page
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# @example
|
13
|
+
# namespace_page.auto_paging_each do |client|
|
14
|
+
# puts(client)
|
15
|
+
# end
|
16
|
+
class NamespacePage
|
17
|
+
include Turbopuffer::Internal::Type::BasePage
|
18
|
+
|
19
|
+
# @return [Array<generic<Elem>>, nil]
|
20
|
+
attr_accessor :namespaces
|
21
|
+
|
22
|
+
# @return [String]
|
23
|
+
attr_accessor :next_cursor
|
24
|
+
|
25
|
+
# @return [Boolean]
|
26
|
+
def next_page?
|
27
|
+
!namespaces.to_a.empty? && !next_cursor.to_s.empty?
|
28
|
+
end
|
29
|
+
|
30
|
+
# @raise [Turbopuffer::HTTP::Error]
|
31
|
+
# @return [self]
|
32
|
+
def next_page
|
33
|
+
unless next_page?
|
34
|
+
message = "No more pages available. Please check #next_page? before calling ##{__method__}"
|
35
|
+
raise RuntimeError.new(message)
|
36
|
+
end
|
37
|
+
|
38
|
+
req = Turbopuffer::Internal::Util.deep_merge(@req, {query: {cursor: next_cursor}})
|
39
|
+
@client.request(req)
|
40
|
+
end
|
41
|
+
|
42
|
+
# @param blk [Proc]
|
43
|
+
#
|
44
|
+
# @yieldparam [generic<Elem>]
|
45
|
+
def auto_paging_each(&blk)
|
46
|
+
unless block_given?
|
47
|
+
raise ArgumentError.new("A block must be given to ##{__method__}")
|
48
|
+
end
|
49
|
+
|
50
|
+
page = self
|
51
|
+
loop do
|
52
|
+
page.namespaces&.each(&blk)
|
53
|
+
|
54
|
+
break unless page.next_page?
|
55
|
+
page = page.next_page
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# @api private
|
60
|
+
#
|
61
|
+
# @param client [Turbopuffer::Internal::Transport::BaseClient]
|
62
|
+
# @param req [Hash{Symbol=>Object}]
|
63
|
+
# @param headers [Hash{String=>String}]
|
64
|
+
# @param page_data [Hash{Symbol=>Object}]
|
65
|
+
def initialize(client:, req:, headers:, page_data:)
|
66
|
+
super
|
67
|
+
|
68
|
+
case page_data
|
69
|
+
in {namespaces: Array => namespaces}
|
70
|
+
@namespaces = namespaces.map { Turbopuffer::Internal::Type::Converter.coerce(@model, _1) }
|
71
|
+
else
|
72
|
+
end
|
73
|
+
@next_cursor = page_data[:next_cursor]
|
74
|
+
end
|
75
|
+
|
76
|
+
# @api private
|
77
|
+
#
|
78
|
+
# @return [String]
|
79
|
+
def inspect
|
80
|
+
model = Turbopuffer::Internal::Type::Converter.inspect(@model, depth: 1)
|
81
|
+
|
82
|
+
"#<#{self.class}[#{model}]:0x#{object_id.to_s(16)} next_cursor=#{next_cursor.inspect}>"
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|