Class: Bitfab::HttpClient

Inherits:
Object
  • Object
show all
Defined in:
lib/bitfab/http_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key:, service_url: nil, timeout: 120) ⇒ HttpClient

Returns a new instance of HttpClient.



14
15
16
17
18
# File 'lib/bitfab/http_client.rb', line 14

def initialize(api_key:, service_url: nil, timeout: 120)
  @api_key = api_key
  @service_url = (service_url || DEFAULT_SERVICE_URL).chomp("/")
  @timeout = timeout
end

Instance Attribute Details

#service_urlObject (readonly)

Returns the value of attribute service_url.



12
13
14
# File 'lib/bitfab/http_client.rb', line 12

def service_url
  @service_url
end

Instance Method Details

#complete_replay(test_run_id) ⇒ Object

Mark a replay test run as completed. Blocking call.



117
118
119
# File 'lib/bitfab/http_client.rb', line 117

def complete_replay(test_run_id)
  request("/api/sdk/replay/complete", {"testRunId" => test_run_id}, timeout: 30)
end

#get(endpoint, timeout: nil) ⇒ Object

Make a GET request to the Bitfab API. Returns parsed JSON response hash.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/bitfab/http_client.rb', line 72

def get(endpoint, timeout: nil)
  uri = URI("#{@service_url}#{endpoint}")
  request_timeout = timeout || @timeout

  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = uri.scheme == "https"
  http.open_timeout = request_timeout
  http.read_timeout = request_timeout

  req = Net::HTTP::Get.new(uri.path, headers)
  response = http.request(req)

  unless response.is_a?(Net::HTTPSuccess)
    raise Net::HTTPError.new("HTTP #{response.code}: #{response.body}", response)
  end

  result = JSON.parse(response.body)

  if result["error"]
    msg = result["error"]
    msg = "#{msg} Configure it at: #{@service_url}#{result["url"]}" if result["url"]
    raise StandardError, msg
  end

  result
end

#get_external_span(span_id) ⇒ Object

Fetch an external span by ID. Blocking GET request.



112
113
114
# File 'lib/bitfab/http_client.rb', line 112

def get_external_span(span_id)
  get("/api/sdk/externalSpans/#{span_id}", timeout: 30)
end

#request(endpoint, payload, timeout: nil, max_retries: 1, retry_delay: 0.1) ⇒ Object

Make a POST request to the Bitfab API. Returns parsed JSON response hash.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/bitfab/http_client.rb', line 22

def request(endpoint, payload, timeout: nil, max_retries: 1, retry_delay: 0.1)
  uri = URI("#{@service_url}#{endpoint}")
  request_timeout = timeout || @timeout

  last_error = nil

  max_retries.times do |attempt|
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == "https"
    http.open_timeout = request_timeout
    http.read_timeout = request_timeout

    req = Net::HTTP::Post.new(uri.path, headers)
    req.body = JSON.generate(payload)

    response = http.request(req)

    unless response.is_a?(Net::HTTPSuccess)
      raise Net::HTTPError.new("HTTP #{response.code}: #{response.body}", response)
    end

    result = JSON.parse(response.body)

    if result["error"]
      msg = result["error"]
      msg = "#{msg} Configure it at: #{@service_url}#{result["url"]}" if result["url"]
      raise StandardError, msg
    end

    return result
  rescue => e
    last_error = e
    sleep(retry_delay) if attempt < max_retries - 1
  end

  raise last_error
end

#send_external_span(payload) ⇒ Object

Send an external span in a background thread. Returns the thread for callers that need to await completion.



62
63
64
65
66
67
68
# File 'lib/bitfab/http_client.rb', line 62

def send_external_span(payload)
  merged = payload.merge("sdkVersion" => VERSION)

  Bitfab._run_in_background do
    request("/api/sdk/externalSpans", merged, timeout: 30)
  end
end

#send_external_trace(payload) ⇒ Object

Send an external trace (fire-and-forget in background thread).



122
123
124
125
126
127
128
# File 'lib/bitfab/http_client.rb', line 122

def send_external_trace(payload)
  merged = payload.merge("sdkVersion" => VERSION)

  Bitfab._run_in_background do
    request("/api/sdk/externalTraces", merged, timeout: 10)
  end
end

#start_replay(trace_function_key, limit, trace_ids: nil) ⇒ Object

Start a replay session by fetching historical traces. Blocking call. Returns hash with testRunId, testRunUrl, and items array.



101
102
103
104
105
106
107
108
109
# File 'lib/bitfab/http_client.rb', line 101

def start_replay(trace_function_key, limit, trace_ids: nil)
  payload = {
    "traceFunctionKey" => trace_function_key,
    "limit" => limit
  }
  payload["traceIds"] = trace_ids if trace_ids

  request("/api/sdk/replay/start", payload, timeout: 30)
end