-
Notifications
You must be signed in to change notification settings - Fork 19
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
fix(influxdb2/client): Send Content-Type headers #30
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,8 @@ class DefaultApi | |
DEFAULT_TIMEOUT = 10 | ||
DEFAULT_REDIRECT_COUNT = 10 | ||
|
||
HEADER_CONTENT_TYPE = 'Content-Type'.freeze | ||
|
||
# @param [Hash] options The options to be used by the client. | ||
def initialize(options:) | ||
@options = options | ||
|
@@ -32,7 +34,17 @@ def initialize(options:) | |
|
||
private | ||
|
||
def _post(payload, uri, limit = @max_redirect_count) | ||
def _post_json(payload, uri, headers: {}) | ||
_check_arg_type(:headers, headers, Hash) | ||
_post(payload, uri, headers: headers.merge({HEADER_CONTENT_TYPE => 'application/json'})) | ||
end | ||
|
||
def _post_text(payload, uri, headers: {}) | ||
_check_arg_type(:headers, headers, Hash) | ||
_post(payload, uri, headers: headers.merge({HEADER_CONTENT_TYPE => 'text/plain'})) | ||
end | ||
|
||
def _post(payload, uri, limit: @max_redirect_count, headers: {}) | ||
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. what was your rationale behind creating two separate methods, (i guess, on second thought, not introducing another argument dependency in favor of encapsulating the content-type-setting logic in this class is probably more robust but still curious to hear your thoughts) 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. The underlying |
||
raise InfluxError.from_message("Too many HTTP redirects. Exceeded limit: #{@max_redirect_count}") if limit.zero? | ||
|
||
http = Net::HTTP.new(uri.host, uri.port) | ||
|
@@ -44,6 +56,8 @@ def _post(payload, uri, limit = @max_redirect_count) | |
request = Net::HTTP::Post.new(uri.request_uri) | ||
request['Authorization'] = "Token #{@options[:token]}" | ||
request['User-Agent'] = "influxdb-client-ruby/#{InfluxDB2::VERSION}" | ||
headers.each { |k,v| request[k] = v } | ||
|
||
request.body = payload | ||
|
||
begin | ||
|
@@ -53,7 +67,7 @@ def _post(payload, uri, limit = @max_redirect_count) | |
response | ||
when Net::HTTPRedirection then | ||
location = response['location'] | ||
_post(payload, URI.parse(location), limit - 1) | ||
_post(payload, URI.parse(location), limit: limit - 1, headers: headers) | ||
else | ||
raise InfluxError.from_response(response) | ||
end | ||
|
@@ -62,6 +76,10 @@ def _post(payload, uri, limit = @max_redirect_count) | |
end | ||
end | ||
|
||
def _check_arg_type(name, value, klass) | ||
raise TypeError, "expected a #{klass.name} for #{name}; got #{value.class.name}" unless value.is_a?(klass) | ||
end | ||
|
||
def _check(key, value) | ||
raise ArgumentError, "The '#{key}' should be defined as argument or default option: #{@options}" if value.nil? | ||
end | ||
|
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.
nice 😎