Reputation: 12191
I would like to know how to create a RestAdapter that can switch between two endpoints. Currently in my app, the RestAdapter is created in the Application class(singleton). I am looking for a way to have different endpoints without actually creating multiple RestAdapter.
Upvotes: 11
Views: 5060
Reputation: 17919
Retrofit 1 calls Endpoint for each request (no cache), to you just need to extend Retrofit.Endpoint with some setter and pass this Endpoint when your creating the RestAdapter :
Endpoint mDynamicEndpoint = new DynamicEndpoint("http://firstdomain.fr");
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(mDynamicEndpoint)
.build();
mDynamicEndpoint.setBaseUrl("http://yourdomain.com");
Possible duplicate : Dynamic Paths in Retrofit
Upvotes: 8
Reputation: 362
You could have a map from endpoint to RestAdapter. You would wind up with one adapter for each domain. Not a great solution if you have a lot of endpoints.
I believe the DynamicEndpoint solution above could lead to race conditions if two requests to different endpoints were fired at the same time.
Upvotes: 1
Reputation: 12191
Endpoint is called for every request. If you want to switch on an algorithm you can implement your own (e.g., to do round-robin). Other than that, a single RestAdapter is tied to an Endpoint, you cannot control it on a per-method basis or anything. - Jake Wharton
So I created different rest adapters for the different endpoints I use in my app.
Upvotes: 6