Class: Ubiquity::Wiredrive::API::V3::HTTPClient
- Inherits:
-
Object
- Object
- Ubiquity::Wiredrive::API::V3::HTTPClient
- Defined in:
- lib/ubiquity/wiredrive/api/v3/http_client.rb
Defined Under Namespace
Classes: CaseSensitiveHeaderKey, RateLimitException
Constant Summary collapse
- DEFAULT_HTTP_HOST_ADDRESS =
'api.wiredrive.com'- DEFAULT_HTTP_HOST_PORT =
443
Instance Attribute Summary collapse
-
#authorization_header_name ⇒ Object
Returns the value of attribute authorization_header_name.
-
#authorization_header_value ⇒ Object
Returns the value of attribute authorization_header_value.
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#default_request_headers ⇒ Object
Returns the value of attribute default_request_headers.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#http ⇒ Object
Returns the value of attribute http.
-
#http_host_address ⇒ Object
Returns the value of attribute http_host_address.
-
#http_host_port ⇒ Object
Returns the value of attribute http_host_port.
-
#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.
-
#password ⇒ Object
Returns the value of attribute password.
-
#request ⇒ Object
Returns the value of attribute request.
-
#response ⇒ Object
Returns the value of attribute response.
-
#username ⇒ Object
Returns the value of attribute username.
Instance Method Summary collapse
- #auth_token=(value) ⇒ Object
- #build_uri(path = '', query = nil) ⇒ Object
- #call_method(method_name = :get, args = { }, options = { }) ⇒ 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, options = { }) ⇒ Object
- #put(path, body, options = { }) ⇒ Object
- #request_method_name_to_class_name(method_name) ⇒ Object
- #response_parsed ⇒ Object
- #send_request(request) ⇒ Object
- #sleep_break(seconds) ⇒ Object
Constructor Details
#initialize(args = { }) ⇒ HTTPClient
Returns a new instance of HTTPClient.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 37 def initialize(args = { }) args = args.dup initialize_logger(args) @http_host_address = args[:http_host_address] ||= DEFAULT_HTTP_HOST_ADDRESS @http_host_port = args[:http_host_port] ||= DEFAULT_HTTP_HOST_PORT initialize_http(args) @user_agent_default = "Ubiquity Wiredrive Ruby SDK Version #{Ubiquity::Wiredrive::VERSION}" @authorization_header_name = 'Authorization' # CaseSensitiveHeaderKey.new('Authorization') @authorization_header_value = nil # @auth_token # "Bearer #{@auth_token}" @default_request_headers = { 'user-agent' => @user_agent_default, 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json', } _auth_token = args[:auth_token] self.auth_token = _auth_token if _auth_token @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) @cancelled = false @delay_between_rate_limit_retries = 5 @parse_response = args.fetch(:parse_response, true) end |
Instance Attribute Details
#authorization_header_name ⇒ Object
Returns the value of attribute authorization_header_name.
28 29 30 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 28 def @authorization_header_name end |
#authorization_header_value ⇒ Object
Returns the value of attribute authorization_header_value.
28 29 30 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 28 def @authorization_header_value end |
#base_uri ⇒ Object
Returns the value of attribute base_uri.
25 26 27 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 25 def base_uri @base_uri end |
#default_request_headers ⇒ Object
Returns the value of attribute default_request_headers.
28 29 30 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 28 def default_request_headers @default_request_headers end |
#hostname ⇒ Object
Returns the value of attribute hostname.
26 27 28 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 26 def hostname @hostname end |
#http ⇒ Object
Returns the value of attribute http.
25 26 27 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 25 def http @http end |
#http_host_address ⇒ Object
Returns the value of attribute http_host_address.
25 26 27 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 25 def http_host_address @http_host_address end |
#http_host_port ⇒ Object
Returns the value of attribute http_host_port.
25 26 27 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 25 def http_host_port @http_host_port end |
#log_pretty_print_body ⇒ Object
Returns the value of attribute log_pretty_print_body.
30 31 32 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 30 def log_pretty_print_body @log_pretty_print_body end |
#log_request_body ⇒ Object
Returns the value of attribute log_request_body.
30 31 32 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 30 def log_request_body @log_request_body end |
#log_response_body ⇒ Object
Returns the value of attribute log_response_body.
30 31 32 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 30 def log_response_body @log_response_body end |
#logger ⇒ Object
Returns the value of attribute logger.
25 26 27 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 25 def logger @logger end |
#password ⇒ Object
Returns the value of attribute password.
26 27 28 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 26 def password @password end |
#request ⇒ Object
Returns the value of attribute request.
32 33 34 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 32 def request @request end |
#response ⇒ Object
Returns the value of attribute response.
32 33 34 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 32 def response @response end |
#username ⇒ Object
Returns the value of attribute username.
26 27 28 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 26 def username @username end |
Instance Method Details
#auth_token=(value) ⇒ Object
170 171 172 173 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 170 def auth_token=(value) @auth_token = value default_request_headers[] = @auth_token # "Bearer #{@auth_token}" end |
#build_uri(path = '', query = nil) ⇒ Object
154 155 156 157 158 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 154 def build_uri(path = '', query = nil) _query = query.is_a?(Hash) ? query.map { |k,v| "#{CGI.escape(k.to_s)}=#{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 |
#call_method(method_name = :get, args = { }, options = { }) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 183 def call_method(method_name = :get, args = { }, = { }) headers = args[:headers] || [:headers] || { } path = args[:path] || '' query = args[:query] || { } body = args[:body] # Allow the default request headers to be overridden _default_request_headers = .fetch(:default_request_headers, default_request_headers) _default_request_headers ||= { } _headers = _default_request_headers.merge(headers) @uri = build_uri(path, query) klass_name = request_method_name_to_class_name(method_name) klass = Net::HTTP.const_get(klass_name) request = klass.new(@uri.request_uri, _headers) if request.request_body_permitted? _body = (body and !body.is_a?(String)) ? JSON.generate(body) : body logger.debug { "Processing Body: '#{_body}'" } request.body = _body if _body end send_request(request) end |
#delete(path, options = { }) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 209 def delete(path, = { }) query = .fetch(:query, { }) @uri = build_uri(path, query) request = Net::HTTP::Delete.new(@uri.request_uri, default_request_headers) body = [:body] if body body = JSON.generate(body) unless body.is_a?(String) request.body = body end send_request(request) end |
#format_body_for_log_output(obj) ⇒ String
Formats a HTTPRequest or HTTPResponse body for log output.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 99 def format_body_for_log_output(obj) case obj.content_type when 'application/json' return obj.body unless @log_pretty_print_body _body = obj.body JSON.pretty_generate(JSON.parse(_body)) rescue _body when 'application/xml'; obj.body else obj.body.inspect end end |
#get(path, query = nil, options = { }) ⇒ Object
224 225 226 227 228 229 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 224 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
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 84 def initialize_http(args = { }) @http = Net::HTTP.new(http_host_address, http_host_port) http.use_ssl = true # TODO Add SSL Patch http.verify_mode = OpenSSL::SSL::VERIFY_NONE @base_uri = args[:base_uri] || "http#{http.use_ssl? ? 's' : ''}://#{http.address}:#{http.port}/" http end |
#initialize_logger(args = { }) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 74 def initialize_logger(args = { }) @logger = args[:logger] ||= Logger.new(args[:log_to] || STDOUT) log_level = args[:log_level] if log_level @logger.level = log_level args[:logger] = @logger end @logger end |
#post(path, body, options = { }) ⇒ Object
241 242 243 244 245 246 247 248 249 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 241 def post(path, body, = { }) 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
231 232 233 234 235 236 237 238 239 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 231 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 |
#request_method_name_to_class_name(method_name) ⇒ Object
161 162 163 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 161 def request_method_name_to_class_name(method_name) method_name.to_s.capitalize end |
#response_parsed ⇒ Object
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 138 def response_parsed @response_parsed ||= begin response_content_type = response.content_type logger.debug { "Parsing Response: #{response_content_type}" } case response_content_type when 'application/json' JSON.parse(response.body) rescue response # when 'text/html' # when 'text/plain' else response.body end end end |
#send_request(request) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 111 def send_request(request) @response_parsed = nil @request = request begin 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? ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(request)}\n-- BODY END --" : ''}) } @response = http.request(request) logger.debug { %(RESPONSE: #{response.inspect} HEADERS: #{response.to_hash.inspect} #{log_response_body and response.respond_to?(:body) ? "\n-- BODY BEGIN --\n#{format_body_for_log_output(response)}\n-- BODY END--" : ''}) } raise RateLimitException, "#{response.to_hash.inspect}" if response.code == '420' rescue RateLimitException => e logger.warn { "Rate Limited. Will retry in #{@delay_between_rate_limit_retries} seconds." } sleep_break @delay_between_rate_limit_retries retry unless @cancelled end @parse_response ? response_parsed : response.body end |
#sleep_break(seconds) ⇒ Object
130 131 132 133 134 135 136 |
# File 'lib/ubiquity/wiredrive/api/v3/http_client.rb', line 130 def sleep_break(seconds) while (seconds > 0) sleep(1) seconds -= 1 break if @cancelled end end |