Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move http client creation into Client object
This will in future allow the http client to be reused between requests, but this needs a bit more work
- Loading branch information
Showing
2 changed files
with
65 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,10 @@ def encrypted=(boolean) | |
@port = boolean ? 443 : 80 | ||
end | ||
|
||
def encrypted? | ||
@scheme == 'https' | ||
end | ||
|
||
## INTERACE WITH THE API ## | ||
|
||
def resource(path) | ||
|
@@ -167,6 +171,57 @@ def trigger_async(channels, event_name, data, options = {}) | |
post_async('/events', trigger_params(channels, event_name, data, options)) | ||
end | ||
|
||
# @private Construct a net/http http client | ||
def net_http_client | ||
@_http_sync ||= begin | ||
if encrypted? | ||
require 'net/https' unless defined?(Net::HTTPS) | ||
else | ||
require 'net/http' unless defined?(Net::HTTP) | ||
end | ||
|
||
http_klass = if (p = @proxy) | ||
Net::HTTP.Proxy(p[:host], p[:port], p[:user], p[:password]) | ||
else | ||
Net::HTTP | ||
end | ||
|
||
http = http_klass.new(@host, @port) | ||
|
||
if encrypted? | ||
http.use_ssl = true | ||
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mloughran
Author
Contributor
|
||
end | ||
|
||
http | ||
end | ||
end | ||
|
||
# @private Construct an em-http-request http client | ||
def em_http_client(uri) | ||
begin | ||
unless defined?(EventMachine) && EventMachine.reactor_running? | ||
raise Error, "In order to use async calling you must be running inside an eventmachine loop" | ||
end | ||
require 'em-http' unless defined?(EventMachine::HttpRequest) | ||
|
||
connection_opts = {} | ||
|
||
if @proxy | ||
proxy_opts = { | ||
:host => @proxy[:host], | ||
:port => @proxy[:port] | ||
} | ||
if @proxy[:user] | ||
proxy_opts[:authorization] = [@proxy[:user], @proxy[:password]] | ||
end | ||
connection_opts[:proxy] = proxy_opts | ||
end | ||
|
||
EventMachine::HttpRequest.new(uri, connection_opts) | ||
end | ||
end | ||
|
||
private | ||
|
||
def trigger_params(channels, event_name, data, params) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Is this intentional?