amalBit
amalBit

Reputation: 12191

Retrofit - Multiple endpoints with same RestAdapter

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

Answers (3)

Hugo Gresse
Hugo Gresse

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

Tremelune
Tremelune

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

amalBit
amalBit

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

Related Questions