11import 'dart:convert' ;
2+ import 'dart:io' ;
23
4+ import 'package:flutter/foundation.dart' ;
35import 'package:http/http.dart' as http;
46import 'package:node_auth/data/exception/remote_data_source_exception.dart' ;
7+ import 'package:path/path.dart' as path;
8+
9+ final _indent = ' ' * 11 ;
10+
11+ void _logRequest (
12+ Uri url,
13+ String method, {
14+ Map <String , String > headers,
15+ Map <String , String > body,
16+ Map <String , String > multipartFields,
17+ List <http.MultipartFile > multipartFiles,
18+ }) {
19+ debugPrint ('[http] --> ${method } ${url }' );
20+ debugPrint ('${_indent }headers: $headers ' );
21+
22+ if (method == 'POST' || method == 'PUT' ) {
23+ debugPrint ('${_indent }body: $body ' );
24+
25+ if (method == 'POST' ) {
26+ if (multipartFields != null ) {
27+ debugPrint ('${_indent }multipartFields: ${multipartFields }' );
28+ }
29+ if (multipartFields != null ) {
30+ debugPrint ('${_indent }multipartFiles: ${multipartFiles }' );
31+ }
32+ }
33+ }
34+ }
35+
36+ void _logResponse (http.Response response) {
37+ debugPrint ('[http] <-- ${response .statusCode } ${response .request }' );
38+ debugPrint ('${_indent }bodyBytes: ${response .bodyBytes ?.length }' );
39+ try {
40+ debugPrint ('${_indent }body: ' + response.body);
41+ } catch (_) {}
42+ }
543
644class NetworkUtils {
745 static Future get (
846 Uri url, {
947 Map <String , String > headers,
1048 }) async {
49+ _logRequest (url, 'GET' , headers: headers);
50+
1151 final response = await http.get (url, headers: headers);
52+ _logResponse (response);
53+ return _parse (response);
54+ }
55+
56+ static dynamic _parse (http.Response response) {
1257 final body = response.body;
1358 final statusCode = response.statusCode;
59+
1460 if (body == null ) {
1561 throw RemoteDataSourceException (statusCode, 'Response body is null' );
1662 }
63+
1764 final decoded = json.decode (body);
1865 if (statusCode < 200 || statusCode >= 300 ) {
1966 throw RemoteDataSourceException (statusCode, decoded['message' ]);
@@ -39,6 +86,8 @@ class NetworkUtils {
3986 Map <String , String > headers,
4087 Map <String , String > body,
4188 }) async {
89+ _logRequest (url, method, headers: headers, body: body);
90+
4291 final request = http.Request (method, url);
4392 if (body != null ) {
4493 request.bodyFields = body;
@@ -48,25 +97,55 @@ class NetworkUtils {
4897 }
4998 final streamedResponse = await request.send ();
5099
51- final statusCode = streamedResponse.statusCode;
52- final decoded = json.decode (await streamedResponse.stream.bytesToString ());
53-
54- if (statusCode < 200 || statusCode >= 300 ) {
55- throw RemoteDataSourceException (statusCode, decoded['message' ]);
56- }
57-
58- return decoded;
100+ final response = await http.Response .fromStream (streamedResponse);
101+ _logResponse (response);
102+ return _parse (response);
59103 }
60104
61105 static Future put (
62106 Uri url, {
63107 Map <String , String > headers,
64- body,
108+ Map < String , String > body,
65109 }) =>
66110 _helper (
67111 'PUT' ,
68112 url,
69113 headers: headers,
70114 body: body,
71115 );
116+
117+ static Future multipartPost (
118+ Uri url,
119+ File file,
120+ String field, {
121+ Map <String , String > headers,
122+ Map <String , String > fields,
123+ }) async {
124+ final stream = http.ByteStream (file.openRead ());
125+ final length = await file.length ();
126+
127+ final request = http.MultipartRequest ('POST' , url)
128+ ..fields.addAll (fields ?? {})
129+ ..files.add (
130+ http.MultipartFile (
131+ field,
132+ stream,
133+ length,
134+ filename: path.basename (file.path),
135+ ),
136+ )
137+ ..headers.addAll (headers ?? {});
138+ _logRequest (
139+ url,
140+ 'POST' ,
141+ headers: headers,
142+ multipartFields: request.fields,
143+ multipartFiles: request.files,
144+ );
145+ final streamedResponse = await request.send ();
146+
147+ final response = await http.Response .fromStream (streamedResponse);
148+ _logResponse (response);
149+ return _parse (response);
150+ }
72151}
0 commit comments