Class: Aliyun::Log::Request
- Inherits:
-
Object
- Object
- Aliyun::Log::Request
- Includes:
- Common::Logging
- Defined in:
- lib/aliyun/log/request.rb
Constant Summary
Constants included from Common::Logging
Common::Logging::MAX_NUM_LOG, Common::Logging::ROTATE_SIZE
Instance Method Summary collapse
- #canonicalized_headers(headers) ⇒ Object
- #canonicalized_resource(resource = '', query = {}) ⇒ Object
- #compact_headers(body = nil, is_pb = false) ⇒ Object
- #delete(resources, payload) ⇒ Object
- #do_request(verb, resources, payload) ⇒ Object
- #get(resources, payload = {}) ⇒ Object
-
#initialize(config) ⇒ Request
constructor
A new instance of Request.
- #post(resources, payload) ⇒ Object
- #put(resources, payload) ⇒ Object
- #signature(verb, resource, headers, query = {}) ⇒ Object
- #string_to_sign(verb, resource, headers, query = {}) ⇒ Object
Methods included from Common::Logging
log_file=, log_level=, #logger, logger, logger=, logger_level
Constructor Details
#initialize(config) ⇒ Request
Returns a new instance of Request.
17 18 19 |
# File 'lib/aliyun/log/request.rb', line 17 def initialize(config) @config = config end |
Instance Method Details
#canonicalized_headers(headers) ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'lib/aliyun/log/request.rb', line 125 def canonicalized_headers(headers) h = {} headers.each do |k, v| h[k.downcase] = v if k =~ /x-log-.*/ end h.keys.sort.map do |e| h[e] "#{e}:#{h[e].gsub(/^\s+/, '')}" end.join("\n") end |
#canonicalized_resource(resource = '', query = {}) ⇒ Object
136 137 138 139 140 141 142 143 144 |
# File 'lib/aliyun/log/request.rb', line 136 def canonicalized_resource(resource = '', query = {}) return resource if query.empty? url = URI.parse(resource) sort_str = query.keys.sort.map do |e| "#{e}=#{query[e]}" end.join('&') "#{url}?#{sort_str}" end |
#compact_headers(body = nil, is_pb = false) ⇒ Object
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 |
# File 'lib/aliyun/log/request.rb', line 77 def compact_headers(body = nil, is_pb = false) headers = { 'x-log-apiversion' => '0.6.0', 'x-log-signaturemethod' => 'hmac-sha1', 'x-log-bodyrawsize' => '0', 'Date' => DateTime.now.httpdate, 'User-Agent' => "aliyun-log ruby-#{RUBY_VERSION}/#{RUBY_PLATFORM}" } return headers if body.nil? if is_pb compressed = Zlib::Deflate.deflate(body.encode) headers['Content-Length'] = compressed.bytesize.to_s raise 'content length is larger than 3MB' if headers['Content-Length'].to_i > 3_145_728 headers['Content-MD5'] = Digest::MD5.hexdigest(compressed).upcase headers['Content-Type'] = 'application/x-protobuf' headers['x-log-compresstype'] = 'deflate' headers['x-log-bodyrawsize'] = body.encode.bytesize.to_s else headers['Content-Type'] = 'application/json' headers['Content-MD5'] = Digest::MD5.hexdigest(body.encode).upcase headers['x-log-bodyrawsize'] = body.bytesize.to_s end headers end |
#delete(resources, payload) ⇒ Object
33 34 35 |
# File 'lib/aliyun/log/request.rb', line 33 def delete(resources, payload) do_request('DELETE', resources, payload) end |
#do_request(verb, resources, payload) ⇒ Object
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 |
# File 'lib/aliyun/log/request.rb', line 37 def do_request(verb, resources, payload) resource_path = Utils.get_resource_path(resources) url = Utils.get_request_url(@config.endpoint, resources) = { method: verb, url: url, open_timeout: @config.open_timeout, read_timeout: @config.read_timeout } if verb == 'GET' headers = compact_headers headers['Authorization'] = signature(verb, resource_path, headers, payload) [:headers] = headers [:url] = URI.escape(canonicalized_resource([:url], payload)) else headers = compact_headers(payload, resources[:is_pb]) headers['Authorization'] = signature(verb, resource_path, headers) [:headers] = headers payload = Zlib::Deflate.deflate(payload.encode) if resources[:is_pb] [:payload] = payload end request = RestClient::Request.new() response = request.execute do |resp| if resp.code >= 300 e = ServerError.new(resp) logger.error("#{e.to_s} #{resp.code} #{resp}") raise e else resp.return! end end logger.debug("Received HTTP response, code: #{response.code}, " \ "url: #{[:url]}, " \ "method: #{verb}, headers: #{response.headers}, " \ "body: #{response.body.force_encoding('UTF-8')}") response end |
#get(resources, payload = {}) ⇒ Object
21 22 23 |
# File 'lib/aliyun/log/request.rb', line 21 def get(resources, payload = {}) do_request('GET', resources, payload) end |
#post(resources, payload) ⇒ Object
25 26 27 |
# File 'lib/aliyun/log/request.rb', line 25 def post(resources, payload) do_request('POST', resources, payload) end |
#put(resources, payload) ⇒ Object
29 30 31 |
# File 'lib/aliyun/log/request.rb', line 29 def put(resources, payload) do_request('PUT', resources, payload) end |
#signature(verb, resource, headers, query = {}) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'lib/aliyun/log/request.rb', line 104 def signature(verb, resource, headers, query = {}) sha1_digest = OpenSSL::HMAC.digest( OpenSSL::Digest.new('sha1'), @config.access_key_secret, string_to_sign(verb, resource, headers, query).chomp ) base64_sign = Base64.strict_encode64(sha1_digest) "LOG #{@config.access_key_id}:#{base64_sign}" end |
#string_to_sign(verb, resource, headers, query = {}) ⇒ Object
114 115 116 117 118 119 120 121 122 123 |
# File 'lib/aliyun/log/request.rb', line 114 def string_to_sign(verb, resource, headers, query = {}) <<~DOC #{verb} #{headers['Content-MD5']} #{headers['Content-Type']} #{headers['Date']} #{canonicalized_headers(headers)} #{canonicalized_resource(resource, query)} DOC end |