WT 1.1.14 SSL
This commit is contained in:
+10
-8
@@ -104,7 +104,9 @@ class Cache with Logging {
|
||||
static final String activeExercisePlanDateKey = "active_exercise_plan_date";
|
||||
static final String activeExercisePlanDetailsKey = "active_exercise_details_plan";
|
||||
|
||||
static String baseUrl = 'http://aitrainer.info:8888/api/';
|
||||
static String baseUrlLive = 'https://aitrainer.info:8943/api/';
|
||||
static String baseUrlTest = 'https://aitrainer.info:8843/api/';
|
||||
late String baseUrl;
|
||||
static final String mediaUrl = 'https://aitrainer.info:4343/media/';
|
||||
static final String username = 'bosi';
|
||||
static final String password = 'andio2009';
|
||||
@@ -171,7 +173,7 @@ class Cache with Logging {
|
||||
String testEnv = EnvironmentConfig.test_env;
|
||||
this.testEnvironment = testEnv;
|
||||
if (testEnv == "1") {
|
||||
baseUrl = 'http://aitrainer.app:8899/api/';
|
||||
baseUrl = baseUrlTest;
|
||||
liveServer = false;
|
||||
}
|
||||
|
||||
@@ -181,7 +183,7 @@ class Cache with Logging {
|
||||
}
|
||||
|
||||
void setTestBaseUrl() {
|
||||
baseUrl = 'http://aitrainer.app:8899/api/';
|
||||
baseUrl = baseUrlTest;
|
||||
}
|
||||
|
||||
String getAuthToken() {
|
||||
@@ -288,22 +290,22 @@ class Cache with Logging {
|
||||
|
||||
void setServerAddress(SharedPreferences prefs) {
|
||||
if (this.testEnvironment == "1") {
|
||||
baseUrl = 'http://aitrainer.app:8899/api/';
|
||||
baseUrl = baseUrlTest;
|
||||
print("TestEnv $baseUrl");
|
||||
return;
|
||||
}
|
||||
final bool? live = prefs.getBool(Cache.serverKey);
|
||||
if (live == null) {
|
||||
baseUrl = 'http://aitrainer.app:8888/api/';
|
||||
baseUrl = baseUrlLive;
|
||||
print("Live Env $baseUrl");
|
||||
liveServer = true;
|
||||
return;
|
||||
}
|
||||
liveServer = live;
|
||||
if (live) {
|
||||
baseUrl = 'http://aitrainer.info:8888/api/';
|
||||
baseUrl = baseUrlLive;
|
||||
} else {
|
||||
baseUrl = 'http://aitrainer.app:8899/api/';
|
||||
baseUrl = baseUrlTest;
|
||||
}
|
||||
|
||||
print("Env $baseUrl");
|
||||
@@ -331,7 +333,7 @@ class Cache with Logging {
|
||||
return prefs.getString(authTokenKey);
|
||||
}
|
||||
|
||||
static String getBaseUrl() {
|
||||
String getBaseUrl() {
|
||||
return baseUrl;
|
||||
}
|
||||
|
||||
|
||||
+113
-6
@@ -1,13 +1,120 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:aitrainer_app/model/result.dart';
|
||||
import 'package:aitrainer_app/service/logging.dart';
|
||||
import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:aitrainer_app/util/not_found_exception.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
|
||||
class APIClient with Common, Logging {
|
||||
Future<dynamic> sendRequestToServer(dynamic model, String reqType, bool isTokenHeader, String token) async {
|
||||
HttpClient client = new HttpClient();
|
||||
client.badCertificateCallback = ((X509Certificate cert, String host, int port) => true);
|
||||
HttpClientRequest request = await client.postUrl(Uri.parse(Cache().getBaseUrl() + reqType));
|
||||
request.headers.set('Content-Type', 'application/json');
|
||||
if (isTokenHeader) {
|
||||
request.headers.set('Authorization', 'Bearer $token');
|
||||
}
|
||||
request.add(utf8.encode(jsonEncode(model)));
|
||||
HttpClientResponse result = await request.close();
|
||||
if (result.statusCode == 200) {
|
||||
return jsonDecode(await result.transform(utf8.decoder).join());
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
dynamic authenticateUser(String email, String password) async {
|
||||
var url = Cache().getBaseUrl() + "authenticate";
|
||||
|
||||
try {
|
||||
ByteData data = await rootBundle.load('asset/data/aitrainer_server.crt.pem');
|
||||
SecurityContext context = SecurityContext.defaultContext;
|
||||
context.setTrustedCertificatesBytes(data.buffer.asUint8List(), password: "[xxxx]");
|
||||
|
||||
HttpClient client = new HttpClient(); //context: context Todo provide the right certificate
|
||||
client.badCertificateCallback = ((X509Certificate cert, String host, int port) {
|
||||
print("Host: $host Port: $port");
|
||||
return true;
|
||||
});
|
||||
var uri = Uri.parse(url);
|
||||
final HttpClientRequest request = await client.postUrl(uri);
|
||||
request.headers.set('Content-Type', 'application/json');
|
||||
request.headers.set('Authorization', '1');
|
||||
|
||||
final body = '{"username":"$email", "password":"$password"}';
|
||||
request.write(body);
|
||||
HttpClientResponse result = await request.close();
|
||||
if (result.statusCode != 200) {
|
||||
trace("authentication response: ${result.statusCode}");
|
||||
throw Exception("Network error, try again later!");
|
||||
}
|
||||
return jsonDecode(await result.transform(utf8.decoder).join());
|
||||
} catch (exception) {
|
||||
print(exception.toString());
|
||||
throw Exception("Network error, try again later!");
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> post(String endPoint, String body) async {
|
||||
final url = Cache().getBaseUrl() + endPoint;
|
||||
trace(" ------------ http/post body $body - url: $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();
|
||||
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');
|
||||
request.write(body);
|
||||
HttpClientResponse result = await request.close();
|
||||
trace(" ------------post 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");
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> get(String endPoint, String param) async {
|
||||
final url = Cache.getBaseUrl() + endPoint + param;
|
||||
final url = Cache().getBaseUrl() + endPoint + param;
|
||||
|
||||
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();
|
||||
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();
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> get0(String endPoint, String param) async {
|
||||
final url = Cache().getBaseUrl() + endPoint + param;
|
||||
|
||||
trace("-------- API get " + url);
|
||||
String authToken = Cache().getAuthToken();
|
||||
@@ -28,8 +135,8 @@ class APIClient with Common, Logging {
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> post(String endPoint, String body) async {
|
||||
final url = Cache.getBaseUrl() + endPoint;
|
||||
Future<String> post0(String endPoint, String body) async {
|
||||
final url = Cache().getBaseUrl() + endPoint;
|
||||
trace(" ------------ http/post body $body - url: $url ");
|
||||
String authToken = Cache().getAuthToken();
|
||||
if (authToken.length == 0) {
|
||||
@@ -48,8 +155,8 @@ class APIClient with Common, Logging {
|
||||
return decodedResponse;
|
||||
}
|
||||
|
||||
dynamic authenticateUser(String email, String password) async {
|
||||
var url = Cache.getBaseUrl() + "authenticate";
|
||||
dynamic authenticateUser0(String email, String password) async {
|
||||
var url = Cache().getBaseUrl() + "authenticate";
|
||||
|
||||
try {
|
||||
final body = '{"username":"$email", "password":"$password"}';
|
||||
@@ -74,7 +181,7 @@ class APIClient with Common, Logging {
|
||||
}
|
||||
|
||||
Future<String?> fetch(var authToken, var endPoint) async {
|
||||
var url = Cache.getBaseUrl() + endPoint;
|
||||
var url = Cache().getBaseUrl() + endPoint;
|
||||
|
||||
try {
|
||||
var uri = Uri.parse(url);
|
||||
|
||||
Reference in New Issue
Block a user