twilio / twilio-java

A Java library for communicating with the Twilio REST API and generating TwiML.
MIT License
484 stars 425 forks source link

How does the Twilio SDK manage multiple rest clients from multiple "Twilio Connect" customers? #656

Closed perrytew closed 2 years ago

perrytew commented 2 years ago

I'm developing an integration with Twilio using your Twilio Connect.
I've been slowed by out of date documentation here: https://www.twilio.com/docs/iam/connect/quickstart/java

I just realized that all the examples elsewhere are now using the Twilio object, and that the TwilioRestClient is no longer a top level object.

I see that the Twilio object has a static volatile rest client.
private static volatile TwilioRestClient restClient; I don't think that's usable within our web server where the same thread may service multiple accounts during its lifetime.

How does the Twilio sdk manage multiple "Twilio Connect" clients without the authorizations stomping each other? Again, I'm running a multi-threaded web server and I cannot manage clients by threads.

I quickly reviewed all the other sdks (python, C#, ruby), and they all return back a client object from their init methods. Why does the java sdk not do this?

Please advise.

perrytew commented 2 years ago

I found this. I think that's my solution.

`Using a Different Client

TwilioRestClient client = new TwilioRestClient.Builder(accountSid, authToken).build();

Message message = Message.creator( new PhoneNumber("+15558881234"), // To number new PhoneNumber("+15559994321"), // From number "Hello world!" // SMS body ).create(client); // Pass the client here

System.out.println(message.getSid());`