32 lines
893 B
Dart
32 lines
893 B
Dart
import 'dart:io';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:dio/io.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
Future<Dio> createEspHttpClient({String token = ""}) async {
|
|
Dio dio = Dio();
|
|
|
|
final certData = await rootBundle.load("assets/certificates/rootCA.crt");
|
|
final certBytes = certData.buffer.asUint8List();
|
|
|
|
dio.httpClientAdapter = IOHttpClientAdapter(
|
|
createHttpClient: () {
|
|
SecurityContext securityContext = SecurityContext.defaultContext;
|
|
securityContext.setTrustedCertificatesBytes(certBytes);
|
|
|
|
return HttpClient(context: securityContext);
|
|
}
|
|
);
|
|
|
|
if (token.isNotEmpty) {
|
|
dio.interceptors.add(InterceptorsWrapper(onRequest: (RequestOptions options, RequestInterceptorHandler handler) {
|
|
options.headers.addAll({
|
|
"Authorization": "Bearer $token"
|
|
});
|
|
return handler.next(options);
|
|
}));
|
|
}
|
|
|
|
return dio;
|
|
} |