Class: Protobuf::Rpc::Connectors::Http
- Includes:
- Logger::LogMethods, Common
- Defined in:
- lib/protobuf/rpc/connectors/http.rb
Instance Attribute Summary
Attributes included from Common
Attributes inherited from Base
#complete_cb, #failure_cb, #options, #success_cb
Instance Method Summary collapse
- #base ⇒ Object
- #client ⇒ Object
-
#close_connection ⇒ Object
private.
-
#error? ⇒ Boolean
Method to determine error state, must be used with Connector api.
- #host ⇒ Object
- #log_signature ⇒ Object
- #send_data ⇒ Object
- #send_request ⇒ Object
Methods included from Logger::LogMethods
included, #log_exception, #sign_message
Methods included from Common
#any_callbacks?, #complete, #data_callback, #fail, #initialize_stats, #parse_response, #post_init, #request_bytes, #request_caller, #setup_connection, #succeed, #timeout_wrap, #validate_request_type!, #verify_callbacks, #verify_options!
Methods inherited from Base
#first_alive_load_balance?, #initialize, #ping_port, #ping_port_enabled?
Constructor Details
This class inherits a constructor from Protobuf::Rpc::Connectors::Base
Instance Method Details
#base ⇒ Object
43 44 45 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 43 def base [:base] or '' end |
#client ⇒ Object
47 48 49 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 47 def client @_client ||= Faraday.new(:url => host) end |
#close_connection ⇒ Object
private
25 26 27 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 25 def close_connection log_debug { ('Connector closed') } end |
#error? ⇒ Boolean
Method to determine error state, must be used with Connector api
30 31 32 33 34 35 36 37 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 30 def error? log_debug { ("Error state : #{@error}") } if @error true else false end end |
#host ⇒ Object
39 40 41 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 39 def host 'http://' + [:host] + ':' + [:port].to_s end |
#log_signature ⇒ Object
19 20 21 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 19 def log_signature @_log_signature ||= "[http-client-#{self.class.name}]" end |
#send_data ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 51 def send_data rpc_request = ::Protobuf::Socketrpc::Request.decode(@request_data) http_response = client.post do |http_request| path_components = [''] + rpc_request[:service_name].split('::') + [rpc_request[:method_name]] http_request.url base + path_components.map{ |x| CGI::escape(x) }.join('/') http_request.headers['Content-Type'] = 'application/x-protobuf' http_request.headers['X-Protobuf-Caller'] = rpc_request[:caller] || '' http_request.body = rpc_request[:request_proto] end # Server returns protobuf response with no error if http_response.status == 200 and http_response.headers['x-protobuf-error'].nil? rpc_response = Protobuf::Socketrpc::Response.new( :response_proto => http_response.body ) # Server returns protobuf error elsif http_response.status != 200 and not http_response.headers['x-protobuf-error'].nil? rpc_response = Protobuf::Socketrpc::Response.new( :response_proto => http_response.body, :error => http_response.headers['x-protobuf-error'], :error_reason => http_response.headers['x-protobuf-error-reason'].to_i ) # Server didn't return a response or error else rpc_response = Protobuf::Socketrpc::Response.new( :response_proto => http_response.body, :error => "Bad response from the server.", :error_reason => Protobuf::Socketrpc::ErrorReason::BAD_RESPONSE_PROTO ) end @response_data = rpc_response.encode() parse_response end |
#send_request ⇒ Object
12 13 14 15 16 17 |
# File 'lib/protobuf/rpc/connectors/http.rb', line 12 def send_request timeout_wrap do setup_connection post_init end end |