Skip to content

Commit

Permalink
Move http client creation into Client object
Browse files Browse the repository at this point in the history
This will in future allow the http client to be reused between requests, but this needs a bit more work
  • Loading branch information
mloughran committed Nov 12, 2012
1 parent 76284d3 commit 3425798
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 41 deletions.
55 changes: 55 additions & 0 deletions lib/pusher/client.rb
Expand Up @@ -74,6 +74,10 @@ def encrypted=(boolean)
@port = boolean ? 443 : 80
end

def encrypted?
@scheme == 'https'
end

## INTERACE WITH THE API ##

def resource(path)
Expand Down Expand Up @@ -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.

Copy link
@markburns

markburns Feb 7, 2013

Contributor

Is this intentional?

This comment has been minimized.

Copy link
@mloughran

mloughran Feb 7, 2013

Author Contributor

It didn't change the previous behaviour, so this commit is correct. But you're right - we should really verify.

This comment has been minimized.

Copy link
@markburns

markburns Feb 7, 2013

Contributor

Will you fix that? Do you want a pull request?

This comment has been minimized.

Copy link
@mloughran

mloughran Feb 7, 2013

Author Contributor

Have added to my todo list :)

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)
Expand Down
51 changes: 10 additions & 41 deletions lib/pusher/request.rb
Expand Up @@ -9,8 +9,7 @@ class Request
attr_reader :body, :params

def initialize(client, verb, uri, params, body = nil)
@verb = verb
@uri = uri
@client, @verb, @uri = client, verb, uri

if body
@body = body
Expand All @@ -20,31 +19,19 @@ def initialize(client, verb, uri, params, body = nil)
request = Signature::Request.new(verb.to_s.upcase, uri.path, params)
request.sign(client.authentication_token)
@params = request.signed_params
@proxy = client.proxy
end

def send_sync
if ssl?
require 'net/https' unless defined?(Net::HTTPS)
else
require 'net/http' unless defined?(Net::HTTP)
end

@http_sync ||= begin
http = (@proxy.nil? ? Net::HTTP : Net::HTTP.Proxy(@proxy[:host], @proxy[:port], @proxy[:user], @proxy[:password])).new(@uri.host, @uri.port)
http.use_ssl = true if ssl?
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if ssl?
http
end
http = @client.net_http_client

begin
case @verb
when :post
response = @http_sync.post(encode_query(@uri, @params), @body, {
response = http.post(encode_query(@uri, @params), @body, {
'Content-Type'=> 'application/json'
})
when :get
response = @http_sync.get(encode_query(@uri, @params), {
response = http.get(encode_query(@uri, @params), {
'Content-Type'=> 'application/json'
})
else
Expand All @@ -66,40 +53,26 @@ def send_sync
end

def send_async
unless defined?(EventMachine) && EventMachine.reactor_running?
raise Error, "In order to use trigger_async you must be running inside an eventmachine loop"
end
require 'em-http' unless defined?(EventMachine::HttpRequest)
df = EM::DefaultDeferrable.new

deferrable = EM::DefaultDeferrable.new

connection_opts = {}
unless @proxy.nil?
connection_opts[:proxy] = {
:host => @proxy[:host],
:port => @proxy[:port]
}
connection_opts[:proxy][:authorization] = [@proxy[:user], @proxy[:password]] unless @proxy[:user].nil?
end

http = EventMachine::HttpRequest.new(@uri, connection_opts).post({
http = @client.em_http_client(@uri).post({
:query => @params, :timeout => 5, :body => @body,
:head => {'Content-Type'=> 'application/json'}
})
http.callback {
begin
handle_response(http.response_header.status, http.response.chomp)
deferrable.succeed
df.succeed
rescue => e
deferrable.fail(e)
df.fail(e)
end
}
http.errback {
Pusher.logger.debug("Network error connecting to pusher: #{http.inspect}")
deferrable.fail(Error.new("Network error connecting to pusher"))
df.fail(Error.new("Network error connecting to pusher"))
}

deferrable
df
end

private
Expand All @@ -123,10 +96,6 @@ def handle_response(status_code, body)
end
end

def ssl?
@uri.scheme == 'https'
end

def symbolize_first_level(hash)
hash.inject({}) do |result, (key, value)|
result[key.to_sym] = value
Expand Down

0 comments on commit 3425798

Please sign in to comment.