Class: Central::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/central/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_url, default_headers = {}) ⇒ Client

Initialize api client

Parameters:

  • api_url (String)
  • default_headers (Hash) (defaults to: {})


15
16
17
18
19
20
21
22
23
24
# File 'lib/central/client.rb', line 15

def initialize(api_url, default_headers = {})
  Excon.defaults[:ssl_verify_peer] = false if ignore_ssl_errors?
  @http_client = Excon.new(api_url)
  @default_headers = {
    'Accept' => 'application/json',
    'Content-Type' => 'application/json',
    'User-Agent' => "central-cli/#{Central::Cli::VERSION}"
  }.merge(default_headers)
  @api_url = api_url
end

Instance Attribute Details

#default_headersObject

Returns the value of attribute default_headers.



8
9
10
# File 'lib/central/client.rb', line 8

def default_headers
  @default_headers
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



9
10
11
# File 'lib/central/client.rb', line 9

def http_client
  @http_client
end

Instance Method Details

#delete(path, body = nil, params = {}, headers = {}) ⇒ Hash

Delete request

Parameters:

  • path (String)
  • body (Hash, String) (defaults to: nil)
  • params (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/central/client.rb', line 116

def delete(path, body = nil, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
    path: request_uri(path),
    headers: request_headers,
    body: encode_body(body, request_headers['Content-Type']),
    query: params
  }
  response = http_client.delete(request_options)
  if response.status == 200
    parse_response(response)
  else
    handle_error_response(response)
  end
end

#get(path, params = nil, headers = {}) ⇒ Hash

Get request

Parameters:

  • path (String)
  • params (Hash, NilClass) (defaults to: nil)
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/central/client.rb', line 32

def get(path, params = nil, headers = {})
  response = http_client.get(
    path: request_uri(path),
    query: params,
    headers: request_headers(headers)
  )
  if response.status == 200
    parse_response(response)
  else
    handle_error_response(response)
  end
end

#get_stream(path, response_block, params = nil, headers = {}) ⇒ Object

Get request

Parameters:

  • path (String)
  • response_block (Lambda)
  • params (Hash, NilClass) (defaults to: nil)
  • headers (Hash) (defaults to: {})


51
52
53
54
55
56
57
58
59
# File 'lib/central/client.rb', line 51

def get_stream(path, response_block, params = nil, headers = {})
  http_client.get(
    read_timeout: 360,
    path: request_uri(path),
    query: params,
    headers: request_headers(headers),
    response_block: response_block
  )
end

#post(path, obj, params = {}, headers = {}) ⇒ Hash

Post request

Parameters:

  • path (String)
  • obj (Object)
  • params (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/central/client.rb', line 68

def post(path, obj, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
    path: request_uri(path),
    headers: request_headers,
    body: encode_body(obj, request_headers['Content-Type']),
    query: params
  }

  response = http_client.post(request_options)
  if [200, 201].include?(response.status)
    parse_response(response)
  else
    handle_error_response(response)
  end
end

#put(path, obj, params = {}, headers = {}) ⇒ Hash

Put request

Parameters:

  • path (String)
  • obj (Object)
  • params (Hash) (defaults to: {})
  • headers (Hash) (defaults to: {})

Returns:

  • (Hash)


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/central/client.rb', line 92

def put(path, obj, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
    path: request_uri(path),
    headers: request_headers,
    body: encode_body(obj, request_headers['Content-Type']),
    query: params
  }

  response = http_client.put(request_options)
  if [200, 201].include?(response.status)
    parse_response(response)
  else
    handle_error_response(response)
  end
end