11

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.

amalBit
  • 12,041
  • 6
  • 77
  • 94

3 Answers3

8

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

Community
  • 1
  • 1
Hugo Gresse
  • 17,195
  • 9
  • 77
  • 119
6

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.

amalBit
  • 12,041
  • 6
  • 77
  • 94
1

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.

Tremelune
  • 352
  • 2
  • 11