sendRequest method
Implementation
Future<TClientResponse> sendRequest(
Method method,
String path, {
Map<String, String>? query,
Object? body,
Map<String, String>? headers,
Duration? sendTimeout,
Duration? receiveTimeout,
}) async {
try {
// options ထဲက default ကို သုံးမလား၊ parameter က custom ကို သုံးမလား ရွေးမယ်
final effectiveSendTimeout = sendTimeout ?? options.sendTimeout;
final effectiveReceiveTimeout = receiveTimeout ?? options.receiveTimeout;
// 1. URL ကို စနစ်တကျ တည်ဆောက်ခြင်း
Uri uri;
if (path.startsWith('http')) {
// Path က URL အပြည့်အစုံ ဖြစ်နေရင် အဲဒါကိုပဲ သုံးမယ်
uri = Uri.parse(path).replace(queryParameters: query);
} else {
// Base URL နဲ့ Path ကို ပေါင်းစပ်မယ်
final baseUri = Uri.parse(options.baseUrl);
uri = baseUri.resolve(path).replace(queryParameters: query);
}
// 2. Request ဖွင့်ခြင်း
final request = await ioClient
.openUrl(method.value, uri)
.timeout(effectiveSendTimeout); // Connect & Send Timeout
// 3. Headers သတ်မှတ်ခြင်း
setHeaders(request, headers);
// JSON အတွက် Default Content-Type ထည့်ပေးခြင်း
if (body != null && request.headers['content-type'] == null) {
request.headers.set('content-type', 'application/json; charset=utf-8');
}
// 4. Body ရေးသားခြင်း
if (body != null) {
final bytes = utf8.encode(jsonEncode(body));
request.contentLength = bytes.length; // Content-Length သတ်မှတ်ပေးခြင်း
request.add(bytes);
}
// 5. Response ရယူခြင်း
final response = await request.close().timeout(effectiveReceiveTimeout);
final responseBody = await response
.transform(utf8.decoder)
.join()
.timeout(effectiveReceiveTimeout); // ဒီနေရာမှာပါ ထည့်သင့်ပါတယ်
return TClientResponse(
statusCode: response.statusCode,
headers: getResponseHeaders(response),
data: responseBody,
);
} on TimeoutException catch (e) {
// ဘယ်နေရာက timeout ဖြစ်တာလဲဆိုတာ သိနိုင်အောင်လို့ပါ
throw Exception(
"Network Timeout: ${e.message ?? 'Operation took too long'}",
);
} on SocketException catch (e) {
throw Exception("No Internet Connection or Server unreachable: $e");
} catch (e) {
throw Exception("Request failed: $e");
}
}