Class: AdobeMediaEncoder::API::Client::HTTPClient
- Inherits:
-
Object
- Object
- AdobeMediaEncoder::API::Client::HTTPClient
- Defined in:
- lib/adobe_media_encoder/api/client.rb
Constant Summary collapse
- DEFAULT_HTTP_HOST_ADDRESS =
'localhost'
- DEFAULT_HTTP_HOST_PORT =
8080
Instance Attribute Summary collapse
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#default_request_headers ⇒ Object
Returns the value of attribute default_request_headers.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http ⇒ Object
Returns the value of attribute http.
-
#log_pretty_print_body ⇒ Object
Returns the value of attribute log_pretty_print_body.
-
#log_request_body ⇒ Object
Returns the value of attribute log_request_body.
-
#log_response_body ⇒ Object
Returns the value of attribute log_response_body.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#port ⇒ Object
Returns the value of attribute port.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
- #build_uri(path = '', query = { }) ⇒ Object
- #delete(path, options = { }) ⇒ Object
-
#format_body_for_log_output(obj) ⇒ String
Formats a HTTPRequest or HTTPResponse body for log output.
- #get(path, query = nil, options = { }) ⇒ Object
-
#initialize(args = { }) ⇒ HTTPClient
constructor
A new instance of HTTPClient.
- #initialize_http(args = { }) ⇒ Object
- #initialize_logger(args = { }) ⇒ Object
- #post(path, body = nil, options = { }) ⇒ Object
- #put(path, body, options = { }) ⇒ Object
- #response_parsed ⇒ Object
- #send_request(request) ⇒ Object
Constructor Details
#initialize(args = { }) ⇒ HTTPClient
Returns a new instance of HTTPClient.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/adobe_media_encoder/api/client.rb', line 65 def initialize(args = { }) initialize_logger(args) initialize_http(args) @default_request_headers = { 'Content-Type' => 'application/xml; charset=utf-8', 'Accept' => 'application/xml', } @parse_response = true @base_uri = "http://#{host}:#{port}/" @log_request_body = args.fetch(:log_request_body, true) @log_response_body = args.fetch(:log_response_body, true) @log_pretty_print_body = args.fetch(:log_pretty_print_body, true) end |
Instance Attribute Details
#base_uri ⇒ Object
Returns the value of attribute base_uri.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def base_uri @base_uri end |
#default_request_headers ⇒ Object
Returns the value of attribute default_request_headers.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def default_request_headers @default_request_headers end |
#host ⇒ Object
Returns the value of attribute host.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def host @host end |
#http ⇒ Object
Returns the value of attribute http.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def http @http end |
#log_pretty_print_body ⇒ Object
Returns the value of attribute log_pretty_print_body.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def log_pretty_print_body @log_pretty_print_body end |
#log_request_body ⇒ Object
Returns the value of attribute log_request_body.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def log_request_body @log_request_body end |
#log_response_body ⇒ Object
Returns the value of attribute log_response_body.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def log_response_body @log_response_body end |
#logger ⇒ Object
Returns the value of attribute logger.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def logger @logger end |
#port ⇒ Object
Returns the value of attribute port.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def port @port end |
#request ⇒ Object
Returns the value of attribute request.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
59 60 61 |
# File 'lib/adobe_media_encoder/api/client.rb', line 59 def response @response end |
Instance Method Details
#build_uri(path = '', query = { }) ⇒ Object
131 132 133 134 135 |
# File 'lib/adobe_media_encoder/api/client.rb', line 131 def build_uri(path = '', query = { }) _query = query.is_a?(Hash) ? query.map { |k,v| "#{CGI.escape(k)}=#{CGI.escape(v)}" }.join('&') : query _path = "#{path}#{_query and _query.respond_to?(:empty?) and !_query.empty? ? "?#{_query}" : ''}" URI.parse(File.join(base_uri, _path)) end |
#delete(path, options = { }) ⇒ Object
137 138 139 140 141 142 |
# File 'lib/adobe_media_encoder/api/client.rb', line 137 def delete(path, = { }) query = .fetch(:query, { }) @uri = build_uri(path, query) request = Net::HTTP::Delete.new(@uri.request_uri, default_request_headers) send_request(request) end |
#format_body_for_log_output(obj) ⇒ String
Formats a HTTPRequest or HTTPResponse body for log output.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/adobe_media_encoder/api/client.rb', line 101 def format_body_for_log_output(obj) output = '' if obj.content_type == 'application/json' if @log_pretty_print_body _body = obj.body output << "\n" output << JSON.pretty_generate(JSON.parse(_body)) rescue _body return output else return obj.body end else return obj.body.inspect end end |
#get(path, query = nil, options = { }) ⇒ Object
144 145 146 147 148 149 |
# File 'lib/adobe_media_encoder/api/client.rb', line 144 def get(path, query = nil, = { }) query ||= .fetch(:query, { }) @uri = build_uri(path, query) request = Net::HTTP::Get.new(@uri.request_uri, default_request_headers) send_request(request) end |
#initialize_http(args = { }) ⇒ Object
92 93 94 95 96 |
# File 'lib/adobe_media_encoder/api/client.rb', line 92 def initialize_http(args = { }) @host = args[:host] ||= DEFAULT_HTTP_HOST_ADDRESS @port = args[:port] ||= DEFAULT_HTTP_HOST_PORT @http = Net::HTTP.new(host, port) end |
#initialize_logger(args = { }) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/adobe_media_encoder/api/client.rb', line 82 def initialize_logger(args = { }) @logger = args[:logger] ||= Logger.new(args[:log_to] || STDERR) log_level = args[:log_level] if log_level @logger.level = log_level args[:logger] = @logger end @logger end |
#post(path, body = nil, options = { }) ⇒ Object
161 162 163 164 165 166 167 168 169 |
# File 'lib/adobe_media_encoder/api/client.rb', line 161 def post(path, body = nil, = { }) query = .fetch(:query, { }) @uri = build_uri(path, query) #body = JSON.generate(body) unless body.is_a?(String) request = Net::HTTP::Post.new(@uri.request_uri, default_request_headers) request.body = body send_request(request) end |
#put(path, body, options = { }) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/adobe_media_encoder/api/client.rb', line 151 def put(path, body, = { }) query = .fetch(:query, { }) @uri = build_uri(path, query) #body = JSON.generate(body) unless body.is_a?(String) request = Net::HTTP::Put.new(@uri.request_uri, default_request_headers) request.body = body send_request(request) end |
#response_parsed ⇒ Object
127 128 129 |
# File 'lib/adobe_media_encoder/api/client.rb', line 127 def response_parsed XMLHelper.parse_payload(response.body) #rescue response end |
#send_request(request) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/adobe_media_encoder/api/client.rb', line 117 def send_request(request) @request = request logger.debug { %(REQUEST: #{request.method} http#{http.use_ssl? ? 's' : ''}://#{http.address}:#{http.port}#{request.path} HEADERS: #{request.to_hash.inspect} #{log_request_body and request.request_body_permitted? ? "BODY: #{format_body_for_log_output(request)}" : ''}) } @response = http.request(request) logger.debug { %(RESPONSE: #{response.inspect} HEADERS: #{response.to_hash.inspect} #{log_response_body and response.respond_to?(:body) ? "BODY: #{format_body_for_log_output(response)}" : ''}) } @parse_response ? response_parsed : response.body end |