Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Merge pull request #29 from swiftype/jasonstoltz/proxy-support
Browse files Browse the repository at this point in the history
Add proxy support to client
  • Loading branch information
JasonStoltz authored Oct 17, 2018
2 parents 7a11b0b + 5fe0c21 commit d284523
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 4 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -185,7 +191,7 @@ Update multiple Documents at once:
])

All methods above will have a return in the following format:

[
{
"id": "5473d6142ed96065a9000001",
Expand Down Expand Up @@ -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.

5 changes: 5 additions & 0 deletions lib/swiftype/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lib/swiftype/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/swiftype/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Swiftype
VERSION = "1.2.3"
VERSION = "1.3.0"
end
17 changes: 17 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit d284523

Please sign in to comment.