v1.26 ios v2
This commit is contained in:
+32
-24
@@ -5,6 +5,7 @@ import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:aitrainer_app/util/not_found_exception.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:sentry_flutter/sentry_flutter.dart';
|
||||
|
||||
class APIClient with Common, Logging {
|
||||
static final APIClient _singleton = APIClient._internal();
|
||||
@@ -45,12 +46,13 @@ class APIClient with Common, Logging {
|
||||
HttpClientResponse result = await request.close();
|
||||
client.close();
|
||||
if (result.statusCode != 200) {
|
||||
trace("authentication response: ${result.statusCode}");
|
||||
throw Exception("Network error, try again later!");
|
||||
trace("authentication response: ${result.statusCode} with URL: $url");
|
||||
throw Exception("Authentication error: ${result.statusCode}");
|
||||
}
|
||||
return jsonDecode(await result.transform(utf8.decoder).join());
|
||||
} catch (exception) {
|
||||
print(exception.toString());
|
||||
await Sentry.captureException(exception);
|
||||
throw Exception("Network error, try again later!");
|
||||
}
|
||||
}
|
||||
@@ -87,37 +89,43 @@ class APIClient with Common, Logging {
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
print("Post Exception: $e");
|
||||
await Sentry.captureException(e);
|
||||
throw Exception("Network Error, please try again later");
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> get(String endPoint, String param) async {
|
||||
final url = Cache().getBaseUrl() + endPoint + param;
|
||||
try {
|
||||
trace("-------- API get " + url);
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.length == 0) {
|
||||
var responseJson = await this.authenticateUser(Cache.username, Cache.password);
|
||||
authToken = responseJson['token'];
|
||||
Cache().authToken = authToken;
|
||||
}
|
||||
var uri = Uri.parse(url);
|
||||
|
||||
trace("-------- API get " + url);
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.length == 0) {
|
||||
var responseJson = await this.authenticateUser(Cache.username, Cache.password);
|
||||
authToken = responseJson['token'];
|
||||
Cache().authToken = authToken;
|
||||
}
|
||||
var uri = Uri.parse(url);
|
||||
HttpClient client = new HttpClient();
|
||||
|
||||
HttpClient client = new HttpClient();
|
||||
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
|
||||
|
||||
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
|
||||
|
||||
final HttpClientRequest request = await client.getUrl(uri);
|
||||
request.headers.set('Content-Type', 'application/json');
|
||||
request.headers.set('Authorization', 'Bearer $authToken');
|
||||
HttpClientResponse result = await request.close();
|
||||
client.close();
|
||||
trace(" ------------get response code: " + result.statusCode.toString());
|
||||
if (result.statusCode == 200) {
|
||||
return await result.transform(utf8.decoder).join();
|
||||
} else if (result.statusCode == 404) {
|
||||
throw NotFoundException(message: "Not Found");
|
||||
} else {
|
||||
final HttpClientRequest request = await client.getUrl(uri);
|
||||
request.headers.set('Content-Type', 'application/json');
|
||||
request.headers.set('Authorization', 'Bearer $authToken');
|
||||
HttpClientResponse result = await request.close();
|
||||
client.close();
|
||||
trace(" ------------get response code: " + result.statusCode.toString());
|
||||
if (result.statusCode == 200) {
|
||||
return await result.transform(utf8.decoder).join();
|
||||
} else if (result.statusCode == 404) {
|
||||
throw NotFoundException(message: "Not Found");
|
||||
} else {
|
||||
throw Exception("Network Error, please try again later");
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
print("Post Exception: $e");
|
||||
await Sentry.captureException(e);
|
||||
throw Exception("Network Error, please try again later");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,10 +62,10 @@ class FirebaseApi with logging.Logging {
|
||||
badge: true,
|
||||
sound: true,
|
||||
);
|
||||
this.firebaseRegToken = await FirebaseMessaging.instance.getToken();
|
||||
this.firebaseRegToken = await FirebaseMessaging.instance.getToken();
|
||||
Cache().firebaseMessageToken = firebaseRegToken;
|
||||
log("FirebaseMessaging token $firebaseRegToken");
|
||||
|
||||
|
||||
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
|
||||
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
||||
print('Got a message whilst in the foreground!');
|
||||
@@ -75,7 +75,6 @@ class FirebaseApi with logging.Logging {
|
||||
print('Message also contained a notification: ${message.notification}');
|
||||
}
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
// Set `_error` state to true if Firebase initialization fails
|
||||
log("Error initializing Firebase");
|
||||
@@ -381,11 +380,10 @@ class FirebaseApi with logging.Logging {
|
||||
|
||||
Future<void> setupRemoteConfig() async {
|
||||
//initializeFlutterFire();
|
||||
RemoteConfig? remoteConfig;
|
||||
FirebaseRemoteConfig? remoteConfig;
|
||||
try {
|
||||
remoteConfig = RemoteConfig.instance;
|
||||
await remoteConfig.setConfigSettings(
|
||||
RemoteConfigSettings(
|
||||
remoteConfig = FirebaseRemoteConfig.instance;
|
||||
await remoteConfig.setConfigSettings(RemoteConfigSettings(
|
||||
fetchTimeout: const Duration(seconds: 10),
|
||||
minimumFetchInterval: const Duration(seconds: 1),
|
||||
));
|
||||
|
||||
Reference in New Issue
Block a user