Skip to content

Commit 6bd8a03

Browse files
committed
Try and parse everything from string to JSON
1 parent 2f7cfb7 commit 6bd8a03

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lib/openai/http.rb

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,49 @@ module HTTP
77
include HTTPHeaders
88

99
def get(path:, parameters: nil)
10-
parse_jsonl(conn.get(uri(path: path), parameters) do |req|
10+
parse_json(conn.get(uri(path: path), parameters) do |req|
1111
req.headers = headers
1212
end&.body)
1313
end
1414

1515
def post(path:)
16-
parse_jsonl(conn.post(uri(path: path)) do |req|
16+
parse_json(conn.post(uri(path: path)) do |req|
1717
req.headers = headers
1818
end&.body)
1919
end
2020

2121
def json_post(path:, parameters:, query_parameters: {})
22-
conn.post(uri(path: path)) do |req|
22+
parse_json(conn.post(uri(path: path)) do |req|
2323
configure_json_post_request(req, parameters)
2424
req.params = req.params.merge(query_parameters)
25-
end&.body
25+
end&.body)
2626
end
2727

2828
def multipart_post(path:, parameters: nil)
29-
conn(multipart: true).post(uri(path: path)) do |req|
29+
parse_json(conn(multipart: true).post(uri(path: path)) do |req|
3030
req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
3131
req.body = multipart_parameters(parameters)
32-
end&.body
32+
end&.body)
3333
end
3434

3535
def delete(path:)
36-
conn.delete(uri(path: path)) do |req|
36+
parse_json(conn.delete(uri(path: path)) do |req|
3737
req.headers = headers
38-
end&.body
38+
end&.body)
3939
end
4040

4141
private
4242

43-
def parse_jsonl(response)
43+
def parse_json(response)
4444
return unless response
4545
return response unless response.is_a?(String)
4646

47-
# Convert a multiline string of JSON objects to a JSON array.
48-
response = response.gsub("}\n{", "},{").prepend("[").concat("]")
47+
if response.include?("}\n{")
48+
# Convert a multiline string of JSON objects to a JSON array.
49+
response = response.gsub("}\n{", "},{").prepend("[").concat("]")
50+
end
4951

50-
JSON.parse(response)
51-
rescue JSON::ParserError
52-
response
52+
try_parse_json(response)
5353
end
5454

5555
# Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks.

0 commit comments

Comments
 (0)