Class: Openai::Client::Http

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/client/http.rb

Instance Method Summary collapse

Constructor Details

#initializeHttp

Returns a new instance of Http.



6
7
8
9
10
11
12
13
14
# File 'lib/openai/client/http.rb', line 6

def initialize
  @connection = Faraday.new(url: Openai::Client.configuration.openai_url, headers: headers) do |conn|
    conn.response :json
    conn.response :raise_error
    conn.request :json
    conn.request :multipart
  end
  @logger = Openai::Client.configuration.logger
end

Instance Method Details

#delete(path) ⇒ Faraday::Response

Public: Makes a DELETE request using the Faraday HTTP Client.

Parameters:

  • path (String)

    API path

Returns:

  • (Faraday::Response)

    instance of Faraday::Response class

Raises:

  • (Faraday::Error)

    on failure API call



72
73
74
75
76
# File 'lib/openai/client/http.rb', line 72

def delete(path)
  connection.delete(path)
rescue Faraday::Error => e
  log_error(e) && raise
end

#get(path, query_params = {}) ⇒ Faraday::Response

Public: Makes a GET request using the Faraday HTTP Client.

Parameters:

  • path (String)

    API path

  • query_params (String) (defaults to: {})

    query params

Returns:

  • (Faraday::Response)

    instance of Faraday::Response class

Raises:

  • (Faraday::Error)

    on failure API call



25
26
27
28
29
# File 'lib/openai/client/http.rb', line 25

def get(path, query_params = {})
  connection.get(path, query_params)
rescue Faraday::Error => e
  log_error(e) && raise
end

#multipart_post(path, body = {}) ⇒ Faraday::Response

Public: Makes a multipart request using the Faraday HTTP Client.

Parameters:

  • path (String)

    API path

  • body (Hash) (defaults to: {})

    request body

Returns:

  • (Faraday::Response)

    instance of Faraday::Response class

Raises:

  • (Faraday::Error)

    on failure API call



55
56
57
58
59
60
61
62
# File 'lib/openai/client/http.rb', line 55

def multipart_post(path, body = {})
  connection.post(path, body) do |request|
    request.headers['Content-Type'] = 'multipart/form-data'
    request.options.timeout = 300
  end
rescue Faraday::Error => e
  log_error(e) && raise
end

#post(path, body = {}) ⇒ Faraday::Response

Public: Makes a POST request using the Faraday HTTP Client.

Parameters:

  • path (String)

    API path

  • body (Hash) (defaults to: {})

    request body

Returns:

  • (Faraday::Response)

    instance of Faraday::Response class

Raises:

  • (Faraday::Error)

    on failure API call



40
41
42
43
44
# File 'lib/openai/client/http.rb', line 40

def post(path, body = {})
  connection.post(path, body)
rescue Faraday::Error => e
  log_error(e) && raise
end