Class: OpenAI::ResponseMetadata

Inherits:
Object
  • Object
show all
Defined in:
lib/openai/http_client.rb,
sig/openai/http_client.rbs

Overview

Metadata from the HTTP response backing a top-level SDK value.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers:, body: nil) ⇒ ResponseMetadata

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of ResponseMetadata.

Parameters:

  • status (Integer)
  • headers (Hash{String=>String})
  • body (String, nil) (defaults to: nil)


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/openai/http_client.rb', line 34

def initialize(status:, headers:, body: nil)
  @status = Integer(status)
  @headers = headers
    .to_h do |name, value|
      [name.to_s.downcase.freeze, value.to_s.dup.freeze]
    end
    .freeze
  @request_id = @headers["x-request-id"]
  @body = body.nil? || body.frozen? ? body : body.dup.freeze
  freeze
end

Instance Attribute Details

#bodyString? (readonly)

The exact response body when include_raw_body: true was requested. Bodies are never retained by default or for streaming responses.

Returns:

  • (String, nil)


27
28
29
# File 'lib/openai/http_client.rb', line 27

def body
  @body
end

#headersHash{String=>String} (readonly)

Immutable response headers with lowercase String keys.

Returns:

  • (Hash{String=>String})


16
17
18
# File 'lib/openai/http_client.rb', line 16

def headers
  @headers
end

#request_idString? (readonly)

The request ID returned in the x-request-id response header.

Returns:

  • (String, nil)


21
22
23
# File 'lib/openai/http_client.rb', line 21

def request_id
  @request_id
end

#statusInteger (readonly)

The HTTP response status code.

Returns:

  • (Integer)


11
12
13
# File 'lib/openai/http_client.rb', line 11

def status
  @status
end

Instance Method Details

#encode_with(coder) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Keep retained response bodies out of Psych object serialization.

Parameters:

  • coder (Psych::Coder)


56
57
58
59
# File 'lib/openai/http_client.rb', line 56

def encode_with(coder)
  coder["status"] = @status
  coder["headers"] = @headers
end

#init_with(coder) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • coder (Psych::Coder)


64
65
66
# File 'lib/openai/http_client.rb', line 64

def init_with(coder)
  initialize(status: coder["status"], headers: coder["headers"])
end

#inspectString

Inspect metadata without exposing sensitive response headers.

Returns:

  • (String)


49
# File 'lib/openai/http_client.rb', line 49

def inspect = "#<#{self.class} status=#{@status} request_id=#{@request_id.inspect}>"

#marshal_dumpArray(Integer, Hash{String=>String})

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Keep retained response bodies out of Marshal object serialization.

Returns:

  • (Array(Integer, Hash{String=>String}))


72
# File 'lib/openai/http_client.rb', line 72

def marshal_dump = [@status, @headers]

#marshal_load(values) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • values (Array(Integer, Hash{String=>String}))


77
78
79
80
# File 'lib/openai/http_client.rb', line 77

def marshal_load(values)
  status, headers = values
  initialize(status: status, headers: headers)
end