Class: Candid::Internal::Http::RawClient Private

Inherits:
Object
  • Object
show all
Defined in:
lib/candid/internal/http/raw_client.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(base_url:, max_retries: 2, timeout: 60.0, headers: {}) ⇒ RawClient

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 RawClient.



12
13
14
15
16
17
18
19
20
21
# File 'lib/candid/internal/http/raw_client.rb', line 12

def initialize(base_url:, max_retries: 2, timeout: 60.0, headers: {})
  @base_url = base_url
  @max_retries = max_retries
  @timeout = timeout
  @default_headers = {
    "X-Fern-Language": "Ruby",
    "X-Fern-SDK-Name": "candid",
    "X-Fern-SDK-Version": "0.0.1"
  }.merge(headers)
end

Instance Method Details

#build_http_request(url:, method:, headers: {}, body: nil) ⇒ HTTP::Request

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 The HTTP request.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/candid/internal/http/raw_client.rb', line 63

def build_http_request(url:, method:, headers: {}, body: nil)
  request = Net::HTTPGenericRequest.new(
    method,
    !body.nil?,
    method != "HEAD",
    url
  )

  request_headers = @default_headers.merge(headers)
  request_headers.each { |name, value| request[name] = value }
  request.body = body if body

  request
end

#build_url(request) ⇒ URI::Generic

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 The URL.



51
52
53
54
55
56
# File 'lib/candid/internal/http/raw_client.rb', line 51

def build_url(request)
  path = request.path.start_with?("/") ? request.path[1..] : request.path
  url = "#{@base_url.chomp("/")}/#{path}"
  url = "#{url}?#{encode_query(request.query)}" if request.query&.any?
  URI.parse(url)
end

#connect(url) ⇒ Net::HTTP

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 The HTTP connection.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/candid/internal/http/raw_client.rb', line 86

def connect(url)
  is_https = (url.scheme == "https")

  port = if url.port
           url.port
         elsif is_https
           Net::HTTP.https_default_port
         else
           Net::HTTP.http_default_port
         end

  http = Net::HTTP.new(url.host, port)
  http.use_ssl = is_https
  http.max_retries = 0
  http
end

#encode_query(query) ⇒ 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.

Returns The encoded query.



80
81
82
# File 'lib/candid/internal/http/raw_client.rb', line 80

def encode_query(query)
  query.to_h.empty? ? nil : URI.encode_www_form(query)
end

#send(request) ⇒ HTTP::Response

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 The HTTP response.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/candid/internal/http/raw_client.rb', line 25

def send(request)
  url = build_url(request)

  http_request = build_http_request(
    url:,
    method: request.method,
    headers: request.encode_headers,
    body: request.encode_body
  )

  conn = connect(url)
  conn.open_timeout = @timeout
  conn.read_timeout = @timeout
  conn.write_timeout = @timeout
  conn.continue_timeout = @timeout

  conn.request(http_request)
  # begin
  #   conn.request(http_request)
  # rescue StandardError => e
  #   raise HttpError, "HTTP request failed: #{e.message}"
  # end
end