Networking Layer
MStore menggunakan Dio + Retrofit untuk HTTP networking dengan interceptor pipeline yang robust untuk handling authentication, retry, logging, dan error handling.๐ Arsitektur Networking
๐ง Dio Configuration
Factory Function
File:lib/core/network/dio_client.dart
Dependency Injection
File:lib/di/network_module.dart
๐ Interceptors
1. CorrelationInterceptor
Tujuan: Menambahkan correlation ID dan trace ID untuk request tracking.X-Correlation-ID: Unique ID per requestX-Trace-ID: Trace ID untuk distributed tracing
2. HeadersInterceptor
Tujuan: Menambahkan common headers ke semua request.Content-Type: application/jsonAccept: application/jsonX-Platform: ios/android/web/windows/macosX-App-Version: App version dari package_infoX-Device-ID: Unique device identifier
3. AuthInterceptor
Tujuan: Menambahkan Authorization token ke request yang memerlukan autentikasi.4. RefreshTokenInterceptor
Tujuan: Auto-refresh token saat access token expired (HTTP 401).- Request gagal dengan 401
- Ambil refresh token dari storage
- Call
/refresh-tokenendpoint - Simpan token baru
- Retry request original
- Jika refresh gagal โ logout user
5. RetryInterceptor
Tujuan: Retry request yang gagal karena network error.- Max retries: 2
- Exponential backoff: 500ms, 1000ms, 1500ms
- Hanya retry untuk: timeout dan connection error
- Tidak retry untuk: 4xx, 5xx errors
6. LoggingInterceptor
Tujuan: Logging semua HTTP request/response untuk debugging.๐ก Retrofit API
API Definition
File:lib/core/product/product_api.dart
Repository Implementation
File:lib/core/product/product_repository_retrofit.dart
โ Error Handling
NetworkException
File:lib/core/network/network_exceptions.dart
Failure Types
๐ Request Flow Example
Complete Flow
๐งช Testing
Mock Dio
๐ Performance Optimization
1. JSON Parsing di Isolate
2. HTTP/2 Support
3. Connection Pooling
Dio secara otomatis menggunakan connection pooling untuk reuse HTTP connections.๐ Security Best Practices
- HTTPS Only: Semua request menggunakan HTTPS
- Certificate Pinning (optional): Untuk production
- Token Storage: Encrypted di Isar database
- No Sensitive Data in Logs: Mask sensitive fields
- Request Timeout: Prevent hanging requests
๐ API Endpoints
Lihat dokumentasi lengkap di API Reference.Next Steps
- ๐ Error Handling
- ๐ Authentication Flow
- ๐พ Offline Sync Strategy
Best Practices:
- โ Gunakan Retrofit untuk type-safe API calls
- โ Handle semua error cases dengan Either pattern
- โ Implement retry logic untuk network errors
- โ Log semua requests untuk debugging
- โ Auto-refresh token untuk seamless UX