class ConoStorage::Client

Constants

DEFAULT_HEADERS

Public Class Methods

new(web_mode: false, auth_url: , tenant_id: , username: , password: , endpoint: ) click to toggle source

@param web_mode [Boolean] true でコンテナやオブジェクトを作成/更新すると公開状態になります。 @param auth_url [String] @param tenant_id [String] @param username [String] @param password [String] @param endpoint [String]

# 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

Public Instance Methods

auth_params() click to toggle source
# File lib/cono_storage/client.rb, line 73
def auth_params
  {
    auth: {
      tenantId: @tenant_id,
      passwordCredentials: {
        username: @username,
        password: @password
      }
    }
  }
end
authorize() click to toggle source
# 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() click to toggle source
# File lib/cono_storage/client.rb, line 107
def connection
  @connection ||= Faraday.new(headers: default_headers) do |connection|
    connection.request :json
    connection.adapter Faraday.default_adapter
  end
end
create_token() click to toggle source
# 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
default_headers() click to toggle source
# File lib/cono_storage/client.rb, line 85
def default_headers
  headers = DEFAULT_HEADERS.clone
  headers['Content-Type'] = 'application/json'
  if @web_mode
    headers['X-Web-Mode'] = 'True'
    headers['X-Container-Read'] = '.r:*'
  end
  headers
end
request(request_method, path, params, headers) click to toggle source
# File lib/cono_storage/client.rb, line 95
def request(request_method, path, params, headers)
  authorize
  Response.new(
    connection.send(
      request_method,
      @endpoint + path,
      params,
      headers.merge('X-Auth-Token' => @token['id'])
    )
  )
end
upload(path, file, params, headers) click to toggle source
# File lib/cono_storage/client.rb, line 39
def upload(path, file, params, headers)
  if file.is_a?(String)
    file = File.open(file)
  end

  content_type = MimeMagic.by_magic(file).type

  io = Faraday::UploadIO.new(file, content_type)
  extra_headers = {
    'X-Detect-Content-Type' => 'True',
    'Transfer-Encoding' => 'chunked',
    'Content-Type' => content_type
  }
  path = path + '?' + params.to_query if params
  request(:put, path, io, extra_headers.merge(headers))
end