Class: Bosh::Blobstore::DavBlobstoreClient

Inherits:
BaseClient show all
Defined in:
lib/blobstore_client/dav_blobstore_client.rb

Constant Summary

Constants inherited from Client

Client::PROVIDER_NAMES, Client::VERSION

Instance Method Summary collapse

Methods inherited from BaseClient

#create, #delete, #exists?, #get

Methods inherited from Client

create, safe_create

Constructor Details

#initialize(options) ⇒ DavBlobstoreClient

Returns a new instance of DavBlobstoreClient.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 11

def initialize(options)
  super(options)
  @client = HTTPClient.new

  # https://www.youtube.com/watch?v=4xgx4k83zzc
  @client.send_timeout = @client.send_timeout * 11

  if @options[:ssl_no_verify]
    @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @client.ssl_config.verify_callback = proc {}
  end

  @endpoint = @options[:endpoint]
  @headers = {}
  user = @options[:user]
  password = @options[:password]
  if user && password
    @headers['Authorization'] = 'Basic ' +
      Base64.strict_encode64("#{user}:#{password}").strip
  end
end

Instance Method Details

#create_file(id, file) ⇒ Object

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 39

def create_file(id, file)
  id ||= generate_object_id

  response = @client.put(url(id), file, @headers)

  raise BlobstoreError, "Could not create object, #{response.status}/#{response.content}" if response.status != 201

  id
end

#delete_object(id) ⇒ Object

Raises:



57
58
59
60
61
62
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 57

def delete_object(id)
  response = @client.delete(url(id), header: @headers)

  raise NotFound, "Object '#{id}' is not found, #{response.status}/#{response.content}" if response.status == 404
  raise BlobstoreError, "Could not delete object, #{response.status}/#{response.content}" if response.status != 204
end

#get_file(id, file) ⇒ Object

Raises:



49
50
51
52
53
54
55
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 49

def get_file(id, file)
  response = @client.get(url(id), {}, @headers) do |block|
    file.write(block)
  end

  raise BlobstoreError, "Could not fetch object, #{response.status}/#{response.content}" if response.status != 200
end

#object_exists?(id) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
67
68
69
70
71
72
73
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 64

def object_exists?(id)
  response = @client.head(url(id), header: @headers)
  if response.status == 200
    true
  elsif response.status == 404
    false
  else
    raise BlobstoreError, "Could not get object existence, #{response.status}/#{response.content}"
  end
end

#url(id) ⇒ Object



33
34
35
36
37
# File 'lib/blobstore_client/dav_blobstore_client.rb', line 33

def url(id)
  prefix = Digest::SHA1.hexdigest(id)[0, 2]

  [@endpoint, prefix, id].compact.join('/')
end