This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:freezed_annotation/freezed_annotation.dart'; | |
import 'dart:io'; | |
part 'NetworkRequestBody.freezed.dart'; | |
part 'NetworkResponse.freezed.dart'; | |
class AccessTokenResponse { | |
String? accessToken; | |
AccessTokenResponse.fromJson(Map<String, dynamic> json) { | |
accessToken = json['access_token']; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkService { | |
NetworkService({ | |
required this.baseUrl, | |
dioClient, | |
httpHeaders, | |
}) : this._dio = dioClient, | |
this._headers = httpHeaders ?? {}; | |
Dio? _dio; | |
final String baseUrl; | |
Map<String, String> _headers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final req = _PreparedNetworkRequest<Model>( | |
request, | |
parser, | |
_dio!, | |
{..._headers, ...(request.headers ?? {})}, | |
onSendProgress, | |
onReceiveProgress, | |
); | |
final result = await compute( | |
executeRequest<Model>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<NetworkResponse<Model>> executeRequest<Model>( | |
_PreparedNetworkRequest request, | |
) async { | |
try { | |
dynamic body = request.request.data.whenOrNull( | |
json: (data) => data, | |
text: (data) => data, | |
); | |
final response = await request.dio.request( | |
request.request.path, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class _PreparedNetworkRequest<Model> { | |
const _PreparedNetworkRequest( | |
this.request, | |
this.parser, | |
this.dio, | |
this.headers, | |
this.onSendProgress, | |
this.onReceiveProgress, | |
); | |
final NetworkRequest request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void addBasicAuth(String accessToken) { | |
_headers['Authorization'] = 'Bearer $accessToken'; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try { | |
final response = await _dio!.request( | |
request.path, | |
data: body, | |
queryParameters: request.queryParams, | |
options: Dio.Options( | |
method: request.type.name, | |
headers: {..._headers, ...(request.headers ?? {})}, // Combine all headers | |
), | |
onSendProgress: onSendProgress, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dynamic body = request.data.whenOrNull( | |
json: (data) => data, | |
text: (data) => data, | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NetworkService { | |
NetworkService({ | |
required this.baseUrl, // Base service url | |
dioClient, // Prepared Dio instance could be injected | |
httpHeaders, // Global headers could be provided as well | |
}) : this._dio = dioClient, | |
this._headers = httpHeaders ?? {}; | |
Dio? _dio; | |
final String baseUrl; | |
Map<String, String> _headers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:freezed_annotation/freezed_annotation.dart'; | |
part 'NetworkResponse.freezed.dart'; | |
@freezed | |
class NetworkResponse<Model> with _$NetworkResponse { | |
const factory NetworkResponse.ok(Model data) = Ok; | |
const factory NetworkResponse.invalidParameters(String message) = InvalidParameters; | |
const factory NetworkResponse.noAuth(String message) = NoAuth; //401 | |
const factory NetworkResponse.noAccess(String message) = NoAccess; //403 | |
const factory NetworkResponse.badRequest(String message) = BadRequest; //400 | |
const factory NetworkResponse.notFound(String message) = NotFound; //404 |
NewerOlder