lib/open_router/http.rb in open_router-0.2.2 vs lib/open_router/http.rb in open_router-0.3.0

- old
+ new

@@ -1,51 +1,42 @@ # frozen_string_literal: true module OpenRouter module HTTP def get(path:) - to_json(conn.get(uri(path:)) do |req| + conn.get(uri(path:)) do |req| req.headers = headers - end&.body) + end&.body end - def json_post(path:, parameters:) - to_json(conn.post(uri(path:)) do |req| + def post(path:, parameters:) + conn.post(uri(path:)) do |req| if parameters[:stream].respond_to?(:call) req.options.on_data = to_json_stream(user_proc: parameters[:stream]) parameters[:stream] = true # Necessary to tell OpenRouter to stream. end req.headers = headers req.body = parameters.to_json - end&.body) + end&.body end def multipart_post(path:, parameters: nil) - to_json(conn(multipart: true).post(uri(path:)) do |req| + conn(multipart: true).post(uri(path:)) do |req| req.headers = headers.merge({ "Content-Type" => "multipart/form-data" }) req.body = multipart_parameters(parameters) - end&.body) + end&.body end def delete(path:) - to_json(conn.delete(uri(path:)) do |req| + conn.delete(uri(path:)) do |req| req.headers = headers - end&.body) + end&.body end private - def to_json(string) - return unless string - - JSON.parse(string) - rescue JSON::ParserError - # Convert a multiline string of JSON objects to a JSON array. - JSON.parse(string.gsub("}\n{", "},{").prepend("[").concat("]")) - end - # Given a proc, returns an outer proc that can be used to iterate over a JSON stream of chunks. # For each chunk, the inner user_proc is called giving it the JSON object. The JSON object could # be a data object or an error object as described in the OpenRouter API documentation. # # If the JSON object for a given data or error message is invalid, it is ignored. @@ -64,9 +55,14 @@ def conn(multipart: false) Faraday.new do |f| f.options[:timeout] = OpenRouter.configuration.request_timeout f.request(:multipart) if multipart + f.use MiddlewareErrors if @log_errors + f.response :raise_error + f.response :json + + OpenRouter.configuration.faraday_config&.call(f) end end def uri(path:) File.join(OpenRouter.configuration.uri_base, OpenRouter.configuration.api_version, path)