1414import io .ably .lib .platform .Platform ;
1515import io .ably .lib .push .Push ;
1616import io .ably .lib .realtime .Connection ;
17+ import io .ably .lib .transport .Defaults ;
1718import io .ably .lib .types .AblyException ;
1819import io .ably .lib .types .AsyncHttpPaginatedResponse ;
1920import io .ably .lib .types .AsyncPaginatedResult ;
4445 */
4546public abstract class AblyBase implements AutoCloseable {
4647
48+ /**
49+ * Some REST endpoints (e.g., stats and batch) changed in protocol v3.
50+ * To preserve backward compatibility for those specific endpoints, we
51+ * explicitly request protocol v2 when calling them.
52+ * <p>
53+ * Use this only for legacy endpoints that must remain on v2; all other
54+ * calls should use the default protocol version.
55+ */
56+ private static final int LEGACY_API_PROTOCOL_V2 = 2 ;
57+
4758 public final ClientOptions options ;
4859 public final Http http ;
4960 public final HttpCore httpCore ;
@@ -249,7 +260,17 @@ public PaginatedResult<Stats> stats(Param[] params) throws AblyException {
249260 }
250261
251262 PaginatedResult <Stats > stats (Http http , Param [] params ) throws AblyException {
252- return new PaginatedQuery <>(http , "/stats" , HttpUtils .defaultAcceptHeaders (false ), params , StatsReader .statsResponseHandler ).get ();
263+ return new PaginatedQuery <>(
264+ http ,
265+ "/stats" ,
266+ // Stats api uses protocol v2 format for now
267+ Param .set (
268+ HttpUtils .defaultAcceptHeaders (false ),
269+ new Param (Defaults .ABLY_PROTOCOL_VERSION_HEADER , LEGACY_API_PROTOCOL_V2 )
270+ ),
271+ params ,
272+ StatsReader .statsResponseHandler
273+ ).get ();
253274 }
254275
255276 /**
@@ -276,8 +297,18 @@ public void statsAsync(Param[] params, Callback<AsyncPaginatedResult<Stats>> cal
276297 statsAsync (http , params , callback );
277298 }
278299
279- void statsAsync (Http http , Param [] params , Callback <AsyncPaginatedResult <Stats >> callback ) {
280- (new AsyncPaginatedQuery <Stats >(http , "/stats" , HttpUtils .defaultAcceptHeaders (false ), params , StatsReader .statsResponseHandler )).get (callback );
300+ void statsAsync (Http http , Param [] params , Callback <AsyncPaginatedResult <Stats >> callback ) {
301+ (new AsyncPaginatedQuery <Stats >(
302+ http ,
303+ "/stats" ,
304+ // Stats api uses protocol v2 format for now
305+ Param .set (
306+ HttpUtils .defaultAcceptHeaders (false ),
307+ new Param (Defaults .ABLY_PROTOCOL_VERSION_HEADER , LEGACY_API_PROTOCOL_V2 )
308+ ),
309+ params ,
310+ StatsReader .statsResponseHandler
311+ )).get (callback );
281312 }
282313
283314 /**
@@ -433,7 +464,12 @@ private Http.Request<PublishResponse[]> publishBatchImpl(final Message.Batch[] p
433464 public void execute (HttpScheduler http , final Callback <PublishResponse []> callback ) throws AblyException {
434465 HttpCore .RequestBody requestBody = options .useBinaryProtocol ? MessageSerializer .asMsgpackRequest (pubSpecs ) : MessageSerializer .asJSONRequest (pubSpecs );
435466 final Param [] params = options .addRequestIds ? Param .set (initialParams , Crypto .generateRandomRequestId ()) : initialParams ; // RSC7c
436- http .post ("/messages" , HttpUtils .defaultAcceptHeaders (options .useBinaryProtocol ), params , requestBody , new HttpCore .ResponseHandler <PublishResponse []>() {
467+ // This method uses an old batch format from protocol v2
468+ Param [] headers = Param .set (
469+ HttpUtils .defaultAcceptHeaders (options .useBinaryProtocol ),
470+ new Param (Defaults .ABLY_PROTOCOL_VERSION_HEADER , LEGACY_API_PROTOCOL_V2 )
471+ );
472+ http .post ("/messages" , headers , params , requestBody , new HttpCore .ResponseHandler <PublishResponse []>() {
437473 @ Override
438474 public PublishResponse [] handleResponse (HttpCore .Response response , ErrorInfo error ) throws AblyException {
439475 if (error != null && error .code != 40020 ) {
@@ -446,11 +482,6 @@ public PublishResponse[] handleResponse(HttpCore.Response response, ErrorInfo er
446482 });
447483 }
448484
449- /**
450- * Authentication token has changed. waitForResult is true if there is a need to
451- * wait for server response to auth request
452- */
453-
454485 /**
455486 * Override this method in AblyRealtime and pass updated token to ConnectionManager
456487 * @param token new token
0 commit comments