Module: Qiniu::HTTP
- Defined in:
- lib/qiniu/http.rb
Constant Summary collapse
- API_RESULT_MIMETYPE =
get
'application/json'
Class Method Summary collapse
- .api_get(url, opts = {}) ⇒ Object
-
.api_post(url, req_body = nil, opts = {}) ⇒ Object
post.
-
.generate_query_string(params) ⇒ Object
is_response_ok?.
-
.get(url, opts = {}) ⇒ Object
generate_query_string.
- .is_response_ok?(http_code) ⇒ Boolean
-
.management_post(url, body = '') ⇒ Object
api_post.
-
.post(url, req_body = nil, opts = {}) ⇒ Object
api_get.
Class Method Details
.api_get(url, opts = {}) ⇒ Object
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 |
# File 'lib/qiniu/http.rb', line 48 def api_get (url, opts = {}) ### 配置请求Header headers = { :accept => API_RESULT_MIMETYPE } # 将特定Header混入外部Header中 if opts[:headers].is_a?(Hash) then opts[:headers] = opts[:headers].dup.merge!(headers) else opts[:headers] = headers end ### 发送请求,然后转换返回值 resp_code, resp_body, resp_headers = get(url, opts) if resp_code.nil? then return 0, {}, {} end content_type = resp_headers["content-type"][0] if !content_type.nil? && content_type == API_RESULT_MIMETYPE then # 如果是JSON格式,则反序列化 resp_body = Utils.safe_json_parse(resp_body) end return resp_code, resp_body, resp_headers end |
.api_post(url, req_body = nil, opts = {}) ⇒ Object
post
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/qiniu/http.rb', line 100 def api_post (url, req_body = nil, opts = {}) ### 配置请求Header headers = { :accept => API_RESULT_MIMETYPE } # 将特定Header混入外部Header中 if opts[:headers].is_a?(Hash) then opts[:headers] = opts[:headers].dup.merge!(headers) else opts[:headers] = headers end ### 发送请求,然后转换返回值 resp_code, resp_body, resp_headers = post(url, req_body, opts) if resp_code.nil? then return 0, {}, {} end content_type = resp_headers["content-type"][0] if !content_type.nil? && content_type == API_RESULT_MIMETYPE then # 如果是JSON格式,则反序列化 resp_body = Utils.safe_json_parse(resp_body) end return resp_code, resp_body, resp_headers end |
.generate_query_string(params) ⇒ Object
is_response_ok?
13 14 15 16 17 18 19 20 |
# File 'lib/qiniu/http.rb', line 13 def generate_query_string(params) if params.is_a?(Hash) total_param = params.map { |key, value| %Q(#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s).gsub('+', '%20')}) } return total_param.join("&") end return params end |
.get(url, opts = {}) ⇒ Object
generate_query_string
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/qiniu/http.rb', line 22 def get (url, opts = {}) ### 配置请求Header req_headers = { :connection => 'close', :accept => '*/*', :user_agent => Config.settings[:user_agent] } # 优先使用外部Header,覆盖任何特定Header if opts[:headers].is_a?(Hash) then req_headers.merge!(opts[:headers]) end ### 发送请求 response = RestClient.get(url, req_headers) return response.code.to_i, response.body, response.raw_headers rescue => e Log.logger.warn "#{e.} => Qiniu::HTTP.get('#{url}')" if e.respond_to?(:response) && e.response.respond_to?(:code) then return e.response.code, e.response.body, e.response.raw_headers end return nil, nil, nil end |
.is_response_ok?(http_code) ⇒ Boolean
9 10 11 |
# File 'lib/qiniu/http.rb', line 9 def is_response_ok?(http_code) return 200 <= http_code && http_code <= 299 end |
.management_post(url, body = '') ⇒ Object
api_post
128 129 130 131 132 133 |
# File 'lib/qiniu/http.rb', line 128 def management_post (url, body = '') ### 授权并执行管理操作 return HTTP.api_post(url, body, { :headers => { 'Authorization' => 'QBox ' + Auth.generate_acctoken(url, body) } }) end |
.post(url, req_body = nil, opts = {}) ⇒ Object
api_get
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/qiniu/http.rb', line 76 def post (url, req_body = nil, opts = {}) ### 配置请求Header req_headers = { :connection => 'close', :accept => '*/*', :user_agent => Config.settings[:user_agent] } # 优先使用外部Header,覆盖任何特定Header if opts[:headers].is_a?(Hash) then req_headers.merge!(opts[:headers]) end ### 发送请求 response = RestClient.post(url, req_body, req_headers) return response.code.to_i, response.body, response.raw_headers rescue => e Log.logger.warn "#{e.} => Qiniu::HTTP.post('#{url}')" if e.respond_to?(:response) && e.response.respond_to?(:code) then return e.response.code, e.response.body, e.response.raw_headers end return nil, nil, nil end |