ANDREAS_LATANOWSKY_CPD_1910877/lib/http_utils.dart

189 lines
5.6 KiB
Dart
Raw Normal View History

2023-11-21 19:48:58 +01:00
import 'dart:convert';
2024-01-10 23:39:33 +01:00
import 'dart:io';
import 'package:cpd_app/language_utils.dart';
2023-11-21 19:48:58 +01:00
import 'package:flutter/services.dart';
2024-01-10 23:39:33 +01:00
import 'package:fluttertoast/fluttertoast.dart';
2023-11-21 19:48:58 +01:00
import 'package:http/http.dart' as http;
2024-01-10 23:39:33 +01:00
import 'package:http/http.dart';
2023-11-21 19:48:58 +01:00
import 'package:http_parser/http_parser.dart';
2024-01-10 23:39:33 +01:00
import 'package:flutter/material.dart';
import 'package:connectivity/connectivity.dart';
import 'dart:async';
2023-11-21 19:48:58 +01:00
class HttpUtils {
var client = http.Client();
2024-01-10 23:39:33 +01:00
2024-01-02 13:53:21 +01:00
Future<String> checkLang(Uint8List imageBytes, String imageName) async {
2024-01-10 23:39:33 +01:00
var postUri = Uri.parse("http://130.61.27.201/upload");
2024-01-02 13:53:21 +01:00
2023-11-21 19:48:58 +01:00
http.MultipartRequest request = http.MultipartRequest("POST", postUri);
http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
'file',
imageBytes,
filename: 'lorem.png',
contentType: MediaType('image', 'png'),
);
request.files.add(multipartFile);
2024-01-10 23:39:33 +01:00
2023-11-21 19:48:58 +01:00
http.StreamedResponse response = await client.send(request);
http.Response finalResponse = await http.Response.fromStream(response);
var lang = "";
if (finalResponse.statusCode == 200) {
Map<String, dynamic> jsonData = jsonDecode(finalResponse.body);
lang = jsonData['language'];
} else {
throw ('Error: ${finalResponse.statusCode}');
}
return lang;
}
2024-01-10 23:39:33 +01:00
Future<String> extractTextKnownSource(
Uint8List imageBytes, String sourceLang) async {
var postUri = Uri.parse("http://130.61.27.201/extractWithKnownSource");
http.MultipartRequest request = http.MultipartRequest("POST", postUri);
http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
'file',
imageBytes,
filename: 'lorem.png',
contentType: MediaType('image', 'png'),
);
MultipartFile source = MultipartFile.fromString(
'sourceLang',
sourceLang,
);
request.files.add(multipartFile);
request.files.add(source);
http.StreamedResponse response = await client.send(request);
http.Response finalResponse = await http.Response.fromStream(response);
var lang = "";
if (finalResponse.statusCode == 200) {
lang = finalResponse.body.toString();
} else {
throw ('Error: ${finalResponse.statusCode}');
}
return lang;
}
Future<String> extractTextUnknownSource(Uint8List imageBytes) async {
var postUri = Uri.parse("http://130.61.27.201/extractWithoutKnownSource");
http.MultipartRequest request = http.MultipartRequest("POST", postUri);
http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
'file',
imageBytes,
filename: 'lorem.png',
contentType: MediaType('image', 'png'),
);
request.files.add(multipartFile);
http.StreamedResponse response = await client.send(request);
http.Response finalResponse = await http.Response.fromStream(response);
var lang = "";
if (finalResponse.statusCode == 200) {
lang = finalResponse.body.toString();
} else {
throw ('Error: ${finalResponse.statusCode}');
}
return lang;
}
Future<Uint8List> translateKnownSource(
Uint8List imageBytes, String sourceLang, String targetLang) async {
var url = Uri.parse("http://130.61.27.201/translKnownSource");
http.MultipartRequest request = http.MultipartRequest("POST", url);
http.MultipartFile multipartFile = http.MultipartFile.fromBytes(
'file',
imageBytes,
filename: 'lorem.png',
contentType: MediaType('image', 'png'),
);
MultipartFile source = MultipartFile.fromString(
'sourceLang',
LanguageUtils.translatorLanguages[sourceLang]!,
);
MultipartFile target = MultipartFile.fromString(
'targetLang',
LanguageUtils.translatorLanguages[targetLang]!,
);
request.files.add(multipartFile);
request.files.add(source);
request.files.add(target);
http.StreamedResponse response = await client.send(request);
http.Response finalResponse = await http.Response.fromStream(response);
Uint8List lang = Uint8List(0);
if (finalResponse.statusCode == 200) {
lang = Uint8List.fromList(finalResponse.bodyBytes);
} else {
throw ('Error: ${finalResponse.statusCode}');
}
return lang;
}
void showProgressBar(BuildContext context) {
showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return const Dialog(
backgroundColor: Color.fromARGB(0, 44, 44, 44),
elevation: 0,
child: Center(
child: CircularProgressIndicator(),
),
);
},
);
}
void hideProgressBar(BuildContext context) {
Navigator.pop(context);
}
Future<bool> internetConnectivityCheck() async {
if (Platform.isAndroid) {
var connectivityResult = await Connectivity().checkConnectivity();
return connectivityResult == ConnectivityResult.wifi ||
connectivityResult == ConnectivityResult.mobile;
} else {
final response = await http.get(Uri.parse('http://130.61.27.201'));
return response.statusCode == 200;
}
}
void triggerNoInetToast() {
Fluttertoast.showToast(
msg: "No internet connection",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
textColor: Colors.white,
fontSize: 16.0,
);
}
void triggerConnectedToast() {
Fluttertoast.showToast(
msg: "Connected",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 1,
backgroundColor: const Color.fromARGB(255, 161, 120, 17),
textColor: Colors.white,
fontSize: 16.0,
);
}
2023-11-21 19:48:58 +01:00
}