Class: ConoStorage::Client
- Inherits:
-
Object
- Object
- ConoStorage::Client
show all
- Includes:
- RequestMethods
- Defined in:
- lib/cono_storage/client.rb
Constant Summary
collapse
{
"Accept" => "application/json",
"User-Agent" => "ConoStorage Ruby Gem v#{ConoStorage::VERSION}",
"Content-Type" => "application/json"
}
Instance Method Summary
collapse
-
#auth_params ⇒ Object
-
#authorize ⇒ Object
-
#connection ⇒ Object
-
#create_token ⇒ Object
-
#default_headers ⇒ Object
-
#initialize(web_mode: false, auth_url:, tenant_id:, username:, password:, endpoint:) ⇒ Client
constructor
A new instance of Client.
-
#request(request_method, path, params, headers) ⇒ Object
-
#upload(path, file, params, headers) ⇒ Object
#delete_container, #delete_object, #get_account, #get_container, #get_object, #head_account, #head_container, #head_object, #post_account, #post_container, #post_object, #put_container, #put_object
Constructor Details
#initialize(web_mode: false, auth_url:, tenant_id:, username:, password:, endpoint:) ⇒ Client
Returns a new instance of Client.
21
22
23
24
25
26
27
28
29
|
# File 'lib/cono_storage/client.rb', line 21
def initialize(web_mode: false,
auth_url: , tenant_id: , username: , password: , endpoint: )
@auth_url = auth_url
@web_mode = web_mode
@tenant_id = tenant_id
@username = username
@password = password
@endpoint = endpoint
end
|
Instance Method Details
#auth_params ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/cono_storage/client.rb', line 73
def auth_params
{
auth: {
tenantId: @tenant_id,
passwordCredentials: {
username: @username,
password: @password
}
}
}
end
|
#authorize ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/cono_storage/client.rb', line 56
def authorize
if @token.nil? || (Time.parse(@token['expires']) <= Time.now)
create_token
else
@token
end
end
|
#connection ⇒ Object
107
108
109
110
111
112
|
# File 'lib/cono_storage/client.rb', line 107
def connection
@connection ||= Faraday.new(headers: ) do |connection|
connection.request :json
connection.adapter Faraday.default_adapter
end
end
|
#create_token ⇒ Object
64
65
66
67
68
69
70
71
|
# File 'lib/cono_storage/client.rb', line 64
def create_token
res = connection.post do |req|
req.url @auth_url + '/tokens'
req.body = auth_params.to_json
end
json = JSON.parse(res.body)
@token = json['access']['token']
end
|
85
86
87
88
89
90
91
92
93
|
# File 'lib/cono_storage/client.rb', line 85
def
= DEFAULT_HEADERS.clone
['Content-Type'] = 'application/json'
if @web_mode
['X-Web-Mode'] = 'True'
['X-Container-Read'] = '.r:*'
end
end
|
#request(request_method, path, params, headers) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/cono_storage/client.rb', line 95
def request(request_method, path, params, )
authorize
Response.new(
connection.send(
request_method,
@endpoint + path,
params,
.merge('X-Auth-Token' => @token['id'])
)
)
end
|
#upload(path, file, params, headers) ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/cono_storage/client.rb', line 39
def upload(path, file, params, )
if file.is_a?(String)
file = File.open(file)
end
content_type = MimeMagic.by_magic(file).type
io = Faraday::UploadIO.new(file, content_type)
= {
'X-Detect-Content-Type' => 'True',
'Transfer-Encoding' => 'chunked',
'Content-Type' => content_type
}
path = path + '?' + params.to_query if params
request(:put, path, io, .merge())
end
|