Class: BddOpenai::Client::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bdd_openai/clients/http.rb

Overview

An HTTP client

Instance Method Summary collapse

Instance Method Details

#call_delete(uri, headers, disable_ssl: false) ⇒ Net::HTTPResponse

Parameters:

  • uri (URI::Generic)
  • headers (Hash)
  • disable_ssl (Boolean) (defaults to: false)

Returns:

  • (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

Parameters:

  • uri (URI::Generic)
  • headers (Hash)
  • disable_ssl (Boolean) (defaults to: false)

Returns:

  • (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

Parameters:

  • uri (URI::Generic)
  • req_body (String)

    Request body in the form of JSON string.

  • headers (Hash)
  • disable_ssl (Boolean) (defaults to: false)

Returns:

  • (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.

Parameters:

  • fields (Hash)

    The fields to be sent in the request body

  • file_fields (Hash)

    The file fields to be sent in the request body, with value as the file path.

Returns:

  • (String, String (frozen))

    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