-
Notifications
You must be signed in to change notification settings - Fork 7
User Agent #310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
User Agent #310
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3fe7d4b
Add user agent plugin and test
richardwang1124 ce6a7c0
Reformat header
richardwang1124 90a5dfe
Reformat
richardwang1124 600a3d7
Merge branch 'decaf' into feature/user-agent
richardwang1124 70d7421
Fix weld plugins after merge
richardwang1124 d5ce19b
Add user agent suffix
richardwang1124 fcd5ab7
PR comments
richardwang1124 9214605
Change doc type
richardwang1124 2ac88b2
Fix docstring
richardwang1124 d79939b
Update docstring
richardwang1124 e5cb6ab
Add feature id tracking
richardwang1124 2277c36
Regenerate projections
richardwang1124 130930a
Rubocop
richardwang1124 92431f8
Revert with_metrics
richardwang1124 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
gems/smithy-client/lib/smithy-client/plugins/user_agent.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# frozen_string_literal: true | ||
|
||
module Smithy | ||
module Client | ||
module Plugins | ||
# @api private | ||
class UserAgent < Plugin | ||
option( | ||
:user_agent_suffix, | ||
doc_type: String, | ||
docstring: <<~DOCS) | ||
An optional string that is appended to the User-Agent header. | ||
The default User-Agent includes the smithy-client version, | ||
the ruby platform and version, and host OS information. | ||
DOCS | ||
|
||
# @api private | ||
class Handler < Client::Handler | ||
def call(context) | ||
context.http_request.headers['User-Agent'] = UserAgent.new(context).to_s | ||
@handler.call(context) | ||
end | ||
|
||
# @api private | ||
class UserAgent | ||
def initialize(context) | ||
@context = context | ||
end | ||
|
||
def to_s | ||
ua = "smithy-ruby/#{Smithy::Client::VERSION}" | ||
ua += " (#{os_metadata};" | ||
ua += " #{language_metadata})" | ||
ua += " #{@context.config.user_agent_suffix}" if @context.config.user_agent_suffix | ||
ua.strip | ||
end | ||
|
||
private | ||
|
||
def os_metadata | ||
os = | ||
case RbConfig::CONFIG['host_os'] | ||
when /mac|darwin/ | ||
'macos' | ||
when /linux|cygwin/ | ||
'linux' | ||
when /mingw|mswin/ | ||
'windows' | ||
else | ||
'other' | ||
end | ||
metadata = os.to_s | ||
local_version = Gem::Platform.local.version | ||
metadata += " #{local_version}" if local_version | ||
metadata + "; #{RbConfig::CONFIG['host_cpu']}" | ||
end | ||
|
||
def language_metadata | ||
"ruby/#{RUBY_VERSION}" | ||
end | ||
end | ||
end | ||
|
||
handler(Handler, step: :sign, priority: 5) | ||
end | ||
end | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
gems/smithy-client/spec/smithy-client/plugins/user_agent_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../../spec_helper' | ||
|
||
module Smithy | ||
module Client | ||
module Plugins | ||
describe UserAgent do | ||
let(:client_class) { ClientHelper.sample_client.const_get(:Client) } | ||
let(:client) { client_class.new(stub_responses: true) } | ||
|
||
it 'adds a user agent header to request' do | ||
resp = client.operation | ||
ua_header = resp.context.http_request.headers['user-agent'] | ||
expect(ua_header).to_not be_nil | ||
richardwang1124 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expect(ua_header).to include('smithy-ruby') | ||
expect(ua_header).to include('macos').or include('linux').or include('windows').or include('other') | ||
expect(ua_header).to include('ruby') | ||
end | ||
|
||
it 'adds a user agent suffix to user agent string when configured' do | ||
client = client_class.new(user_agent_suffix: 'test-suffix', stub_responses: true) | ||
resp = client.operation | ||
ua_header = resp.context.http_request.headers['user-agent'] | ||
expect(ua_header).to end_with('test-suffix') | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.