2022-07-25 14:06:55 +00:00
|
|
|
import 'package:graphql_flutter/graphql_flutter.dart';
|
2022-07-31 23:10:37 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-07-25 14:06:55 +00:00
|
|
|
|
|
|
|
abstract class ApiMap {
|
|
|
|
Future<GraphQLClient> getClient() async {
|
|
|
|
final httpLink = HttpLink(
|
|
|
|
'https://api.$rootAddress/graphql',
|
|
|
|
);
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
final String token = _getApiToken();
|
|
|
|
|
2022-07-25 14:06:55 +00:00
|
|
|
final Link graphQLLink = isWithToken
|
|
|
|
? AuthLink(
|
2022-08-24 05:35:49 +00:00
|
|
|
getToken: () async =>
|
|
|
|
customToken == '' ? 'Bearer $token' : customToken,
|
2022-07-25 14:06:55 +00:00
|
|
|
).concat(httpLink)
|
|
|
|
: httpLink;
|
|
|
|
|
|
|
|
return GraphQLClient(
|
|
|
|
cache: GraphQLCache(),
|
|
|
|
link: graphQLLink,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-08-24 05:35:49 +00:00
|
|
|
Future<GraphQLClient> getSubscriptionClient() async {
|
|
|
|
final String token = _getApiToken();
|
|
|
|
|
|
|
|
final WebSocketLink webSocketLink = WebSocketLink(
|
|
|
|
'ws://api.$rootAddress/graphql',
|
|
|
|
config: SocketClientConfig(
|
|
|
|
autoReconnect: true,
|
|
|
|
headers: token.isEmpty ? null : {'Authorization': 'Bearer $token'},
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
return GraphQLClient(
|
|
|
|
cache: GraphQLCache(),
|
|
|
|
link: webSocketLink,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
String _getApiToken() {
|
|
|
|
String token = '';
|
|
|
|
final serverDetails = getIt<ApiConfigModel>().serverDetails;
|
|
|
|
if (serverDetails != null) {
|
|
|
|
token = getIt<ApiConfigModel>().serverDetails!.apiToken;
|
|
|
|
}
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2022-07-25 14:06:55 +00:00
|
|
|
abstract final String? rootAddress;
|
|
|
|
abstract final bool hasLogger;
|
|
|
|
abstract final bool isWithToken;
|
2022-07-31 23:10:37 +00:00
|
|
|
abstract final String customToken;
|
2022-07-25 14:06:55 +00:00
|
|
|
}
|