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

Commit d284523

Browse files
authored
Merge pull request #29 from swiftype/jasonstoltz/proxy-support
Add proxy support to client
2 parents 7a11b0b + 5fe0c21 commit d284523

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ You can also provide the API key when creating the client instance:
5252

5353
If the API key is provided as an option to constructor, it will override the globally configured Swiftype API key (if any).
5454

55+
### Specifying an HTTP Proxy
56+
57+
client = Swiftype::Client.new(:api_key => 'api_key', :proxy => 'http://localhost:8888')
58+
59+
This client will also support configuring a proxy via the environment variable `http_proxy`.
60+
5561
### Full-text search
5662

5763
If you want to search for `cat` on your engine, you can use:
@@ -185,7 +191,7 @@ Update multiple Documents at once:
185191
])
186192

187193
All methods above will have a return in the following format:
188-
194+
189195
[
190196
{
191197
"id": "5473d6142ed96065a9000001",
@@ -410,4 +416,3 @@ or simply `Swiftype.api_key = 'your_api_key'`.
410416
You can run tests with `rspec`. All HTTP interactions are stubbed out using VCR.
411417

412418
To contribute code to this gem, please fork the repository and submit a pull request.
413-

lib/swiftype/client.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def self.configure(&block)
2121
# @option options [String] :platform_access_token a user's access token, will be used instead of API key for authenticating requests
2222
# @option options [Numeric] :overall_timeout overall timeout for requests in seconds (default: 15s)
2323
# @option options [Numeric] :open_timeout the number of seconds Net::HTTP (default: 15s)
24+
# @option options [String] :proxy url of proxy to use, ex: "http://localhost:8888"
2425
# will wait while opening a connection before raising a Timeout::Error
2526

2627
def initialize(options={})
@@ -35,6 +36,10 @@ def platform_access_token
3536
@options[:platform_access_token]
3637
end
3738

39+
def proxy
40+
@options[:proxy]
41+
end
42+
3843
def open_timeout
3944
@options[:open_timeout] || DEFAULT_TIMEOUT
4045
end

lib/swiftype/request.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,14 @@ def request(method, path, params={})
5757
uri = URI.parse("#{Swiftype.endpoint}#{path}")
5858

5959
request = build_request(method, uri, params)
60-
http = Net::HTTP.new(uri.host, uri.port)
60+
61+
if proxy
62+
proxy_parts = URI.parse(proxy)
63+
http = Net::HTTP.new(uri.host, uri.port, proxy_parts.host, proxy_parts.port)
64+
else
65+
http = Net::HTTP.new(uri.host, uri.port)
66+
end
67+
6168
http.open_timeout = open_timeout
6269
http.read_timeout = overall_timeout
6370

lib/swiftype/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Swiftype
2-
VERSION = "1.2.3"
2+
VERSION = "1.3.0"
33
end

spec/client_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,23 @@
101101
end
102102
end
103103
end
104+
105+
context 'with proxy specified' do
106+
let(:options) { { :proxy => 'http://localhost:8888' } }
107+
108+
it 'will set proxy' do
109+
expect(options_client.proxy).to eq('http://localhost:8888')
110+
end
111+
112+
# There doesn't seem to be an elgant way to test that a request actually uses a proxy, so the best
113+
# we can do here is ensure that the behavior for methods operates normally
114+
it 'will execute methods with proxy' do
115+
VCR.use_cassette(:engine_search) do
116+
results = options_client.search(engine_slug, 'cat')
117+
expect(results.document_types.size).to eq(2)
118+
end
119+
end
120+
end
104121
end
105122
end
106123

0 commit comments

Comments
 (0)