111 lines
3.3 KiB
Dart
111 lines
3.3 KiB
Dart
import 'dart:convert';
|
|
import 'package:aitrainer_app/util/common.dart';
|
|
import 'package:aitrainer_app/util/not_found_exception.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
|
|
class APIClient with Common {
|
|
|
|
Future<String> get(String endPoint, String param) async {
|
|
final url = Cache.getBaseUrl() + endPoint + param;
|
|
String authToken = Cache().getAuthToken();
|
|
if ( authToken.length == 0 ) {
|
|
var responseJson = await APIClient.authenticateUser(
|
|
Cache.username,
|
|
Cache.password
|
|
);
|
|
authToken = responseJson['token'];
|
|
}
|
|
final response = await http.get(url,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization' : "Bearer " + authToken }
|
|
);
|
|
print(" ------------get response code: " + response.statusCode.toString());
|
|
if(response.statusCode == 200) {
|
|
return utf8.decode(response.bodyBytes);
|
|
} else if(response.statusCode == 404 ) {
|
|
throw NotFoundException(message: "Not Found");
|
|
} else {
|
|
throw Exception("Network Error, please try again later");
|
|
}
|
|
}
|
|
|
|
Future<String> post(String endPoint, String body) async {
|
|
final url = Cache.getBaseUrl() + endPoint;
|
|
print(" ------------ http/post endpoint $endPoint body $body - url: $url ");
|
|
String authToken = Cache().getAuthToken();
|
|
if ( authToken.length == 0 ) {
|
|
var responseJson = await APIClient.authenticateUser(
|
|
Cache.username,
|
|
Cache.password
|
|
);
|
|
authToken = responseJson['token'];
|
|
}
|
|
|
|
final response = await http.post(url,
|
|
headers: {
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
|
'Authorization' : "Bearer " + authToken
|
|
},
|
|
body: body,
|
|
);
|
|
print(" ------------post response code: " + response.statusCode.toString());
|
|
final String decodedResponse = utf8convert(response.body);
|
|
print(" ------------ response: $decodedResponse");
|
|
return decodedResponse;
|
|
}
|
|
|
|
static dynamic authenticateUser(String email, String password) async {
|
|
var uri = Cache.getBaseUrl() + "authenticate";
|
|
|
|
try {
|
|
final body = '{"username":"$email", "password":"$password"}';
|
|
print("authentication with $email");
|
|
final response = await http.post(
|
|
uri,
|
|
headers: {
|
|
'Authorization': '1',
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: body
|
|
);
|
|
final responseCode = response.statusCode;
|
|
if ( responseCode != 200) {
|
|
return { "error" : "Authentication error, total failure", };
|
|
}
|
|
|
|
final responseJson = json.decode(response.body);
|
|
return responseJson;
|
|
|
|
} catch (exception) {
|
|
return { "error" : "Network error, try again later "
|
|
+ exception.toString()};
|
|
}
|
|
}
|
|
|
|
static fetch(var authToken, var endPoint) async {
|
|
var uri = Cache.getBaseUrl() + endPoint;
|
|
|
|
try {
|
|
final response = await http.get(
|
|
uri,
|
|
headers: {
|
|
'Authorization': authToken
|
|
},
|
|
);
|
|
|
|
final responseJson = json.decode(response.body);
|
|
return responseJson;
|
|
|
|
} catch (exception) {
|
|
print(exception);
|
|
if(exception.toString().contains('SocketException')) {
|
|
return 'NetworkError';
|
|
} else {
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
} |