Skip to content

Commit

Permalink
Avoid Wrong encoding for data
Browse files Browse the repository at this point in the history
BUGFIX: 2.1.2 did introduce another issue: data was not properly encoded
when sending the request to Consul agent.

This will fix #189
  • Loading branch information
pierresouchay committed Mar 20, 2019
1 parent 5f57ca9 commit 9aec4f0
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ Naming/UncommunicativeMethodParamName:

Lint/UnneededCopDisableDirective:
Enabled: false

Metrics/PerceivedComplexity:
Max: 10

Metrics/ParameterLists:
Max: 6
4 changes: 2 additions & 2 deletions lib/diplomat/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def update(value, options = {})
raise Diplomat::IdParameterRequired unless value['ID'] || value[:ID]

custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/update'], options, value.to_json, custom_params)
@raw = send_put_request(@conn, ['/v1/acl/update'], options, value, custom_params)
parse_body
end

Expand All @@ -64,7 +64,7 @@ def update(value, options = {})
# @return [Hash] The result Acl
def create(value, options = {})
custom_params = use_cas(@options)
@raw = send_put_request(@conn, ['/v1/acl/create'], options, value.to_json, custom_params)
@raw = send_put_request(@conn, ['/v1/acl/create'], options, value, custom_params)
parse_body
end

Expand Down
5 changes: 3 additions & 2 deletions lib/diplomat/kv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ def put(key, value, options = {})
custom_params << use_cas(@options)
custom_params << dc(@options)
custom_params << acquire(@options)
@raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, value, custom_params)
@raw = send_put_request(@conn, ["/v1/kv/#{key}"], options, value, custom_params,
'application/x-www-form-urlencoded')
if @raw.body.chomp == 'true'
@key = key
@value = value
Expand Down Expand Up @@ -167,7 +168,7 @@ def txn(value, options = {})
custom_params = []
custom_params << dc(options)
custom_params << transaction_consistency(options)
raw = send_put_request(@conn_no_err, ['/v1/txn'], options, JSON.generate(value), custom_params)
raw = send_put_request(@conn_no_err, ['/v1/txn'], options, value, custom_params)
transaction_return JSON.parse(raw.body), options
end

Expand Down
4 changes: 2 additions & 2 deletions lib/diplomat/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_all(options = {})
# @param options [Hash] options parameter hash
# @return [Boolean]
def register(definition, options = {})
register = send_put_request(@conn, ['/v1/catalog/register'], options, JSON.dump(definition))
register = send_put_request(@conn, ['/v1/catalog/register'], options, definition)
register.status == 200
end

Expand All @@ -36,7 +36,7 @@ def register(definition, options = {})
# @param options [Hash] options parameter hash
# @return [Boolean]
def deregister(definition, options = {})
deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, JSON.dump(definition))
deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, definition)
deregister.status == 200
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/diplomat/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def delete(key, options = {})
# @return [Boolean]
def update(key, definition, options = {})
custom_params = options[:dc] ? use_named_parameter('dc', options[:dc]) : nil
ret = send_put_request(@conn, ["/v1/query/#{key}"], options, JSON.dump(definition), custom_params)
ret = send_put_request(@conn, ["/v1/query/#{key}"], options, definition, custom_params)
ret.status == 200
end

Expand Down
11 changes: 9 additions & 2 deletions lib/diplomat/rest_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,21 @@ def send_get_request(connection, url, options, custom_params = nil)
end
end

def send_put_request(connection, url, options, data, custom_params = nil)
def send_put_request(connection, url, options, data, custom_params = nil, mime = 'application/json')
rest_options = parse_options(options)
url += rest_options[:query_params]
url += custom_params unless custom_params.nil?
connection.put do |req|
req.url rest_options[:url_prefix] ? rest_options[:url_prefix] + concat_url(url) : concat_url(url)
rest_options[:headers].map { |k, v| req.headers[k.to_sym] = v } unless rest_options[:headers].nil?
req.body = data unless data.nil?
unless data.nil?
(req.headers || {})['Content-Type'] = mime
req.body = if mime == 'application/json' && !data.is_a?(String)
data.to_json
else
data
end
end
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/diplomat/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_all(options = {})
# @return [Boolean]
def register(definition, options = {})
url = options[:path] || ['/v1/agent/service/register']
register = send_put_request(@conn, url, options, JSON.dump(definition))
register = send_put_request(@conn, url, options, definition)
register.status == 200
end

Expand All @@ -74,7 +74,7 @@ def register_external(definition, options = {})
# @param options [Hash] options parameter hash
# @return [Boolean]
def deregister_external(definition, options = {})
deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, JSON.dump(definition))
deregister = send_put_request(@conn, ['/v1/catalog/deregister'], options, definition)
deregister.status == 200
end

Expand Down

0 comments on commit 9aec4f0

Please sign in to comment.