Skip to content

Commit c80b432

Browse files
chore(internal): minor touch ups on sdk internals
1 parent 591d0b2 commit c80b432

File tree

7 files changed

+63
-61
lines changed

7 files changed

+63
-61
lines changed

lib/openai/internal/cursor_page.rb

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,6 @@ class CursorPage
2222
# @return [Boolean]
2323
attr_accessor :has_more
2424

25-
# @api private
26-
#
27-
# @param client [OpenAI::Internal::Transport::BaseClient]
28-
# @param req [Hash{Symbol=>Object}]
29-
# @param headers [Hash{String=>String}, Net::HTTPHeader]
30-
# @param page_data [Hash{Symbol=>Object}]
31-
def initialize(client:, req:, headers:, page_data:)
32-
super
33-
34-
case page_data
35-
in {data: Array | nil => data}
36-
@data = data&.map { OpenAI::Internal::Type::Converter.coerce(@model, _1) }
37-
else
38-
end
39-
40-
case page_data
41-
in {has_more: true | false | nil => has_more}
42-
@has_more = has_more
43-
else
44-
end
45-
end
46-
4725
# @return [Boolean]
4826
def next_page?
4927
has_more
@@ -78,6 +56,23 @@ def auto_paging_each(&blk)
7856
end
7957
end
8058

59+
# @api private
60+
#
61+
# @param client [OpenAI::Internal::Transport::BaseClient]
62+
# @param req [Hash{Symbol=>Object}]
63+
# @param headers [Hash{String=>String}, Net::HTTPHeader]
64+
# @param page_data [Hash{Symbol=>Object}]
65+
def initialize(client:, req:, headers:, page_data:)
66+
super
67+
68+
case page_data
69+
in {data: Array | nil => data}
70+
@data = data&.map { OpenAI::Internal::Type::Converter.coerce(@model, _1) }
71+
else
72+
end
73+
@has_more = page_data[:has_more]
74+
end
75+
8176
# @api private
8277
#
8378
# @return [String]

lib/openai/internal/page.rb

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,6 @@ class Page
2222
# @return [String]
2323
attr_accessor :object
2424

25-
# @api private
26-
#
27-
# @param client [OpenAI::Internal::Transport::BaseClient]
28-
# @param req [Hash{Symbol=>Object}]
29-
# @param headers [Hash{String=>String}, Net::HTTPHeader]
30-
# @param page_data [Array<Object>]
31-
def initialize(client:, req:, headers:, page_data:)
32-
super
33-
34-
case page_data
35-
in {data: Array | nil => data}
36-
@data = data&.map { OpenAI::Internal::Type::Converter.coerce(@model, _1) }
37-
else
38-
end
39-
40-
case page_data
41-
in {object: String => object}
42-
@object = object
43-
else
44-
end
45-
end
46-
4725
# @return [Boolean]
4826
def next_page?
4927
false
@@ -72,6 +50,23 @@ def auto_paging_each(&blk)
7250
end
7351
end
7452

53+
# @api private
54+
#
55+
# @param client [OpenAI::Internal::Transport::BaseClient]
56+
# @param req [Hash{Symbol=>Object}]
57+
# @param headers [Hash{String=>String}, Net::HTTPHeader]
58+
# @param page_data [Array<Object>]
59+
def initialize(client:, req:, headers:, page_data:)
60+
super
61+
62+
case page_data
63+
in {data: Array | nil => data}
64+
@data = data&.map { OpenAI::Internal::Type::Converter.coerce(@model, _1) }
65+
else
66+
end
67+
@object = page_data[:object]
68+
end
69+
7570
# @api private
7671
#
7772
# @return [String]

lib/openai/internal/transport/base_client.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,23 @@ def follow_redirect(request, status:, response_headers:)
9393
URI.join(url, response_headers["location"])
9494
rescue ArgumentError
9595
message = "Server responded with status #{status} but no valid location header."
96-
raise OpenAI::Errors::APIConnectionError.new(url: url, message: message)
96+
raise OpenAI::Errors::APIConnectionError.new(
97+
url: url,
98+
response: response_headers,
99+
message: message
100+
)
97101
end
98102

99103
request = {**request, url: location}
100104

