484 lines
13 KiB
Dart
484 lines
13 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:get/get.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../../constant/api_key.dart';
|
|
import '../../constant/box_name.dart';
|
|
import '../../constant/links.dart';
|
|
import '../../env/env.dart';
|
|
import '../../main.dart';
|
|
import '../../print.dart';
|
|
|
|
class CRUD {
|
|
Future<dynamic> get({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(
|
|
url,
|
|
body: payload,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials.toString()))}',
|
|
},
|
|
);
|
|
// if (response.statusCode == 200) {
|
|
var jsonData = jsonDecode(response.body);
|
|
if (jsonData['status'] == 'success') {
|
|
return response.body;
|
|
}
|
|
|
|
return jsonData['status'];
|
|
}
|
|
// }
|
|
|
|
Future<dynamic> getAgoraToken({
|
|
required String channelName,
|
|
required String uid,
|
|
}) async {
|
|
var uid = box.read(BoxName.phone) ?? box.read(BoxName.phoneDriver);
|
|
var res = await http.get(
|
|
Uri.parse(
|
|
'https://repulsive-pig-rugby-shirt.cyclic.app/token?channelName=$channelName'),
|
|
headers: {'Authorization': 'Bearer ${AK.agoraAppCertificate}'});
|
|
|
|
if (res.statusCode == 200) {
|
|
var response = jsonDecode(res.body);
|
|
return response['token'];
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> getLlama({
|
|
required String link,
|
|
required String payload,
|
|
required String prompt,
|
|
}) async {
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization':
|
|
'Bearer LL-X5lJ0Px9CzKK0HTuVZ3u2u4v3tGWkImLTG7okGRk4t25zrsLqJ0qNoUzZ2x4ciPy'
|
|
// 'Authorization': 'Bearer ${Env.llamaKey}'
|
|
};
|
|
var data = json.encode({
|
|
"model": "Llama-3-70b-Inst-FW",
|
|
// "model": "llama-13b-chat",
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content":
|
|
"Extract the desired information from the following passage as json decoded like $prompt just in this:\n\n$payload"
|
|
}
|
|
],
|
|
"temperature": 0.9
|
|
});
|
|
var response = await http.post(
|
|
url,
|
|
body: data,
|
|
headers: headers,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return response.body;
|
|
}
|
|
return response.statusCode;
|
|
}
|
|
|
|
Future allMethodForAI(String prompt, driverID, imagePath) async {
|
|
// await ImageController().choosImage(linkPHP, imagePath);
|
|
Future.delayed(const Duration(seconds: 2));
|
|
var extractedString = await arabicTextExtractByVisionAndAI(
|
|
imagePath: imagePath, driverID: driverID);
|
|
var json = jsonDecode(extractedString);
|
|
var textValues = getAllTextValuesWithLineNumbers(json);
|
|
// List<String> textValues = getAllTextValues(json);
|
|
|
|
// await AI().geminiAiExtraction(prompt, textValues);
|
|
}
|
|
|
|
Map<String, List<Map<String, String>>> getAllTextValuesWithLineNumbers(
|
|
Map json) {
|
|
Map<String, List<Map<String, String>>> output = {};
|
|
int lineNumber = 1;
|
|
|
|
if (json.containsKey('regions')) {
|
|
List<dynamic> regions = json['regions'];
|
|
for (Map<String, dynamic> region in regions) {
|
|
if (region.containsKey('lines')) {
|
|
List<dynamic> lines = region['lines'];
|
|
List<Map<String, String>> linesWithText = [];
|
|
for (Map<String, dynamic> line in lines) {
|
|
if (line.containsKey('words')) {
|
|
List<dynamic> words = line['words'];
|
|
String lineText = "";
|
|
for (Map<String, dynamic> word in words) {
|
|
if (word.containsKey('text')) {
|
|
lineText += word['text'] + " ";
|
|
}
|
|
}
|
|
lineText = lineText.trim();
|
|
linesWithText.add(
|
|
{"line_number": lineNumber.toString(), "text": lineText});
|
|
lineNumber++;
|
|
}
|
|
}
|
|
output["region_${region.hashCode}"] = linesWithText;
|
|
}
|
|
}
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
// List<String> getAllTextValues(Map json) {
|
|
// List<String> textValues = [];
|
|
|
|
// if (json.containsKey('regions')) {
|
|
// List<dynamic> regions = json['regions'];
|
|
// for (Map<String, dynamic> region in regions) {
|
|
// if (region.containsKey('lines')) {
|
|
// List<dynamic> lines = region['lines'];
|
|
// for (Map<String, dynamic> line in lines) {
|
|
// if (line.containsKey('words')) {
|
|
// List<dynamic> words = line['words'];
|
|
// for (Map<String, dynamic> word in words) {
|
|
// if (word.containsKey('text')) {
|
|
// textValues.add(word['text']);
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// return textValues;
|
|
// }
|
|
|
|
Future<dynamic> arabicTextExtractByVisionAndAI({
|
|
required String imagePath,
|
|
required String driverID,
|
|
}) async {
|
|
var headers = {
|
|
'Content-Type': 'application/json',
|
|
'Ocp-Apim-Subscription-Key': AK.ocpApimSubscriptionKey
|
|
};
|
|
|
|
String imagePathFull =
|
|
'${AppLink.server}/card_image/$imagePath-$driverID.jpg';
|
|
Log.print('imagePathFull: $imagePathFull');
|
|
var request = http.Request(
|
|
'POST',
|
|
Uri.parse(
|
|
'https://eastus.api.cognitive.microsoft.com/computervision/imageanalysis:analyze?features=caption,read&model-version=latest&language=en&api-version=2024-02-01'));
|
|
request.body = json.encode({"url": imagePathFull});
|
|
Log.print('request.body: ${request.body}');
|
|
request.headers.addAll(headers);
|
|
Log.print('request.headers: ${request.headers}');
|
|
|
|
http.StreamedResponse response = await request.send();
|
|
|
|
if (response.statusCode == 200) {
|
|
return await response.stream.bytesToString();
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> getChatGPT({
|
|
required String link,
|
|
required String payload,
|
|
}) async {
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var headers = {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'Bearer ${Env.chatGPTkeySeferNew}'
|
|
};
|
|
var data = json.encode({
|
|
"model": "gpt-3.5-turbo",
|
|
"messages": [
|
|
{
|
|
"role": "user",
|
|
"content":
|
|
"Extract the desired information from the following passage as json decoded like vin,make,made,year,expiration_date,color,owner,registration_date just in this:\n\n$payload"
|
|
}
|
|
],
|
|
"temperature": 0.9
|
|
});
|
|
var response = await http.post(
|
|
url,
|
|
body: data,
|
|
headers: headers,
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
return response.body;
|
|
}
|
|
return response.statusCode;
|
|
}
|
|
|
|
Future<dynamic> postStripe({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
// String? secretKey = await storage.read(key: BoxName.secretKey);
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(
|
|
url,
|
|
body: payload,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
'Authorization': 'Bearer ${AK.secretKey}',
|
|
},
|
|
);
|
|
if (response.statusCode == 200) {
|
|
return response.body;
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> post({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
// String? basicAuthCredentials =
|
|
// await storage.read(key: BoxName.basicAuthCredentials);
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(
|
|
url,
|
|
body: payload,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
|
},
|
|
);
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
if (response.statusCode == 200) {
|
|
if (jsonData['status'] == 'success') {
|
|
return response.body;
|
|
} else {
|
|
return (jsonData['status']);
|
|
}
|
|
} else {
|
|
return response.statusCode;
|
|
}
|
|
}
|
|
|
|
Future<dynamic> kazumiSMS({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var headers = {'Content-Type': 'application/json'};
|
|
var request = http.Request('POST', url);
|
|
request.body = json.encode({
|
|
"username": "Sefer",
|
|
"password": AK.smsPasswordEgypt,
|
|
});
|
|
request.headers.addAll(headers);
|
|
|
|
http.StreamedResponse response = await request.send();
|
|
|
|
if (response.statusCode == 200) {
|
|
var responseBody = await response.stream.bytesToString();
|
|
var data = json.decode(responseBody);
|
|
return data;
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> postPayMob({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
// String? basicAuthCredentials =
|
|
// await storage.read(key: BoxName.basicAuthCredentials);
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(url,
|
|
body: payload, headers: {'Content-Type': 'application/json'});
|
|
|
|
var jsonData = jsonDecode(response.body);
|
|
if (response.statusCode == 200) {
|
|
if (jsonData['status'] == 'success') {
|
|
return response.body;
|
|
} else {
|
|
return (jsonData['status']);
|
|
}
|
|
} else {
|
|
return response.statusCode;
|
|
}
|
|
}
|
|
|
|
sendEmail(
|
|
String link,
|
|
Map<String, String>? payload,
|
|
) async {
|
|
var headers = {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
|
};
|
|
var request = http.Request('POST', Uri.parse(link));
|
|
request.bodyFields = payload!;
|
|
request.headers.addAll(headers);
|
|
|
|
http.StreamedResponse response = await request.send();
|
|
if (response.statusCode == 200) {
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> postFromDialogue({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
// String? basicAuthCredentials =
|
|
// await storage.read(key: BoxName.basicAuthCredentials);
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(
|
|
url,
|
|
body: payload,
|
|
headers: {
|
|
"Content-Type": "application/x-www-form-urlencoded",
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
|
},
|
|
);
|
|
|
|
if (response.body.isNotEmpty) {
|
|
var jsonData = jsonDecode(response.body);
|
|
if (response.statusCode == 200) {
|
|
if (jsonData['status'] == 'success') {
|
|
Get.back();
|
|
// Get.snackbar(
|
|
// jsonData['status'],
|
|
// jsonData['message'],
|
|
// );
|
|
|
|
return response.body;
|
|
}
|
|
}
|
|
return (jsonData['status']);
|
|
}
|
|
}
|
|
|
|
Future<void> sendVerificationRequest(String phoneNumber) async {
|
|
final accountSid = AK.accountSIDTwillo;
|
|
final authToken = AK.authTokenTwillo;
|
|
final verifySid = AK.twilloRecoveryCode;
|
|
|
|
final Uri verificationUri = Uri.parse(
|
|
'https://verify.twilio.com/v2/Services/$verifySid/Verifications');
|
|
|
|
// Send the verification request
|
|
final response = await http.post(
|
|
verificationUri,
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode('$accountSid:$authToken'))}',
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: {
|
|
'To': phoneNumber,
|
|
'Channel': 'sms',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 201) {
|
|
} else {}
|
|
|
|
// Prompt the user to enter the OTP
|
|
const otpCode = "123456"; // Replace with user input
|
|
|
|
// Check the verification code
|
|
final checkUri = Uri.parse(
|
|
'https://verify.twilio.com/v2/Services/$verifySid/VerificationCheck');
|
|
|
|
final checkResponse = await http.post(
|
|
checkUri,
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode('$accountSid:$authToken'))}',
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
},
|
|
body: {
|
|
'To': phoneNumber,
|
|
'Code': otpCode,
|
|
},
|
|
);
|
|
|
|
if (checkResponse.statusCode == 201) {
|
|
} else {}
|
|
}
|
|
|
|
Future<dynamic> getGoogleApi({
|
|
required String link,
|
|
Map<String, dynamic>? payload,
|
|
}) async {
|
|
var url = Uri.parse(
|
|
link,
|
|
);
|
|
var response = await http.post(
|
|
url,
|
|
body: payload,
|
|
);
|
|
var jsonData = jsonDecode(response.body);
|
|
if (jsonData['status'] == 'OK') {
|
|
return jsonData;
|
|
}
|
|
return (jsonData['status']);
|
|
}
|
|
|
|
Future<dynamic> update({
|
|
required String endpoint,
|
|
required Map<String, dynamic> data,
|
|
required String id,
|
|
}) async {
|
|
// String? basicAuthCredentials =
|
|
// await storage.read(key: BoxName.basicAuthCredentials);
|
|
var url = Uri.parse('$endpoint/$id');
|
|
var response = await http.put(
|
|
url,
|
|
body: json.encode(data),
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
|
},
|
|
);
|
|
return json.decode(response.body);
|
|
}
|
|
|
|
Future<dynamic> delete({
|
|
required String endpoint,
|
|
required String id,
|
|
}) async {
|
|
// String? basicAuthCredentials =
|
|
// await storage.read(key: BoxName.basicAuthCredentials);
|
|
var url = Uri.parse('$endpoint/$id');
|
|
var response = await http.delete(
|
|
url,
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AK.basicAuthCredentials))}',
|
|
},
|
|
);
|
|
return json.decode(response.body);
|
|
}
|
|
}
|