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



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: 300,
    read_timeout: 300,
    write_timeout: 300,
    request_timeout: 300
  }
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