twilio / twilio-ruby

A Ruby gem for communicating with the Twilio API and generating TwiML
MIT License
1.35k stars 462 forks source link

Getting an error trying to send an SMS with a client configured with more than the default # of retries #585

Closed samuel-henry closed 2 years ago

samuel-henry commented 2 years ago

Issue Summary

Getting an error trying to send an SMS with a client configured with more than the default # of retries

Steps to Reproduce

  1. Create client with retry_limit option passed in
  2. Send an SMS

Code Snippet

twilio_client_options = {
    retry_limit: 3,
}
client = Twilio::REST::Client.new(account_sid, auth_token, twilio_client_options) 
client.messages.create(
            body: 'Foo',  # a well-formed message that works without passing in the twilio_client_options param
            messaging_service_sid: TWILIO_MESSAGING_SERVICE_ID,      # my service id
            to: '+1234567890' # dummy phone number for this issue
        ) 

Exception/Log

"bad URI(is not URI?): \"https://api.twilio.com/2010-04-01/Accounts/{:retry_limit=>3}/Messages.json\""
--

<br class="Apple-interchange-newline">

Technical details:

eshanholtz commented 2 years ago

Hi @samuel-henry,

Where are you seeing a retry_limit parameter? I don't believe this is a supported client option by default, and you will likely need to define and pass a custom HTTP client that has your desired retry limit configured.

samuel-henry commented 2 years ago

Hi Elise,

Thank you for taking a look at this

I saw it on https://www.rubydoc.info/gems/twilio-ruby/3.10.1/Twilio/REST/Client

We see that it's set to 1 by default:

API_VERSION = '2010-04-01' HTTP_HEADERS = { 'Accept' => 'application/json', 'Accept-Charset' => 'utf-8', 'User-Agent' => "twilio-ruby/#{Twilio::VERSION}", } DEFAULTS = { :host => 'api.twilio.com', :port => 443, :use_ssl => true, :ssl_verify_peer => true, :ssl_ca_file => File.dirname(__FILE__) + '/../../../conf/cacert.pem', :timeout => 30, :proxy_addr => nil, :proxy_port => nil, :proxy_user => nil, :proxy_pass => nil, :retry_limit => 1, }

But that you can pass in an option to override the default (https://www.rubydoc.info/gems/twilio-ruby/3.10.1/Twilio/REST/Client#initialize-instance_method):

:retry_limit => 1 "The number of times to retry a request that has failed before throwing an exception. Defaults to one."

Thank you for the link regarding a custom HTTP client

Thank you, Sam

samuel-henry commented 2 years ago

Hi Elise,

Just checking back on this.

Thank you! Sam

eshanholtz commented 2 years ago

Hi Sam,

The documentation you linked is for twilio-ruby version 3.10.1, whereas you stated you are using 5.48.0. If you look at the documentation for version 5.48.0 you'll see the initialization function does not accept a options object as the third argument, but rather an optional account SID. This is why the object you're passing ({:retry_limit=>3}) is being substituted for the account_sid (instead of the value you passed as the username parameter) in the URI the client builds.

twilio-ruby version 3.10.1 is no longer being actively supported.

You will need to define a custom HTTP client with a specified retry limit and pass it into the Client initializer. Please reference this documentation for guidance on how to implement a custom HTTP client and pass it to the Twilio Client initializer.

samuel-henry commented 2 years ago

Ah, thank you Elise

I'll close this out but can you please pass along the feature request or +1 the existing backlog item to re-add this option to make it simple to configure retry strategy without having to define a custom client?

Thank you Sam