Files
doormile_milderapp/lib/xpress/providers/deliverylog/deliverylog_provider.dart
2026-08-28 18:16:28 +05:30

207 lines
7.2 KiB
Dart

import 'dart:async';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:miler/data/api_config.dart';
import 'package:flutter/foundation.dart';
// Combined Deliverylog providers:
/// ── The SSL bypass is gone ──
///
/// This file built its own `IOClient` with `onBadCertificate: (_) => true` and
/// a hand-rolled `SecureSocket` upgrade, so **every certificate was accepted**
/// — a delivery rider's position, his order ids and his session were readable
/// and rewritable by anything on the path. The parcel line deleted the same
/// code (see `providers/Riderlog/riderlog_provider.dart`); the delivery line
/// kept it because nothing in `lib/` imported this file directly.
///
/// It also sent **no `Authorization` header**. The URLs it posts to are v1
/// routes on `api.doormile.com` — resolved through the [DeliveryApi] facade —
/// so every one of these calls could only ever have been answered with a 401.
/// A plain client plus [ApiConfig.authHeaders] fixes both at once.
Future<Map<String, String>> _headers() => ApiConfig.authHeaders();
class CreateDeliveryLogProvider {
Future<Map<String, dynamic>?> createDeliveryLog(
String urldata,
Map<String, dynamic> data, {
bool wrapInArray = true,
}) async {
Map<String, dynamic>? result;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final body = json.encode(wrapInArray ? [data] : data);
final response = await client
.post(url, body: body, headers: await _headers())
.timeout(const Duration(seconds: 10));
debugPrint('createDeliveryLog url $urldata');
debugPrint(body);
debugPrint('createDeliveryLog response ${response.body}');
if (response.statusCode >= 200 && response.statusCode < 300) {
result = json.decode(response.body.toString()) as Map<String, dynamic>;
debugPrint('createDeliveryLog parsed ${result.toString()}');
} else {
debugPrint('createDeliveryLog failed: HTTP ${response.statusCode}');
}
} on TimeoutException catch (e) {
debugPrint('createDeliveryLog timeout: $e');
} catch (e) {
debugPrint('createDeliveryLog error: $e');
} finally {
client.close();
}
return result;
}
}
class UpdateDeliveryProvider {
Future<Map<String, dynamic>?> updateDelivery(
Map<String, dynamic> data,
String urldata,
) async {
Map<String, dynamic>? updateDeliveryResponse;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final response = await client
.put(url, body: json.encode(data), headers: await _headers())
.timeout(const Duration(seconds: 10));
debugPrint('updateDelivery url $urldata');
debugPrint('updateDelivery status ${response.statusCode}');
debugPrint('updateDelivery response ${response.body}');
debugPrint(json.encode(data));
if (response.statusCode >= 200 && response.statusCode < 300) {
updateDeliveryResponse =
json.decode(response.body) as Map<String, dynamic>;
} else {
debugPrint('updateDelivery failed: HTTP ${response.statusCode}');
}
} on TimeoutException catch (e) {
debugPrint('updateDelivery timeout: $e');
} catch (e) {
debugPrint('updateDelivery error: $e');
} finally {
client.close();
}
return updateDeliveryResponse;
}
Future<Map<String, dynamic>?> updateArrivedDelivery(
Map<String, dynamic> data,
String urldata,
) async {
Map<String, dynamic>? updateDeliveryResponse;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final response = await client
.put(url, body: json.encode(data), headers: await _headers())
.timeout(const Duration(seconds: 10));
debugPrint('updateArrived url $urldata');
debugPrint('updateArrived status ${response.statusCode}');
debugPrint(json.encode(data));
if (response.statusCode >= 200 && response.statusCode < 300) {
updateDeliveryResponse =
json.decode(response.body) as Map<String, dynamic>;
} else {
debugPrint('updateArrived failed: HTTP ${response.statusCode}');
}
} on TimeoutException catch (e) {
debugPrint('updateArrived timeout: $e');
} catch (e) {
debugPrint('updateArrived error: $e');
} finally {
client.close();
}
return updateDeliveryResponse;
}
Future<Map<String, dynamic>?> updatePickedDelivery(
Map<String, dynamic> data,
String urldata,
) async {
Map<String, dynamic>? updateDeliveryResponse;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final response = await client
.put(url, body: json.encode(data), headers: await _headers())
.timeout(const Duration(seconds: 10));
debugPrint('updatePicked url $urldata');
debugPrint('updatePicked status ${response.statusCode}');
debugPrint(json.encode(data));
if (response.statusCode >= 200 && response.statusCode < 300) {
updateDeliveryResponse =
json.decode(response.body) as Map<String, dynamic>;
} else {
debugPrint('updatePicked failed: HTTP ${response.statusCode}');
}
} on TimeoutException catch (e) {
debugPrint('updatePicked timeout: $e');
} catch (e) {
debugPrint('updatePicked error: $e');
} finally {
client.close();
}
return updateDeliveryResponse;
}
Future<Map<String, dynamic>?> updateActiveDelivery(
Map<String, dynamic> data,
String urldata,
) async {
Map<String, dynamic>? updateDeliveryResponse;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final response = await client
.put(url, body: json.encode(data), headers: await _headers())
.timeout(const Duration(seconds: 10));
debugPrint('updateActive url $urldata');
debugPrint('updateActive status ${response.statusCode}');
debugPrint(json.encode(data));
if (response.statusCode >= 200 && response.statusCode < 300) {
updateDeliveryResponse =
json.decode(response.body) as Map<String, dynamic>;
} else {
debugPrint('updateActive failed: HTTP ${response.statusCode}');
}
} on TimeoutException catch (e) {
debugPrint('updateActive timeout: $e');
} catch (e) {
debugPrint('updateActive error: $e');
} finally {
client.close();
}
return updateDeliveryResponse;
}
}
class GetDeliveryLogProvider {
Future<Map<String, dynamic>?> getDeliveryLog(String urldata) async {
Map<String, dynamic>? result;
final client = http.Client();
try {
final url = Uri.parse(urldata);
final response = await client.get(
url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
);
debugPrint('getDeliveryLog url $urldata');
debugPrint('getDeliveryLog response ${response.body}');
if (response.statusCode >= 200 && response.statusCode < 300) {
result = json.decode(response.body.toString()) as Map<String, dynamic>;
}
} catch (e) {
debugPrint('getDeliveryLog error: $e');
} finally {
client.close();
}
return result;
}
}