Class: BddOpenai::Client::HttpClient
- Inherits:
-
Object
- Object
- BddOpenai::Client::HttpClient
- Defined in:
- lib/bdd_openai/clients/http.rb
Overview
An HTTP client
Instance Method Summary collapse
- #call_delete(uri, headers, disable_ssl: false) ⇒ Net::HTTPResponse
- #call_get(uri, headers, disable_ssl: false) ⇒ Net::HTTPResponse
- #call_post(uri, req_body, headers, disable_ssl: false) ⇒ Net::HTTPResponse
-
#create_multipart_body(fields, file_fields) ⇒ String, String (frozen)
The request body and the boundary string.
Instance Method Details
#call_delete(uri, headers, disable_ssl: false) ⇒ Net::HTTPResponse
57 58 59 60 61 62 |
# File 'lib/bdd_openai/clients/http.rb', line 57 def call_delete(uri, headers, disable_ssl: false) http = Net::HTTP.new(uri.host || '', uri.port) http.use_ssl = disable_ssl ? false : true request = Net::HTTP::Delete.new(uri.path || '', headers) http.request(request) end |
#call_get(uri, headers, disable_ssl: false) ⇒ Net::HTTPResponse
12 13 14 15 16 17 |
# File 'lib/bdd_openai/clients/http.rb', line 12 def call_get(uri, headers, disable_ssl: false) http = Net::HTTP.new(uri.host || '', uri.port) http.use_ssl = disable_ssl ? false : true request = Net::HTTP::Get.new(uri.path || '', headers) http.request(request) end |
#call_post(uri, req_body, headers, disable_ssl: false) ⇒ Net::HTTPResponse
24 25 26 27 28 29 30 |
# File 'lib/bdd_openai/clients/http.rb', line 24 def call_post(uri, req_body, headers, disable_ssl: false) http = Net::HTTP.new(uri.host || '', uri.port) http.use_ssl = disable_ssl ? false : true request = Net::HTTP::Post.new(uri.path || '', headers) request.body = req_body unless req_body.nil? http.request(request) end |
#create_multipart_body(fields, file_fields) ⇒ String, String (frozen)
Returns The request body and the boundary string.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bdd_openai/clients/http.rb', line 35 def create_multipart_body(fields, file_fields) boundary = "----#{SecureRandom.hex(16)}" body = +'' fields.each do |name, value| body << "--#{boundary}\r\n" body << "Content-Disposition: form-data; name=\"#{name}\"\r\n\r\n" body << "#{value}\r\n" end file_fields.each do |name, file_path| body << "--#{boundary}\r\n" body << "Content-Disposition: form-data; name=\"#{name}\"; filename=\"#{File.basename(file_path)}\"\r\n" body << "Content-Type: application/octet-stream\r\n\r\n" body << "#{File.read(file_path)}\r\n" end body << "--#{boundary}--\r\n" [body, boundary] end |