Class: Kong::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/kong/client.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Initialize api client



18
19
20
21
22
23
# File 'lib/kong/client.rb', line 18

def initialize
  Excon.defaults[:ssl_verify_peer] = false if ignore_ssl_errors?
  @api_url = api_url
  self.class.http_client = Excon.new(@api_url, omit_default_port: true)
  @default_headers = { 'Accept' => 'application/json' }
end

Class Attribute Details

.http_clientObject

Returns the value of attribute http_client.



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

def http_client
  @http_client
end

Instance Attribute Details

#default_headersObject

Returns the value of attribute default_headers.



14
15
16
# File 'lib/kong/client.rb', line 14

def default_headers
  @default_headers
end

Class Method Details

.api_urlObject



25
26
27
# File 'lib/kong/client.rb', line 25

def self.api_url
  @api_url || ENV['KONG_URI'] || 'http://localhost:8001'
end

.api_url=(url) ⇒ Object



29
30
31
32
# File 'lib/kong/client.rb', line 29

def self.api_url=(url)
  @api_url = url
  @http_client = Excon.new(self.api_url, omit_default_port: true)
end

Instance Method Details

#api_urlString

Kong Admin API URL

Returns:

  • (String)


41
42
43
# File 'lib/kong/client.rb', line 41

def api_url
  self.class.api_url
end

#api_url=(url) ⇒ Object



45
46
47
# File 'lib/kong/client.rb', line 45

def api_url=(url)
  @api_url = url
end

#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)


146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kong/client.rb', line 146

def delete(path, body = nil, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
      path: path,
      headers: request_headers,
      body: encode_body(body, request_headers['Content-Type']),
      query: encode_params(params)
  }
  response = http_client.delete(request_options)
  unless response.status == 204
    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)


55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/kong/client.rb', line 55

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

#http_clientObject



34
35
36
# File 'lib/kong/client.rb', line 34

def http_client
  self.class.http_client
end

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

Patch request

Parameters:

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

Returns:

  • (Hash)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kong/client.rb', line 98

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

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

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

Post request

Parameters:

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

Returns:

  • (Hash)


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/kong/client.rb', line 75

def post(path, obj, params = {}, headers = {})
  request_headers = request_headers(headers)
  request_options = {
      path: path,
      headers: request_headers,
      body: encode_body(obj, request_headers['Content-Type']),
      query: encode_params(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)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/kong/client.rb', line 122

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

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