Class: Baidubce::Http::BaseHttpClient
- Inherits:
-
Object
- Object
- Baidubce::Http::BaseHttpClient
- Includes:
- Log
- Defined in:
- lib/baidubce/http/base_http_client.rb
Defined Under Namespace
Classes: BufWriter
Constant Summary
Constants included from Log
Log::DEFAULT_LOG_FILE, Log::LOG_FILE_SIZE, Log::MAX_NUM_LOG
Instance Method Summary collapse
-
#send_request(config, signer, http_method, path, params, headers, body, save_path = nil, use_backup_endpoint = false, &block) ⇒ Object
Send request to BCE services.
- #set_content_length_header(headers, body, &block) ⇒ Object
Methods included from Log
#logger, set_log_file, set_log_level
Instance Method Details
#send_request(config, signer, http_method, path, params, headers, body, save_path = nil, use_backup_endpoint = false, &block) ⇒ Object
Send request to BCE services.
35 36 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/baidubce/http/base_http_client.rb', line 35 def send_request(config, signer, http_method, path, params, headers, body, save_path=nil, use_backup_endpoint=false, &block) headers[USER_AGENT] = sprintf( 'bce-sdk-ruby/%s/%s/%s', VERSION, RUBY_VERSION, RUBY_PLATFORM ) should_get_new_date = headers.has_key?(BCE_DATE) ? false : true url, headers[HOST] = Utils.parse_url_host(config, use_backup_endpoint) url += Utils.url_encode_except_slash(path) query_str = Utils.get_canonical_querystring(params, false) url += "?#{query_str}" unless query_str.to_s.empty? logger.info("url: #{url}, params: #{params}") set_content_length_header(headers, body, &block) headers[STS_SECURITY_TOKEN] = config.credentials.security_token unless config.credentials.security_token.to_s.empty? retries_attempted = 0 while true headers[BCE_DATE] = Time.now.utc.iso8601 if should_get_new_date headers[AUTHORIZATION] = signer.sign(config.credentials, http_method, path, headers, params) logger.debug("Request headers: #{headers}") args = { method: http_method, url: url, headers: headers, payload: body, open_timeout: config.open_timeout_in_millis / 1000.0, read_timeout: config.read_timeout_in_millis / 1000.0 } args[:payload] = BufWriter.new(&block) if block_given? begin if save_path logger.debug("Response save file path: #{save_path}") resp_headers = {} File.open(save_path, 'w+') { |f| block = proc { |response| response.read_body { |chunk| f << chunk } resp_headers = response.to_hash resp_headers.each { |k, v| resp_headers[k]=v[0] } raise BceHttpException.new(response.code.to_i, resp_headers, '', 'get_object_to_file exception') if response.code.to_i > 300 } block_arg = { block_response: block } args.merge! block_arg RestClient::Request.new(args).execute return '', resp_headers } else resp = RestClient::Request.new(args).execute logger.debug("Response code: #{resp.code}") logger.debug("Response headers: #{resp.headers.to_s}") return resp.body, resp.headers end rescue SocketError => err if config.retry_policy.should_retry(SOCKET_ERROR_CODE, retries_attempted) delay_in_millis = config.retry_policy.get_delay_before_next_retry_in_millis(retries_attempted) sleep(delay_in_millis / 1000.0) else raise BceClientException.new("SocketError: #{err}") end rescue BceHttpException, RestClient::ExceptionWithResponse => err logger.debug("ExceptionWithResponse: #{err.http_code}, #{err.http_body}, #{err.http_headers}, #{err.}") if config.retry_policy.should_retry(err.http_code, retries_attempted) delay_in_millis = config.retry_policy.get_delay_before_next_retry_in_millis(retries_attempted) sleep(delay_in_millis / 1000.0) else request_id = err.http_headers[:x_bce_request_id] if err.is_a?(BceHttpException) err.http_body = File.read(save_path) request_id = err.http_headers['x-bce-request-id'] end msg = err.http_body if err.http_body.empty? msg = "{\"code\":\"#{err.http_code}\",\"message\":\"#{err.}\",\"requestId\":\"#{request_id}\"}" end raise BceServerException.new(err.http_code, msg) end end retries_attempted += 1 end end |
#set_content_length_header(headers, body, &block) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/baidubce/http/base_http_client.rb', line 125 def set_content_length_header(headers, body, &block) unless block_given? if body.to_s.empty? # headers[CONTENT_LENGTH] = 0 headers.delete(CONTENT_LENGTH) elsif body.instance_of?(String) body = body.encode('UTF-8') if body.encoding.name != 'UTF-8' headers[CONTENT_LENGTH] = body.bytesize elsif body.instance_of?(File) headers[CONTENT_LENGTH] = body.size() else headers[CONTENT_LENGTH] = ObjectSpace.memsize_of(body) end end end |