Module: Octo::Helpers::KongHelper
- Included in:
- Authorization, KongBridge
- Defined in:
- lib/octocore/helpers/kong_helper.rb
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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/octocore/helpers/kong_helper.rb', line 79 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
116 117 118 119 120 |
# File 'lib/octocore/helpers/kong_helper.rb', line 116 def apislist(payload = {}) url = '/apis/' res = process_kong_request(url, :GET, payload) res['data'] end |
#consumerlist(payload = {}) ⇒ Hash
List of Client
98 99 100 101 102 |
# File 'lib/octocore/helpers/kong_helper.rb', line 98 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
65 66 67 68 69 70 71 72 |
# File 'lib/octocore/helpers/kong_helper.rb', line 65 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
125 126 127 128 129 |
# File 'lib/octocore/helpers/kong_helper.rb', line 125 def delete_api(name) url = '/apis/' + name payload = {} process_kong_request(url, :DELETE, payload) end |
#delete_consumer(username) ⇒ String
Delete Consumers from Kong
107 108 109 110 111 |
# File 'lib/octocore/helpers/kong_helper.rb', line 107 def delete_consumer(username) url = '/consumers/' + username payload = {} process_kong_request(url, :DELETE, payload) end |
#kong_url ⇒ String
Fetch Kong URL
13 14 15 16 17 18 19 20 |
# File 'lib/octocore/helpers/kong_helper.rb', line 13 def kong_url kong_config = Octo.get_config :kong if kong_config.has_key?(:url) kong_config[:url] else 'http://127.0.0.1:8001' end end |
#process_kong_request(url, method, payload) ⇒ Hash
Process Every Kong Request
27 28 29 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 |
# File 'lib/octocore/helpers/kong_helper.rb', line 27 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(url) 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 req.body = "#{ payload.to_json }" res = http.request(req) JSON.parse(res.body) # Returned Data rescue Exception => e { message: e.to_s }.to_json end end |