Sha256: d37f67925c89654b8129d49dd748272832f0b3394bfa1811b19b40ec26eb3a4e

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

# Copyright (c) 2009-2012 VMware, Inc.

require "httpclient"

module Bosh
  module Blobstore
    class SimpleBlobstoreClient < BaseClient

      def initialize(options)
        super(options)
        @client = HTTPClient.new
        @endpoint = @options[:endpoint]
        @bucket = @options[:bucket] || "resources"
        @headers = {}
        user = @options[:user]
        password = @options[:password]
        if user && password
          @headers["Authorization"] = "Basic " +
            Base64.encode64("#{user}:#{password}").strip
        end
      end

      def url(id=nil)
        ["#{@endpoint}/#{@bucket}", id].compact.join("/")
      end

      def create_file(file)
        response = @client.post(url, {:content => file}, @headers)
        if response.status != 200
          raise BlobstoreError,
            "Could not create object, #{response.status}/#{response.content}"
        end
        response.content
      end

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

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

      def delete(id)
        response = @client.delete(url(id), @headers)
        if response.status != 204
          raise "Could not delete object, #{response.status}/#{response.content}"
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blobstore_client-0.5.0 lib/blobstore_client/simple_blobstore_client.rb
blobstore_client-0.4.0 lib/blobstore_client/simple_blobstore_client.rb