Class: ConoStorage::Client

Inherits:
Object
  • Object
show all
Includes:
RequestMethods
Defined in:
lib/cono_storage/client.rb

Constant Summary collapse

DEFAULT_HEADERS =
{
  "Accept" => "application/json",
  "User-Agent" => "ConoStorage Ruby Gem v#{ConoStorage::VERSION}",
  "Content-Type" => "application/json"
}

Instance Method Summary collapse

Methods included from RequestMethods

#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.

Parameters:

  • web_mode (Boolean) (defaults to: false)

    true でコンテナやオブジェクトを作成/更新すると公開状態になります。

  • auth_url (String)
  • tenant_id (String)
  • username (String)
  • password (String)
  • endpoint (String)


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_paramsObject



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

#authorizeObject



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

#connectionObject



107
108
109
110
111
112
# 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_tokenObject



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

#default_headersObject



85
86
87
88
89
90
91
92
93
# 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) ⇒ 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, headers)
  authorize
  Response.new(
    connection.send(
      request_method,
      @endpoint + path,
      params,
      headers.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, 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