fixed msg sending and added error handler

This commit is contained in:
Kima
2023-12-23 00:05:12 +01:00
parent fdc6209656
commit f46610314d
5 changed files with 107 additions and 11 deletions

View File

@@ -156,6 +156,7 @@ class KretaClient {
if (res == null) throw "Login error";
if (json) {
print(jsonDecode(res.body));
return jsonDecode(res.body);
} else {
return res.body;
@@ -168,6 +169,69 @@ class KretaClient {
}
}
Future<dynamic> sendFilesAPI(
String url, {
Map<String, String>? headers,
bool autoHeader = true,
Map<String, String>? body,
}) async {
Map<String, String> headerMap;
if (headers != null) {
headerMap = headers;
} else {
headerMap = {};
}
try {
http.StreamedResponse? res;
for (int i = 0; i < 3; i++) {
if (autoHeader) {
if (!headerMap.containsKey("authorization") && accessToken != null) {
headerMap["authorization"] = "Bearer $accessToken";
}
if (!headerMap.containsKey("user-agent") && userAgent != null) {
headerMap["user-agent"] = "$userAgent";
}
if (!headerMap.containsKey("content-type")) {
headerMap["content-type"] = "multipart/form-data";
}
if (url.contains('kommunikacio/uzenetek')) {
headerMap["X-Uzenet-Lokalizacio"] = "hu-HU";
}
}
var request = http.MultipartRequest("POST", Uri.parse(url));
// request.files.add(value)
request.fields.addAll(body ?? {});
request.headers.addAll(headers ?? {});
res = await request.send();
if (res.statusCode == 401) {
await refreshLogin();
headerMap.remove("authorization");
} else {
break;
}
}
if (res == null) throw "Login error";
print(res.statusCode);
return res.statusCode;
} on http.ClientException catch (error) {
print(
"ERROR: KretaClient.postAPI ($url) ClientException: ${error.message}");
} catch (error) {
print("ERROR: KretaClient.postAPI ($url) ${error.runtimeType}: $error");
}
}
Future<void> refreshLogin() async {
if (_loginRefreshing) return;
_loginRefreshing = true;

View File

@@ -156,8 +156,9 @@ class SendRecipient {
});
factory SendRecipient.fromJson(Map json, SendRecipientType type) {
print(json);
return SendRecipient(
id: int.parse(json['oktatasiAzonosito'] ?? '0'),
id: int.parse(json['kretaAzonosito'] ?? '0'),
kretaId: json['kretaAzonosito'],
name: json['nev'],
type: type,
@@ -166,7 +167,7 @@ class SendRecipient {
}
Object get kretaJson => {
'azonosito': id ?? 0,
'azonosito': kretaId ?? 0,
'kretaAzonosito': kretaId ?? 0,
'nev': name ?? 'Teszt Lajos',
'tipus': {

View File

@@ -1,5 +1,5 @@
// ignore_for_file: use_build_context_synchronously
import 'dart:convert';
import 'dart:math';
import 'package:filcnaplo/api/providers/user_provider.dart';
@@ -209,7 +209,7 @@ class MessageProvider with ChangeNotifier {
}
// send message
Future<void> sendMessage({
Future<String?> sendMessage({
required List<SendRecipient> recipients,
String subject = "Nincs tárgy",
required String messageText,
@@ -235,20 +235,34 @@ class MessageProvider with ChangeNotifier {
// }
recipientList.addAll(recipients.map((e) => e.kretaJson));
Object body = {
Map body = {
"cimzettLista": recipientList,
"csatolmanyok": [],
"azonosito": Random().nextInt(10000) + 10000,
"azonosito": (Random().nextInt(10000) + 10000),
"feladoNev": user.name,
"feladoTitulus": user.role == Role.parent ? "Szülő" : "Diák",
"kuldesDatum": DateTime.now().toIso8601String(),
"targy": subject,
"szoveg": messageText,
"elozoUzenetAzonosito": 0,
// "elozoUzenetAzonosito": 0,
};
// send the message
await Provider.of<KretaClient>(_context, listen: false)
.postAPI(KretaAPI.sendMessage, autoHeader: true, body: body);
Map<String, String> headers = {
"content-type": "application/json",
};
var res = await Provider.of<KretaClient>(_context, listen: false).postAPI(
KretaAPI.sendMessage,
autoHeader: true,
json: true,
body: json.encode(body),
headers: headers,
);
if (res!['hibakod'] == 'UzenetKuldesEngedelyRule') {
return 'send_permission_error';
}
return 'successfully_sent';
}
}