Class: Kong::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Initialize api client



15
16
17
18
19
20
# File 'lib/kong/client.rb', line 15

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

Instance Attribute Details

#default_headersObject

Returns the value of attribute default_headers.



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

def default_headers
  @default_headers
end

#http_clientObject (readonly)

Returns the value of attribute http_client.



11
12
13
# File 'lib/kong/client.rb', line 11

def http_client
  @http_client
end

Class Method Details

.api_urlObject



22
23
24
# File 'lib/kong/client.rb', line 22

def self.api_url
  self.instance.api_url
end

.api_url=(url) ⇒ Object



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

def self.api_url=(url)
  self.instance.api_url = url
end

Instance Method Details

#api_urlString

Kong Admin API URL

Returns:

  • (String)


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

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

#api_url=(url) ⇒ Object



37
38
39
# File 'lib/kong/client.rb', line 37

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)


140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kong/client.rb', line 140

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)


48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kong/client.rb', line 48

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

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

Put request

Parameters:

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

Returns:

  • (Hash)


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

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)


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

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)


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

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