Module: Octo::Helpers::KongHelper
- Included in:
- Authorization, KongBridge
- Defined in:
- lib/octocore-mongo/helpers/kong_helper.rb
Constant Summary collapse
- KONG_URL =
The default URL for kong to connect to
'http://127.0.0.1:8001'
Instance Method Summary collapse
-
#add_ratelimiting_plugin(apikey, consumer_id, config) ⇒ String
Add a Kong ratelimiting plugin.
-
#apislist(payload = {}) ⇒ Hash
List of APIS.
-
#consumerlist(payload = {}) ⇒ Hash
List of Client.
-
#create_keyauth(username, keyauth_key) ⇒ Hash
Add Key of client for Key Authorization.
-
#delete_api(name) ⇒ String
Delete apis from Kong.
-
#delete_consumer(username) ⇒ String
Delete Consumers from Kong.
-
#kong_url ⇒ String
Fetch Kong URL.
-
#process_kong_request(url, method, payload) ⇒ Hash
Process Every Kong Request.
Instance Method Details
#add_ratelimiting_plugin(apikey, consumer_id, config) ⇒ String
Add a Kong ratelimiting plugin
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 87 def add_ratelimiting_plugin(apikey, consumer_id, config) url = '/apis/' + apikey + '/plugins/' payload = { name: "rate-limiting", consumer_id: consumer_id, config: config } response = process_kong_request(url, :POST, payload) if response['id'] response['id'].to_s end end |
#apislist(payload = {}) ⇒ Hash
List of APIS
124 125 126 127 128 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 124 def apislist(payload = {}) url = '/apis/' res = process_kong_request(url, :GET, payload) res['data'] end |
#consumerlist(payload = {}) ⇒ Hash
List of Client
106 107 108 109 110 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 106 def consumerlist(payload = {}) url = '/consumers/' res = process_kong_request(url, :GET, payload) res['data'] end |
#create_keyauth(username, keyauth_key) ⇒ Hash
Add Key of client for Key Authorization
73 74 75 76 77 78 79 80 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 73 def create_keyauth(username, keyauth_key) url = '/consumers/'+ username +'/key-auth' payload = { key: keyauth_key } process_kong_request(url, :POST, payload) end |
#delete_api(name) ⇒ String
Delete apis from Kong
133 134 135 136 137 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 133 def delete_api(name) url = '/apis/' + name payload = {} process_kong_request(url, :DELETE, payload) end |
#delete_consumer(username) ⇒ String
Delete Consumers from Kong
115 116 117 118 119 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 115 def delete_consumer(username) url = '/consumers/' + username payload = {} process_kong_request(url, :DELETE, payload) end |
#kong_url ⇒ String
Fetch Kong URL
16 17 18 19 20 21 22 23 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 16 def kong_url kong_config = Octo.get_config :kong if kong_config.class == String kong_config elsif kong_config.class == Hash kong_config.fetch :url, KONG_URL end end |
#process_kong_request(url, method, payload) ⇒ Hash
Process Every Kong Request
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/octocore-mongo/helpers/kong_helper.rb', line 30 def process_kong_request (url, method, payload) begin url = kong_url + url header = { 'Accept' => 'application/json, text/plain, */*', 'Content-Type' => 'application/json' } uri = URI.parse(URI.encode(url.strip)) http = Net::HTTP.new(uri.host,uri.port) case method when :GET req = Net::HTTP::Get.new(uri, header) # GET Method when :POST req = Net::HTTP::Post.new(uri.path, header) # POST Method when :PUT req = Net::HTTP::Put.new(uri.path, header) # PUT Method when :PATCH req = Net::HTTP::Patch.new(uri.path, header) # PATCH Method when :DELETE req = Net::HTTP::Delete.new(uri.path, header) # DELETE Method else # Default Case end puts "HTTP #{ method } to #{ uri.host}:#{ uri.port }. Body: #{ payload.to_json }" body = "#{ payload.to_json }" res = http.request(req, body) if res.body JSON.parse(res.body) # Returned Data else {} end rescue Exception => e puts "Exception: " + e..to_s + "\n" + e.backtrace_locations.join("\n") + "}" { message: e.to_s }.to_json end end |