darb_delivery 0.1.0
darb_delivery: ^0.1.0 copied to clipboard
Client for the Darb delivery marketplace (Oman). Send a customer and a destination; Darb prices the delivery, charges your credit and assigns a courier.
example/darb_delivery_example.dart
import 'dart:io';
import 'package:darb_delivery/darb_delivery.dart';
/// Sends one real delivery to the Darb test environment.
///
/// DARB_SHOP_ID=… DARB_SECRET_KEY=… dart run example/darb_delivery_example.dart
Future<void> main() async {
final shopId = Platform.environment['DARB_SHOP_ID'];
final secret = Platform.environment['DARB_SECRET_KEY'];
if (shopId == null || secret == null) {
stderr.writeln('Set DARB_SHOP_ID and DARB_SECRET_KEY (issued by Darb).');
exit(2);
}
final darb = DarbClient(
shopId: shopId,
auth: HmacAuth(secret),
environment: DarbEnvironment.test,
);
try {
await darb.testConnection();
print('Connected: credentials accepted.');
final result = await darb.createDelivery(DeliveryRequest(
externalOrderId: 'example-${DateTime.now().millisecondsSinceEpoch}',
customerName: 'أحمد الشامسي',
customerPhone: '+968 9123 4567',
dropoff: Dropoff.wilayaOnly('السيب'),
items: [
OrderItem(name: 'كعكة', qty: 1, price: 8.5),
OrderItem(name: 'عصير', qty: 2, price: 1.25),
],
notes: 'فيلا ٥، الخوض — بجانب المسجد',
));
print('Created ${result.orderId}: ${result.status}, ${result.deliveryPrice} ${result.currency}');
if (result.awaitingLocation) {
print('Darb has messaged the customer for their location.');
}
} on DarbApiException catch (e) {
// Stable codes: branch on these, show messageAr to people.
switch (e.code) {
case DarbErrorCode.insufficientCredit:
print('Top up your Darb credit: ${e.messageAr}');
case DarbErrorCode.validationError:
print('Fix these fields: ${e.fields}');
default:
print('Darb refused (${e.rawCode}): ${e.message} [ref ${e.requestId}]');
}
exit(1);
} on DarbTransportException catch (e) {
print('Could not reach Darb: ${e.message}');
exit(1);
} finally {
darb.close();
}
}