From 6312cceedd927fe7a3ea9ec57d11f1e309838964 Mon Sep 17 00:00:00 2001 From: Jason Stoltzfus Date: Mon, 15 Oct 2018 13:37:47 -0400 Subject: [PATCH 1/2] Add proxy support to client --- README.md | 9 +++++++-- lib/swiftype/client.rb | 5 +++++ lib/swiftype/request.rb | 9 ++++++++- spec/client_spec.rb | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index bbd2e5b..ab9009b 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,12 @@ You can also provide the API key when creating the client instance: If the API key is provided as an option to constructor, it will override the globally configured Swiftype API key (if any). +### Specifying an HTTP Proxy + + client = Swiftype::Client.new(:api_key => 'api_key', :proxy => 'http://localhost:8888') + +This client will also support configuring a proxy via the environment variable `http_proxy`. + ### Full-text search If you want to search for `cat` on your engine, you can use: @@ -185,7 +191,7 @@ Update multiple Documents at once: ]) All methods above will have a return in the following format: - + [ { "id": "5473d6142ed96065a9000001", @@ -410,4 +416,3 @@ or simply `Swiftype.api_key = 'your_api_key'`. You can run tests with `rspec`. All HTTP interactions are stubbed out using VCR. To contribute code to this gem, please fork the repository and submit a pull request. - diff --git a/lib/swiftype/client.rb b/lib/swiftype/client.rb index 2e095b5..6881ac0 100644 --- a/lib/swiftype/client.rb +++ b/lib/swiftype/client.rb @@ -21,6 +21,7 @@ def self.configure(&block) # @option options [String] :platform_access_token a user's access token, will be used instead of API key for authenticating requests # @option options [Numeric] :overall_timeout overall timeout for requests in seconds (default: 15s) # @option options [Numeric] :open_timeout the number of seconds Net::HTTP (default: 15s) + # @option options [String] :proxy url of proxy to use, ex: "http://localhost:8888" # will wait while opening a connection before raising a Timeout::Error def initialize(options={}) @@ -35,6 +36,10 @@ def platform_access_token @options[:platform_access_token] end + def proxy + @options[:proxy] + end + def open_timeout @options[:open_timeout] || DEFAULT_TIMEOUT end diff --git a/lib/swiftype/request.rb b/lib/swiftype/request.rb index f1965f3..60c498d 100644 --- a/lib/swiftype/request.rb +++ b/lib/swiftype/request.rb @@ -57,7 +57,14 @@ def request(method, path, params={}) uri = URI.parse("#{Swiftype.endpoint}#{path}") request = build_request(method, uri, params) - http = Net::HTTP.new(uri.host, uri.port) + + if proxy + proxy_parts = URI.parse(proxy) + http = Net::HTTP.new(uri.host, uri.port, proxy_parts.host, proxy_parts.port) + else + http = Net::HTTP.new(uri.host, uri.port) + end + http.open_timeout = open_timeout http.read_timeout = overall_timeout diff --git a/spec/client_spec.rb b/spec/client_spec.rb index e797e27..52b6261 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -101,6 +101,23 @@ end end end + + context 'with proxy specified' do + let(:options) { { :proxy => 'http://localhost:8888' } } + + it 'will set proxy' do + expect(options_client.proxy).to eq('http://localhost:8888') + end + + # There doesn't seem to be an elgant way to test that a request actually uses a proxy, so the best + # we can do here is ensure that the behavior for methods operates normally + it 'will execute methods with proxy' do + VCR.use_cassette(:engine_search) do + results = options_client.search(engine_slug, 'cat') + expect(results.document_types.size).to eq(2) + end + end + end end end From 5fe0c21b17660d0c158205b23d95d1f9ee58ea8f Mon Sep 17 00:00:00 2001 From: Jason Stoltzfus Date: Mon, 15 Oct 2018 14:09:31 -0400 Subject: [PATCH 2/2] Release 1.3.0 --- lib/swiftype/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/swiftype/version.rb b/lib/swiftype/version.rb index e4432d1..02d3409 100644 --- a/lib/swiftype/version.rb +++ b/lib/swiftype/version.rb @@ -1,3 +1,3 @@ module Swiftype - VERSION = "1.2.3" + VERSION = "1.3.0" end