Module: Anthropic::HTTP

Includes:
HTTPHeaders
Included in:
Client
Defined in:
lib/anthropic/http.rb

Instance Method Summary collapse

Methods included from HTTPHeaders

#add_headers

Instance Method Details

#delete(path:) ⇒ Object

Unused?



46
47
48
49
50
# File 'lib/anthropic/http.rb', line 46

def delete(path:)
  to_json(conn.delete(uri(path: path)) do |req|
    req.headers = headers
  end&.body)
end

#get(path:) ⇒ Object

Unused?



11
12
13
14
15
# File 'lib/anthropic/http.rb', line 11

def get(path:)
  to_json(conn.get(uri(path: path)) do |req|
    req.headers = headers
  end&.body)
end

#json_post(path:, parameters:) ⇒ Object

This is currently the workhorse for all API calls. rubocop:disable Metrics/MethodLength



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/anthropic/http.rb', line 19

def json_post(path:, parameters:)
  str_resp = {}
  response = conn.post(uri(path: path)) do |req|
    if parameters[:stream].is_a?(Proc)
      req.options.on_data = to_json_stream(user_proc: parameters[:stream], response: str_resp,
                                           preprocess: parameters[:preprocess_stream])
      parameters[:stream] = true # Necessary to tell Anthropic to stream.
      parameters.delete(:preprocess_stream)
    end

    req.headers = headers
    req.body = parameters.to_json
  end

  str_resp.empty? ? response.body : str_resp
end

#multipart_post(path:, parameters: nil) ⇒ Object

Unused?



38
39
40
41
42
43
# File 'lib/anthropic/http.rb', line 38

def multipart_post(path:, parameters: nil)
  to_json(conn(multipart: true).post(uri(path: path)) do |req|
    req.headers = headers.merge({ "Content-Type" => "multipart/form-data" })
    req.body = multipart_parameters(parameters)
  end&.body)
end