Class: Qinium::Client
- Inherits:
-
Object
show all
- Includes:
- Utils
- Defined in:
- lib/qinium/client.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Utils
#blank?, #encode_entry_uri, #logger, #to_query_string, #urlsafe_base64_decode, #urlsafe_base64_encode
Constructor Details
#initialize(config) ⇒ Client
Returns a new instance of Client.
7
8
9
|
# File 'lib/qinium/client.rb', line 7
def initialize(config)
@config = config
end
|
Instance Attribute Details
Returns the value of attribute config.
5
6
7
|
# File 'lib/qinium/client.rb', line 5
def config
@config
end
|
Class Method Details
.success?(http_code) ⇒ Boolean
119
120
121
|
# File 'lib/qinium/client.rb', line 119
def self.success?(http_code)
http_code.between?(200, 299)
end
|
Instance Method Details
#access_key ⇒ Object
13
|
# File 'lib/qinium/client.rb', line 13
def access_key; config.access_key end
|
#get(url, opts = {}) ⇒ Object
rubocop: enable Style/SingleLineMethods
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/qinium/client.rb', line 17
def get(url, opts = {})
opts[:raise] = true unless opts.key?(:raise)
= {
connection: "close",
accept: "*/*",
user_agent: user_agent
}
.merge!(opts[:headers]) if opts[:headers].is_a?(Hash)
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
logger.debug "Get #{uri}"
logger.debug " options: #{opts.to_json}"
req = Net::HTTP::Get.new(uri)
.each_pair do |key, value|
req.add_field key, value
end
res = http.request(req)
code = res.code.to_i
body = res.body
= res..to_hash.transform_values do |v|
v.join(", ")
end
if ["content-type"] == "application/json"
body = begin
JSON.parse(body)
rescue StandardError
{}
end
end
raise APIError.new(code, body, ) if opts[:raise] && !success?(code)
[code, body, ]
end
|
#management_get(url) ⇒ Object
102
103
104
105
106
107
108
|
# File 'lib/qinium/client.rb', line 102
def management_get(url)
access_token = Auth.generate_access_token(access_key, secret_key, url)
get(url, headers: {
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "QBox " + access_token
})
end
|
#management_post(url, body = nil) ⇒ Object
110
111
112
113
114
115
116
117
|
# File 'lib/qinium/client.rb', line 110
def management_post(url, body = nil)
body = JSON.dump(body) if body && body.size > 0
access_token = Auth.generate_access_token(access_key, secret_key, url, body)
post(url, body, headers: {
"Content-Type" => "application/x-www-form-urlencoded",
"Authorization" => "QBox " + access_token
})
end
|
#post(url, req_body = "", opts = {}) ⇒ Object
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
|
# File 'lib/qinium/client.rb', line 59
def post(url, req_body = "", opts = {})
opts[:raise] = true unless opts.key?(:raise)
= {
connection: "close",
accept: "*/*",
user_agent: user_agent
}
.merge!(opts[:headers]) if opts[:headers].is_a?(Hash)
uri = URI(url)
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
end
logger.debug "POST #{uri}"
logger.debug " body: #{req_body[0, 50]}" if req_body.is_a?(String)
logger.debug " headers: #{opts.to_json}"
req = Net::HTTP::Post.new(uri)
.each_pair do |key, value|
req.add_field key, value
end
req.body = req_body
res = http.request(req)
code = res.code.to_i
body = res.body
= res..to_hash.transform_values do |v|
v.join(", ")
end
if ["content-type"] == "application/json"
body = begin
JSON.parse(body)
rescue StandardError
nil
end
end
raise APIError.new(code, body, ) if opts[:raise] && !success?(code)
[code, body, ]
end
|
#secret_key ⇒ Object
14
|
# File 'lib/qinium/client.rb', line 14
def secret_key; config.secret_key end
|
#success?(http_code) ⇒ Boolean
123
124
125
|
# File 'lib/qinium/client.rb', line 123
def success?(http_code)
self.class.success?(http_code)
end
|
#user_agent ⇒ Object
rubocop: disable Style/SingleLineMethods
12
|
# File 'lib/qinium/client.rb', line 12
def user_agent; config.user_agent end
|