Skip to content

Commit 69dba2d

Browse files
authored
Accept client to Polly presigner (#2614)
1 parent d71e287 commit 69dba2d

File tree

3 files changed

+76
-34
lines changed

3 files changed

+76
-34
lines changed

gems/aws-sdk-polly/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Feature - Allow `Aws::Polly::Presigner` to take a `:client` with FIPS or Dualstack configuration.
5+
46
1.48.0 (2021-11-04)
57
------------------
68

@@ -315,4 +317,4 @@ Unreleased Changes
315317
1.0.0.rc2 (2016-12-09)
316318
------------------
317319

318-
* Feature - Initial release of `aws-sdk-polly`.
320+
* Feature - Initial release of `aws-sdk-polly`.

gems/aws-sdk-polly/lib/aws-sdk-polly/presigner.rb

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ module Polly
1818
#
1919
class Presigner
2020

21-
# @option options [required, Credentials] :credentials
22-
# You need provide an object that responds to `#credentials`
23-
# returning another object that responds to `#access_key_id`, `#secret_access_key`,
24-
# and `#session_token`.
21+
# @option options [Client] :client Optionally provide an existing Polly
22+
# client.
23+
#
24+
# @option options [Credentials] :credentials This option is deprecated,
25+
# please use :client instead. Provide an object that responds to
26+
# `#credentials` that returns another object that responds to
27+
# `#access_key_id`, `#secret_access_key`, and `#session_token`.
2528
#
2629
# For example, you could provide an instance of following classes:
2730
# * `Aws::Credentials`
@@ -30,35 +33,42 @@ class Presigner
3033
# * `Aws::AssumeRoleCredentials`
3134
# * `Aws::ECSCredentials`
3235
#
33-
# @option options [required, string] :region
34-
# The region name, e.g. 'us-west-2'
36+
# @option options [String] :region This option is deprecated, please use
37+
# :client instead. The region name, e.g. 'us-west-2'.
38+
#
3539
def initialize(options = {})
36-
@credentials = options.fetch(:credentials)
37-
@region = options.fetch(:region)
40+
if !(options.keys - [:credentials, :region, :client]).empty?
41+
raise ArgumentError,
42+
':options may only contain :client, :region, :credentials '\
43+
'keys. Please use the :client option instead.'
44+
end
45+
@client = options[:client] || Aws::Polly::Client.new(options)
3846
end
3947

40-
# @param [Hash] params parameter inputs for synthesize_speech operation
48+
# @param [Hash] params Parameter inputs for {Client#synthesize_speech}
49+
# operation.
4150
def synthesize_speech_presigned_url(params = {})
42-
input_shape = Client.api.operation(:synthesize_speech).input.shape
43-
sign_but_dont_send(input_shape, params)
44-
end
51+
req = @client.build_request(:synthesize_speech, params)
4552

46-
private
47-
48-
def sign_but_dont_send(input_shape, params)
4953
parts = []
50-
input_shape.members.each do |name, ref|
54+
req.context.operation.input.shape.members.each do |name, ref|
5155
parts << [ ref, params[name] ] unless params[name].nil?
5256
end
5357
query = Aws::Rest::Request::QuerystringBuilder.new.build(parts)
5458

5559
signer = Aws::Sigv4::Signer.new(
5660
service: 'polly',
57-
region: @region,
58-
credentials_provider: @credentials
61+
region: req.context.config.region,
62+
credentials_provider: req.context.config.credentials
63+
)
64+
65+
url = Aws::Partitions::EndpointProvider.resolve(
66+
signer.region, 'polly', 'regional',
67+
{
68+
dualstack: req.context.config.use_dualstack_endpoint,
69+
fips: req.context.config.use_fips_endpoint
70+
}
5971
)
60-
# TODO - use context so that dualstack can be used
61-
url = Aws::Partitions::EndpointProvider.resolve(signer.region, 'polly')
6272
url += "/v1/speech?#{query}"
6373
pre_signed_url = signer.presign_url(
6474
http_method: 'GET',

gems/aws-sdk-polly/spec/presigner_spec.rb

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,62 @@ module Polly
1515
let(:now) { double('now') }
1616
let(:utc) { double('utc-time') }
1717
let(:datetime) { '20160101T112233Z' }
18+
1819
let(:credentials) { Credentials.new('akid', 'secret') }
1920
let(:region) { 'us-west-2' }
21+
let(:client) { Client.new(region: region, credentials: credentials) }
2022

2123
describe '#initialize' do
2224

23-
it 'requires :credentials' do
24-
expect {
25-
Presigner.new(region: region)
26-
}.to raise_error(
27-
KeyError, /credentials/
28-
)
25+
it 'accepts :credentials and region (backwards compatible)' do
26+
expect do
27+
Presigner.new(region: region, credentials: credentials)
28+
end.to_not raise_error
29+
end
30+
31+
it 'accepts :client' do
32+
expect do
33+
Presigner.new(client: client)
34+
end.to_not raise_error
2935
end
3036

31-
it 'requries :region' do
32-
expect {
33-
Presigner.new(credentials: credentials)
34-
}.to raise_error(
35-
KeyError, /region/
36-
)
37+
it 'raises when provided options that are not supported' do
38+
expect do
39+
Presigner.new(foo: 'bar')
40+
end.to raise_error(ArgumentError, /use the :client option/)
3741
end
3842

3943
end
4044

4145
describe '#presigned_url' do
4246

43-
it 'can presign #synthesize_speech correctly' do
47+
it 'can presign #synthesize_speech using client' do
48+
expected_url =
49+
'https://polly.us-west-2.amazonaws.com/v1/speech?'\
50+
'LexiconNames=mno&LexiconNames=abc&OutputFormat=mp3&SampleRate=128&'\
51+
'Text=Hello%20World&TextType=text&VoiceId=Ewa&'\
52+
'X-Amz-Algorithm=AWS4-HMAC-SHA256&'\
53+
'X-Amz-Credential=akid%2F20160101%2F'\
54+
'us-west-2%2Fpolly%2Faws4_request&'\
55+
'X-Amz-Date=20160101T112233Z&X-Amz-Expires=900&'\
56+
'X-Amz-SignedHeaders=host&'\
57+
'X-Amz-Signature=acb554087d5e340223b3415c79618adeb5c2b6dba19ba91771fbbe1d50a78f3d'
58+
59+
pre = Presigner.new(client: client)
60+
params = {
61+
lexicon_names: [ 'mno', 'abc' ],
62+
output_format: 'mp3',
63+
sample_rate: '128',
64+
text: 'Hello World',
65+
text_type: 'text',
66+
voice_id: 'Ewa'
67+
}
68+
69+
actual_url = pre.synthesize_speech_presigned_url(params)
70+
expect(actual_url).to eq(expected_url)
71+
end
72+
73+
it 'can presign #synthesize_speech using region and credentials' do
4474
expected_url =
4575
'https://polly.us-west-2.amazonaws.com/v1/speech?'\
4676
'LexiconNames=mno&LexiconNames=abc&OutputFormat=mp3&SampleRate=128&'\

0 commit comments

Comments
 (0)