Skip to content

Commit

Permalink
Merge pull request #74 from yaxia/dev
Browse files Browse the repository at this point in the history
Changes for 0.11.5-preview
  • Loading branch information
vinjiang committed Dec 28, 2016
2 parents ad98acb + 6a5b4b5 commit 6ec14d9
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 49 deletions.
6 changes: 6 additions & 0 deletions ChangeLog.md
@@ -1,3 +1,9 @@
2016.12 - version 0.11.5-preview

ALL
* Added the support for setting customer user agent. [#71](https://github.com/Azure/azure-storage-ruby/issues/71)
* Added the support for hooking in sending requests.

2016.11 - version 0.11.4-preview

ALL
Expand Down
13 changes: 13 additions & 0 deletions README.md
Expand Up @@ -239,6 +239,19 @@ queues.delete_queue("test-queue")

```

<a name="Customize the user-agent"></a>
## Customize the user-agent

You can customize the user-agent string by setting your user agent prefix when creating the service client.

```ruby
# Require the azure storage rubygem
require "azure/storage"

# Setup a specific instance of an Azure::Storage::Client with :user_agent_prefix option
client = Azure::Storage::Client.create(:storage_account_name => "your account name", :storage_access_key => "your access key", :user_agent_prefix => "your application name")
```

# Getting Started for Contributors

If you would like to become an active contributor to this project please follow the instructions provided in [Azure Projects Contribution Guidelines](http://azure.github.io/guidelines/).
Expand Down
2 changes: 1 addition & 1 deletion azure-storage.gemspec
Expand Up @@ -41,7 +41,7 @@ Gem::Specification.new do |s|
s.add_runtime_dependency('azure-core', '~> 0.1')
s.add_runtime_dependency('faraday', '~> 0.9')
s.add_runtime_dependency('faraday_middleware', '~> 0.10')
s.add_runtime_dependency('nokogiri', '~> 1.6')
s.add_runtime_dependency('nokogiri', '~> 1.6.0')

s.add_development_dependency('dotenv', '~> 2.0')
s.add_development_dependency('minitest', '~> 5')
Expand Down
4 changes: 2 additions & 2 deletions lib/azure/storage/blob/blob_service.rb
Expand Up @@ -38,10 +38,10 @@ class BlobService < StorageService
include Azure::Storage::Blob
include Azure::Storage::Blob::Container

def initialize(options = {})
def initialize(options = {}, &block)
client_config = options[:client] || Azure::Storage
signer = options[:signer] || Azure::Storage::Core::Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key)
super(signer, client_config.storage_account_name, options)
super(signer, client_config.storage_account_name, options, &block)
@host = client.storage_blob_host
end

Expand Down
34 changes: 20 additions & 14 deletions lib/azure/storage/client.rb
Expand Up @@ -27,6 +27,7 @@

require 'azure/storage/client_options'

require 'azure/storage/service/storage_service'
require 'azure/storage/blob/blob_service'
require 'azure/storage/table/table_service'
require 'azure/storage/queue/queue_service'
Expand Down Expand Up @@ -59,6 +60,7 @@ class Client
# * +:default_endpoints_protocol+ - String. http or https
# * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
# * +:ca_file+ - String. File path of the CA file if having issue with SSL
# * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
#
# The valid set of options inlcude:
# * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
Expand All @@ -76,19 +78,24 @@ class Client
# When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::ClientOptions.env_vars_mapping] for the mapping relationship
#
# @return [Azure::Storage::Client]
def initialize(options = {})
def initialize(options = {}, &block)
if options.is_a?(Hash)
options = setup_options if options.length == 0
options = parse_connection_string(options[:storage_connection_string]) if options[:storage_connection_string]
end

if options.has_key?(:user_agent_prefix)
Azure::Storage::Service::StorageService.user_agent_prefix = options[:user_agent_prefix]
options.delete :user_agent_prefix
end
end
Azure::Storage::Service::StorageService.register_request_callback &block if block_given?
reset!(options)
end

# Azure Blob service client configured from this Azure Storage client instance
# @return [Azure::Storage::Blob::BlobService]
def blob_client(options = {})
@blob_client ||= Azure::Storage::Blob::BlobService.new(default_client(options))
def blob_client(options = {}, &block)
@blob_client ||= Azure::Storage::Blob::BlobService.new(default_client(options), &block)
end

# Azure Queue service client configured from this Azure Storage client instance
Expand All @@ -104,7 +111,6 @@ def table_client(options = {})
end

class << self

# Public: Creates an instance of [Azure::Storage::Client]
#
# ==== Attributes
Expand All @@ -127,6 +133,7 @@ class << self
# * +:default_endpoints_protocol+ - String. http or https
# * +:use_path_style_uri+ - String. Whether use path style URI for specified endpoints
# * +:ca_file+ - String. File path of the CA file if having issue with SSL
# * +:user_agent_prefix+ - String. The user agent prefix that can identify the application calls the library
#
# The valid set of options inlcude:
# * Storage Emulator: +:use_development_storage+ required, +:development_storage_proxy_uri+ optionally
Expand All @@ -144,8 +151,8 @@ class << self
# When empty options are given, it will try to read settings from Environment Variables. Refer to [Azure::Storage::ClientOptions.env_vars_mapping] for the mapping relationship
#
# @return [Azure::Storage::Client]
def create(options={})
Client.new(options)
def create(options={}, &block)
Client.new(options, &block)
end

# Public: Creates an instance of [Azure::Storage::Client] with Storage Emulator
Expand All @@ -155,17 +162,16 @@ def create(options={})
# * +proxy_uri+ - String. Used with +:use_development_storage+ if emulator is hosted other than localhost.
#
# @return [Azure::Storage::Client]
def create_development(proxy_uri=nil)
def create_development(proxy_uri=nil, &block)
proxy_uri ||= StorageServiceClientConstants::DEV_STORE_URI
create(:use_development_storage => true, :development_storage_proxy_uri => proxy_uri)
create(:use_development_storage => true, :development_storage_proxy_uri => proxy_uri, &block)
end


# Public: Creates an instance of [Azure::Storage::Client] from Environment Variables
#
# @return [Azure::Storage::Client]
def create_from_env
create
def create_from_env(&block)
create &block
end

# Public: Creates an instance of [Azure::Storage::Client] from Environment Variables
Expand All @@ -175,8 +181,8 @@ def create_from_env
# * +connection_string+ - String. Please refer to https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/.
#
# @return [Azure::Storage::Client]
def create_from_connection_string(connection_string)
Client.new(connection_string)
def create_from_connection_string(connection_string, &block)
Client.new(connection_string, &block)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/azure/storage/queue/queue_service.rb
Expand Up @@ -30,10 +30,10 @@ module Queue
include Azure::Storage::Service
class QueueService < StorageService

def initialize(options = {})
def initialize(options = {}, &block)
client_config = options[:client] || Azure::Storage
signer = options[:signer] || Azure::Storage::Core::Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key)
super(signer, client_config.storage_account_name, options)
super(signer, client_config.storage_account_name, options, &block)
@host = @client.storage_queue_host
end

Expand Down
22 changes: 19 additions & 3 deletions lib/azure/storage/service/storage_service.rb
Expand Up @@ -33,10 +33,11 @@ class StorageService < Azure::Core::SignedService
# Create a new instance of the StorageService
#
# @param signer [Azure::Core::Auth::Signer] An implementation of Signer used for signing requests.
# (optional, Default=Azure::Storage::Auth::SharedKey.new)
# (optional, Default=Azure::Storage::Auth::SharedKey.new)
# @param account_name [String] The account name (optional, Default=Azure::Storage.storage_account_name)
# @param options [Azure::Storage::Configurable] the client configuration context
def initialize(signer=nil, account_name=nil, options = {})
def initialize(signer=nil, account_name=nil, options = {}, &block)
StorageService.register_request_callback &block if block_given?
options[:client] = Azure::Storage if options[:client] == nil
client_config = options[:client]
signer = signer || Azure::Storage::Core::Auth::SharedKey.new(
Expand Down Expand Up @@ -118,6 +119,20 @@ def generate_uri(path='', query={})
end

class << self
# @!attribute user_agent_prefix
# @return [Proc] Get or set the user agent prefix
attr_accessor :user_agent_prefix

# @!attribute request_callback
# @return [Proc] The callback before the request is signed and sent
attr_reader :request_callback

# Registers the callback when sending the request
# The headers in the request can be viewed or changed in the code block
def register_request_callback
@request_callback = Proc.new
end

# Adds metadata properties to header hash with required prefix
#
# metadata - A Hash of metadata name/value pairs
Expand Down Expand Up @@ -157,9 +172,10 @@ def with_value(object, key, value)
def common_headers(options = {})
headers = {
'x-ms-version' => Azure::Storage::Default::STG_VERSION,
'User-Agent' => Azure::Storage::Default::USER_AGENT
'User-Agent' => user_agent_prefix ? "#{user_agent_prefix}; #{Azure::Storage::Default::USER_AGENT}" : Azure::Storage::Default::USER_AGENT
}
headers.merge!({'x-ms-client-request-id' => options[:request_id]}) if options[:request_id]
@request_callback.call(headers) if @request_callback
headers
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/azure/storage/table/table_service.rb
Expand Up @@ -31,10 +31,10 @@ module Azure::Storage
module Table
class TableService < Service::StorageService

def initialize(options = {})
def initialize(options = {}, &block)
client_config = options[:client] || Azure::Storage
signer = options[:signer] || Auth::SharedKey.new(client_config.storage_account_name, client_config.storage_access_key)
super(signer, client_config.storage_account_name, options)
super(signer, client_config.storage_account_name, options, &block)
@host = client.storage_table_host
end

Expand Down
2 changes: 1 addition & 1 deletion lib/azure/storage/version.rb
Expand Up @@ -28,7 +28,7 @@ class Version
# Fields represent the parts defined in http://semver.org/
MAJOR = 0 unless defined? MAJOR
MINOR = 11 unless defined? MINOR
UPDATE = 4 unless defined? UPDATE
UPDATE = 5 unless defined? UPDATE
PRE = 'preview' unless defined? PRE

class << self
Expand Down
7 changes: 6 additions & 1 deletion test/integration/blob/container/container_metadata_test.rb
Expand Up @@ -25,7 +25,12 @@
require "azure/storage/blob/blob_service"

describe Azure::Storage::Blob::BlobService do
subject { Azure::Storage::Blob::BlobService.new }
let(:user_agent_prefix) { 'azure_storage_ruby_integration_test' }
subject {
Azure::Storage::Blob::BlobService.new { |headers|
headers['User-Agent'] = "#{user_agent_prefix}; #{headers['User-Agent']}"
}
}
after { ContainerNameHelper.clean }

describe '#set/get_container_metadata' do
Expand Down
7 changes: 6 additions & 1 deletion test/integration/queue/create_queue_test.rb
Expand Up @@ -26,7 +26,12 @@
require "azure/core/http/http_error"

describe Azure::Storage::Queue::QueueService do
subject { Azure::Storage::Queue::QueueService.new }
let(:user_agent_prefix) { 'azure_storage_ruby_integration_test' }
subject {
Azure::Storage::Queue::QueueService.new { |headers|
headers['User-Agent'] = "#{user_agent_prefix}; #{headers['User-Agent']}"
}
}

describe '#create_queue' do
let(:queue_name){ QueueNameHelper.name }
Expand Down
7 changes: 6 additions & 1 deletion test/integration/table/create_table_test.rb
Expand Up @@ -27,7 +27,12 @@

describe Azure::Storage::Table::TableService do
describe "#create_table" do
subject { Azure::Storage::Table::TableService.new }
let(:user_agent_prefix) { 'azure_storage_ruby_integration_test' }
subject {
Azure::Storage::Table::TableService.new { |headers|
headers['User-Agent'] = "#{user_agent_prefix}; #{headers['User-Agent']}"
}
}
let(:table_name){ TableNameHelper.name }
after { TableNameHelper.clean }

Expand Down

0 comments on commit 6ec14d9

Please sign in to comment.