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.

Parameters:

  • base_url (String)

    The base url for the request.

  • max_retries (Integer) (defaults to: 2)

    The number of times to retry a failed request, defaults to 2.

  • timeout (Float) (defaults to: 60.0)

    The timeout for the request, defaults to 60.0 seconds.

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

    The headers for the request.



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.

Parameters:

  • url (URI::Generic)

    The url to the resource.

  • method (String)

    The HTTP method to use.

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

    The headers for the request.

  • body (String, nil) (defaults to: nil)

    The body for the request.

Returns:

  • (HTTP::Request)

    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.

Parameters:

Returns:

  • (URI::Generic)

    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.

Parameters:

  • url (URI::Generic)

    The url to connect to.

Returns:

  • (Net::HTTP)

    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.

Parameters:

  • query (Hash)

    The query for the request.

Returns:

  • (String, nil)

    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.

Parameters:

Returns:

  • (HTTP::Response)

    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