101105
case [url.scheme, location.scheme]
102106
in ["https", "http"]
103107
message = "Tried to redirect to a insecure URL"
104-
raise OpenAI::Errors::APIConnectionError.new(url: url, message: message)
108+
raise OpenAI::Errors::APIConnectionError.new(
109+
url: url,
110+
response: response_headers,
111+
message: message
112+
)
105113
else
106114
nil
107115
end
@@ -350,7 +358,7 @@ def initialize(
350358
self.class.reap_connection!(status, stream: stream)
351359

352360
message = "Failed to complete the request within #{self.class::MAX_REDIRECTS} redirects."
353-
raise OpenAI::Errors::APIConnectionError.new(url: url, message: message)
361+
raise OpenAI::Errors::APIConnectionError.new(url: url, response: response, message: message)
354362
in 300..399
355363
self.class.reap_connection!(status, stream: stream)
356364

lib/openai/internal/type/array_of.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class ArrayOf
1515

1616
private_class_method :new
1717

18+
# @overload [](type_info, spec = {})
19+
#
1820
# @param type_info [Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class]
1921
#
2022
# @param spec [Hash{Symbol=>Object}] .
@@ -26,7 +28,7 @@ class ArrayOf
2628
# @option spec [Proc] :union
2729
#
2830
# @option spec [Boolean] :"nil?"
29-
def self.[](type_info, spec = {}) = new(type_info, spec)
31+
def self.[](...) = new(...)
3032

3133
# @param other [Object]
3234
#
@@ -131,9 +133,9 @@ def initialize(type_info, spec = {})
131133
#
132134
# @return [String]
133135
def inspect(depth: 0)
134-
# rubocop:disable Layout/LineLength
135-
"#{self.class}[#{[OpenAI::Internal::Type::Converter.inspect(item_type, depth: depth.succ), nilable? ? 'nil' : nil].compact.join(' | ')}]"
136-
# rubocop:enable Layout/LineLength
136+
items = OpenAI::Internal::Type::Converter.inspect(item_type, depth: depth.succ)
137+
138+
"#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]"
137139
end
138140
end
139141
end

lib/openai/internal/type/enum.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ def coerce(value, state:)
114114
#
115115
# @return [String]
116116
def inspect(depth: 0)
117-
# rubocop:disable Layout/LineLength
118117
return super() if depth.positive?
119118

120-
"#{name}[#{values.map { OpenAI::Internal::Type::Converter.inspect(_1, depth: depth.succ) }.join(' | ')}]"
121-
# rubocop:enable Layout/LineLength
119+
members = values.map { OpenAI::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
120+
121+
"#{name}[#{members.join(' | ')}]"
122122
end
123123
end
124124
end

lib/openai/internal/type/hash_of.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ class HashOf
1515

1616
private_class_method :new
1717

18+
# @overload [](type_info, spec = {})
19+
#
1820
# @param type_info [Hash{Symbol=>Object}, Proc, OpenAI::Internal::Type::Converter, Class]
1921
#
2022
# @param spec [Hash{Symbol=>Object}] .
@@ -26,7 +28,7 @@ class HashOf
2628
# @option spec [Proc] :union
2729
#
2830
# @option spec [Boolean] :"nil?"
29-
def self.[](type_info, spec = {}) = new(type_info, spec)
31+
def self.[](...) = new(...)
3032

3133
# @param other [Object]
3234
#
@@ -151,9 +153,9 @@ def initialize(type_info, spec = {})
151153
#
152154
# @return [String]
153155
def inspect(depth: 0)
154-
# rubocop:disable Layout/LineLength
155-
"#{self.class}[#{[OpenAI::Internal::Type::Converter.inspect(item_type, depth: depth.succ), nilable? ? 'nil' : nil].compact.join(' | ')}]"
156-
# rubocop:enable Layout/LineLength
156+
items = OpenAI::Internal::Type::Converter.inspect(item_type, depth: depth.succ)
157+
158+
"#{self.class}[#{[items, nilable? ? 'nil' : nil].compact.join(' | ')}]"
157159
end
158160
end
159161
end

lib/openai/internal/type/union.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ def dump(value, state:)
217217
#
218218
# @return [String]
219219
def inspect(depth: 0)
220-
# rubocop:disable Layout/LineLength
221220
return super() if depth.positive?
222221

223-
"#{name}[#{variants.map { OpenAI::Internal::Type::Converter.inspect(_1, depth: depth.succ) }.join(' | ')}]"
224-
# rubocop:enable Layout/LineLength
222+
members = variants.map { OpenAI::Internal::Type::Converter.inspect(_1, depth: depth.succ) }
223+
224+
"#{name}[#{members.join(' | ')}]"
225225
end
226226
end
227227
end

0 commit comments

Comments
 (0)