Skip to main content

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.
Headers yang ditambahkan:
  • X-Correlation-ID: Unique ID per request
  • X-Trace-ID: Trace ID untuk distributed tracing

2. HeadersInterceptor

Tujuan: Menambahkan common headers ke semua request.
Headers:
  • Content-Type: application/json
  • Accept: application/json
  • X-Platform: ios/android/web/windows/macos
  • X-App-Version: App version dari package_info
  • X-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).
Flow:
  1. Request gagal dengan 401
  2. Ambil refresh token dari storage
  3. Call /refresh-token endpoint
  4. Simpan token baru
  5. Retry request original
  6. Jika refresh gagal โ†’ logout user

5. RetryInterceptor

Tujuan: Retry request yang gagal karena network error.
Retry Strategy:
  • 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

  1. HTTPS Only: Semua request menggunakan HTTPS
  2. Certificate Pinning (optional): Untuk production
  3. Token Storage: Encrypted di Isar database
  4. No Sensitive Data in Logs: Mask sensitive fields
  5. Request Timeout: Prevent hanging requests

๐Ÿ“ API Endpoints

Lihat dokumentasi lengkap di API Reference.

Next Steps


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