Skip to content

feat: Add option to set http attributes when creating an instance of Client #20

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

Merged
merged 3 commits into from
Dec 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
host = 'https://api.sendgrid.com'
client = SendGrid::Client.new(host: host, request_headers: headers)

# You can pass in an http_options hash to set values for NET::HTTP attributes
# https://ruby-doc.org/stdlib-2.4.1/libdoc/net/http/rdoc/Net/HTTP.html
# client = SendGrid::Client.new(host: host,
# request_headers: headers,
# http_options: {open_timeout: 15, read_timeout: 30})

# GET Collection
query_params = { 'limit' => 100, 'offset' => 0 }
response = client.version('v3').api_keys.get(query_params: query_params)
Expand Down
9 changes: 7 additions & 2 deletions lib/ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,15 @@ class Client
# (e.g. client._("/v3"))
# - +url_path+ -> A list of the url path segments
#
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil)
def initialize(host: nil, request_headers: nil, version: nil, url_path: nil, http_options: {})
@host = host
@request_headers = request_headers || {}
@version = version
@url_path = url_path || []
@methods = %w[delete get patch post put]
@query_params = nil
@request_body = nil
@http_options = http_options
end

# Update the headers for the request
Expand Down Expand Up @@ -152,6 +153,9 @@ def build_request(name, args)
else
@request.body = @request_body
end
@http_options.each do |attribute, value|
@http.send("#{attribute}=", value)
end
make_request(@http, @request)
end

Expand Down Expand Up @@ -198,7 +202,8 @@ def _(name = nil)
url_path = name ? @url_path.push(name) : @url_path
@url_path = []
Client.new(host: @host, request_headers: @request_headers,
version: @version, url_path: url_path)
version: @version, url_path: url_path,
http_options: @http_options)
end

# Dynamically add segments to the url, then call a method.
Expand Down
13 changes: 12 additions & 1 deletion test/test_ruby_http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ def setup
')
@host = 'http://localhost:4010'
@version = 'v3'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jrectenwald Would you be OK with adding a duplicate test to this one, that doesn't pass http_options, so we can prove in the tests that it works both ways?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mbernier How's that?

@http_options = {open_timeout: 60, read_timeout: 60}
@client = MockRequest.new(host: @host,
request_headers: @headers,
version: @version)
@client_with_options = MockRequest.new(host: @host,
request_headers: @headers,
version: @version,
http_options: @http_options)
end

def test_init
Expand Down Expand Up @@ -173,6 +178,13 @@ def test_method_missing
assert_equal({'headers' => 'test'}, response.headers)
end

def test_http_options
url1 = @client_with_options._('test')
assert_equal(@host, @client_with_options.host)
assert_equal(@headers, @client_with_options.request_headers)
assert_equal(['test'], url1.url_path)
end

def test_docker_exists
assert(File.file?('./Dockerfile') || File.file?('./docker/Dockerfile'))
end
Expand Down Expand Up @@ -242,5 +254,4 @@ def test_license_date_is_updated
current_year = Time.new.year
assert_equal(current_year, license_end_year)
end

end