-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Allow to operate against multiple Kong API servers #26
Open
nevalla
wants to merge
1
commit into
master
Choose a base branch
from
feature/refactor_kong_client
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,36 +4,46 @@ module ClassMethods | |
|
||
# List resources | ||
# @return [Array] | ||
def list(params = {}) | ||
def list(params = {}, opts = {}) | ||
opts = {} if opts.nil? | ||
client = opts[:client] ? opts[:client] : Client.instance | ||
collection = opts[:collection] ? opts[:collection] : Collection.new(self, client) | ||
api_end_point = opts[:api_end_point] ? opts[:api_end_point] : self::API_END_POINT | ||
result = [] | ||
json_data = Client.instance.get(self::API_END_POINT, params) | ||
json_data = client.get(api_end_point, params) | ||
if json_data['data'] | ||
json_data['data'].each do |instance| | ||
result << self.new(instance) | ||
result << self.new(instance, { client: client, collection: collection }) | ||
end | ||
end | ||
result | ||
end | ||
|
||
alias_method :all, :list | ||
def all(params = {}, opts = {}) | ||
self.list({ size: 9999999 }, opts) | ||
end | ||
|
||
def first(opts={}) | ||
self.list({},opts).first | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Space missing after comma. |
||
end | ||
|
||
# Create resource | ||
# @param [Hash] attributes | ||
def create(attributes = {}) | ||
self.new(attributes).create | ||
def create(attributes = {}, opts = {}) | ||
self.new(attributes, opts).create | ||
end | ||
|
||
# Find resource | ||
# @param [String] id | ||
def find(id) | ||
self.new.get(id) | ||
def find(id, opts = {}) | ||
self.new({}, opts).get(id) | ||
end | ||
|
||
def method_missing(method, *arguments, &block) | ||
if method.to_s.start_with?('find_by_') | ||
attribute = method.to_s.sub('find_by_', '') | ||
if self.attribute_names.include?(attribute) | ||
self.list({ attribute => arguments[0] })[0] | ||
self.list({ attribute => arguments[0] }, arguments[1])[0] | ||
else | ||
super | ||
end | ||
|
@@ -57,6 +67,7 @@ def respond_to?(method, include_private = false) | |
end | ||
|
||
attr_accessor :attributes, :api_end_point | ||
attr_reader :client, :collection | ||
|
||
def self.included(base) | ||
base.extend(ClassMethods) | ||
|
@@ -72,18 +83,13 @@ def self.included(base) | |
|
||
## | ||
# @param [Hash] attributes | ||
def initialize(attributes = {}) | ||
def initialize(attributes = {}, opts = {}) | ||
@client = opts[:client] ? opts[:client] : Client.instance | ||
@collection = opts[:collection] ? opts[:collection] : Collection.new(self.class, @client) | ||
init_api_end_point | ||
init_attributes(attributes) | ||
end | ||
|
||
# Get Kong API client | ||
# @return [Kong::Client] | ||
def client | ||
Client.instance | ||
end | ||
|
||
|
||
# Get resource | ||
# @param [String] key | ||
def get(key = nil) | ||
|
@@ -104,13 +110,30 @@ def new? | |
self.id.nil? | ||
end | ||
|
||
def set(attributes) | ||
self.attributes = attributes | ||
self.attributes | ||
end | ||
|
||
def to_s | ||
self.attributes.to_s | ||
end | ||
|
||
def to_json | ||
self.attributes.to_json | ||
end | ||
|
||
def inspect | ||
self.attributes.inspect | ||
end | ||
# Save resource to Kong | ||
def save | ||
create_or_update | ||
end | ||
|
||
# Create resource | ||
def create | ||
def create(attributes = {}) | ||
attributes = self.attributes.merge(attributes) | ||
headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } | ||
response = client.post(@api_end_point, nil, attributes, headers) | ||
init_attributes(response) | ||
|
@@ -156,8 +179,12 @@ def respond_to?(method, include_private = false) | |
|
||
def init_attributes(attributes) | ||
@attributes = {} | ||
self.attributes = attributes | ||
end | ||
|
||
def attributes=(attributes) | ||
attributes.each do |key, value| | ||
@attributes[key.to_s] = value | ||
@attributes[key.to_s] = value if self.class.attribute_names.include?(key.to_s) | ||
end | ||
use_consumer_end_point if respond_to?(:use_consumer_end_point) | ||
use_api_end_point if respond_to?(:use_api_end_point) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Kong | ||
class Collection | ||
def initialize(klass, client, api_end_point = nil) | ||
@klass = klass | ||
@client = client | ||
@api_end_point = api_end_point | ||
end | ||
|
||
def first | ||
self.list.first | ||
end | ||
|
||
def last | ||
self.all.last | ||
end | ||
|
||
def method_missing(method, *arguments, &block) | ||
opts = { client: @client, collection: self, api_end_point: @api_end_point } | ||
if arguments.size == 0 | ||
arguments = [{}, opts] | ||
else | ||
arguments << opts | ||
end | ||
@klass.send(method, *arguments) | ||
end | ||
|
||
def respond_to?(method, include_private = false) | ||
@klass.respond_to?(method, include_private) | ||
end | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Final newline missing. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Surrounding space missing in default value assignment.