Skip to content

Commit

Permalink
[FVS-79] pass connection_id to every request (#475)
Browse files Browse the repository at this point in the history
  • Loading branch information
kanat committed Sep 15, 2023
1 parent a398028 commit 03f57e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,30 @@ const _idEvents = 1;
/// An accessor that allows us to communicate with the API around video calls.
class CoordinatorClientOpenApi extends CoordinatorClient {
CoordinatorClientOpenApi({
required String rpcUrl,
required this.rpcUrl,
required this.wsUrl,
required this.apiKey,
required this.tokenManager,
required this.latencyService,
required this.retryPolicy,
}) : _apiClient = open.ApiClient(
basePath: rpcUrl,
authentication:
_Authentication(apiKey: apiKey, tokenManager: tokenManager),
);
});

final _logger = taggedLogger(tag: 'SV:CoordClient');
final String apiKey;
final String rpcUrl;
final String wsUrl;
final String apiKey;
final TokenManager tokenManager;
final LatencyService latencyService;
final RetryPolicy retryPolicy;

final open.ApiClient _apiClient;
late final open.ApiClient _apiClient = open.ApiClient(
basePath: rpcUrl,
authentication: _Authentication(
apiKey: apiKey,
tokenManager: tokenManager,
getConnectionId: () => _ws?.connectionId,
),
);
late final defaultApi = open.DefaultApi(_apiClient);
late final serverSideApi = open.ServerSideApi(_apiClient);
late final locationService = LocationService();
Expand Down Expand Up @@ -302,7 +306,6 @@ class CoordinatorClientOpenApi extends CoordinatorClient {
ring: ringing,
location: location,
),
connectionId: _ws?.clientId,
);
if (result == null) {
return Result.error('joinCall result is null');
Expand Down Expand Up @@ -806,11 +809,18 @@ class CoordinatorClientOpenApi extends CoordinatorClient {
}
}

typedef GetConnectionId = String? Function();

class _Authentication extends open.Authentication {
_Authentication({required this.apiKey, required this.tokenManager});
_Authentication({
required this.apiKey,
required this.tokenManager,
required this.getConnectionId,
});

final String apiKey;
final TokenManager tokenManager;
final GetConnectionId getConnectionId;

@override
Future<void> applyToParams(
Expand All @@ -822,6 +832,10 @@ class _Authentication extends open.Authentication {
throw (tokenResult as Failure).error;
}
queryParams.add(open.QueryParam('api_key', apiKey));
final connectionId = getConnectionId();
if (connectionId != null) {
queryParams.add(open.QueryParam('connection_id', connectionId));
}
headerParams['Authorization'] = tokenResult.getDataOrNull()!.rawValue;
headerParams['stream-auth-type'] =
tokenResult.getDataOrNull()!.authType.name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import '../../logger/impl/tagged_logger.dart';
import '../../models/user_info.dart';
import '../../retry/retry_policy.dart';
import '../../shared_emitter.dart';
import '../../token/token.dart';
import '../../token/token_manager.dart';
import '../../types/other.dart';
import '../../utils/none.dart';
Expand Down Expand Up @@ -74,6 +73,7 @@ class CoordinatorWebSocketOpenApi extends CoordinatorWebSocket

String? userId;
String? clientId;
String? connectionId;

@override
Future<Result<None>> connect() {
Expand Down Expand Up @@ -177,6 +177,7 @@ class CoordinatorWebSocketOpenApi extends CoordinatorWebSocket
// resetting connection
userId = null;
clientId = null;
connectionId = null;

// check if we manually closed the connection
if (_manuallyClosed) {
Expand Down Expand Up @@ -243,6 +244,7 @@ class CoordinatorWebSocketOpenApi extends CoordinatorWebSocket
healthMonitor.onPongReceived();
userId ??= event.me.id;
clientId ??= event.connectionId;
connectionId ??= event.connectionId;
}

void _handleHealthCheckEvent(open.HealthCheckEvent event) {
Expand Down

0 comments on commit 03f57e7

Please sign in to comment.