Class: Tng::HttpClient

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

Instance Method Summary collapse

Constructor Details

#initialize(api_endpoint, api_key) ⇒ HttpClient

Returns a new instance of HttpClient.



5
6
7
8
9
10
11
12
13
14
# File 'lib/tng/api/http_client.rb', line 5

def initialize(api_endpoint, api_key)
  @api_endpoint = api_endpoint
  @api_key = api_key
  @timeout = {
    connect_timeout: 420,
    read_timeout: 420,
    write_timeout: 420,
    request_timeout: 420
  }
end

Instance Method Details

#get(path, headers: {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tng/api/http_client.rb', line 42

def get(path, headers: {})
  merged_headers = json_default_headers.merge(headers)

  response = HTTPX.with(timeout: @timeout).get(
    "#{@api_endpoint}/#{path}",
    headers: merged_headers
  )

  debug_response("GET #{path}", response) if debug_enabled?
  response
end

#patch(path, payload: {}, headers: {}) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/tng/api/http_client.rb', line 54

def patch(path, payload: {}, headers: {})
  merged_headers = json_default_headers.merge(headers)

  response = HTTPX.with(timeout: @timeout).patch(
    "#{@api_endpoint}/#{path}",
    json: payload,
    headers: merged_headers
  )

  debug_response("PATCH #{path}", response) if debug_enabled?
  response
end

#pingObject



67
68
69
70
71
# File 'lib/tng/api/http_client.rb', line 67

def ping
  response = HTTPX.with(timeout: @timeout).get("#{@api_endpoint}/ping")
  debug_response("GET /ping", response) if debug_enabled?
  response
end

#post(path, payload: {}, headers: {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tng/api/http_client.rb', line 16

def post(path, payload: {}, headers: {})
  merged_headers = json_default_headers.merge(headers)

  response = HTTPX.with(timeout: @timeout).post(
    "#{@api_endpoint}/#{path}",
    json: payload,
    headers: merged_headers
  )

  debug_response("POST #{path}", response) if debug_enabled?
  response
end

#post_binary(path, data, headers: {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tng/api/http_client.rb', line 29

def post_binary(path, data, headers: {})
  merged_headers = stream_default_headers.merge(headers)

  response = HTTPX.with(timeout: @timeout).post(
    "#{@api_endpoint}/#{path}",
    body: data,
    headers: merged_headers
  )

  debug_response("POST #{path} (binary)", response) if debug_enabled?
  response
end

#verify_authObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/tng/api/http_client.rb', line 73

def verify_auth
  response = get("cli/tng_rails/stats")

  return { valid: false, error: "Network error" } if response.is_a?(HTTPX::ErrorResponse)

  case response.status
  when 200, 201
    { valid: true, error: nil }
  when 401
    { valid: false, error: "Invalid or missing API key" }
  when 403
    { valid: false, error: "API key expired or usage limit reached" }
  else
    { valid: false, error: "Unexpected error (#{response.status})" }
  end
end