Skip to content

Commit

Permalink
feat: Add Twilio::HTTP::Client#configure_connection (#559)
Browse files Browse the repository at this point in the history
Co-authored-by: Darwin Carrillo <darwin.carrillo@shopify.com>
Co-authored-by: Adam Archibald <adam.archibald@shopify.com>
Co-authored-by: Sam Bostock <sam.bostock@shopify.com>
  • Loading branch information
4 people authored Jun 25, 2021
1 parent ee8f8a7 commit ccc5965
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ message_sid = 'SMxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

To use a custom HTTP client with this helper library, please see the [Twilio documentation](https://www.twilio.com/docs/libraries/ruby/custom-http-clients).

To apply customizations such as middleware, you can use the `configure_connection` method like so:

```ruby
@client.http_client.configure_connection do |faraday|
faraday.use SomeMiddleware
end
```

### Handling Errors

```ruby
Expand Down
12 changes: 11 additions & 1 deletion lib/twilio-ruby/http/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,28 @@ def initialize(proxy_prot = nil, proxy_addr = nil, proxy_port = nil, proxy_user
@ssl_ca_file = ssl_ca_file
@timeout = timeout
@adapter = Faraday.default_adapter
@configure_connection_blocks = []
end

def configure_connection(&block)
raise ArgumentError, "#{__method__} must be given a block!" unless block_given?

@configure_connection_blocks << block
nil
end

def _request(request)
@connection = Faraday.new(url: request.host + ':' + request.port.to_s, ssl: { verify: true }) do |f|
f.options.params_encoder = Faraday::FlatParamsEncoder
f.request :url_encoded
f.adapter @adapter
f.headers = request.headers
f.basic_auth(request.auth[0], request.auth[1])
f.proxy = "#{@proxy_prot}://#{@proxy_auth}#{@proxy_path}" if @proxy_prot && @proxy_path
f.options.open_timeout = request.timeout || @timeout
f.options.timeout = request.timeout || @timeout

@configure_connection_blocks.each { |block| block.call(f) }
f.adapter @adapter
end

@last_request = request
Expand Down
23 changes: 23 additions & 0 deletions spec/http/http_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@
@client = Twilio::HTTP::Client.new
end

it 'should allow adding a request configuration block' do
@client = Twilio::HTTP::Client.new
@connection = Faraday::Connection.new

blocks_spy = spy('blocks')

@client.configure_connection do |f|
blocks_spy.first_block_called(f)
end

@client.configure_connection do |f|
blocks_spy.second_block_called(f)
end

expect(Faraday).to receive(:new).and_yield(@connection).and_return(@connection)
allow_any_instance_of(Faraday::Connection).to receive(:send).and_return(double('response', status: 301, body: {}, headers: {}))

@client.request('host', 'port', 'GET', 'url', nil, nil, {}, ['a', 'b'])

expect(blocks_spy).to have_received(:first_block_called).with(@connection)
expect(blocks_spy).to have_received(:second_block_called).with(@connection)
end

it 'should allow setting a global timeout' do
@client = Twilio::HTTP::Client.new(timeout: 10)
@connection = Faraday::Connection.new
Expand Down

0 comments on commit ccc5965

Please sign in to comment.