@@ -2,7 +2,9 @@ import 'dart:async';
2
2
import 'dart:convert' ;
3
3
import 'dart:io' ;
4
4
5
+ import 'package:flutter/foundation.dart' show debugPrint;
5
6
import 'package:http/http.dart' as http;
7
+ import 'package:path/path.dart' as path;
6
8
7
9
class Response {
8
10
final String token;
@@ -19,11 +21,16 @@ class User {
19
21
final String name;
20
22
final String email;
21
23
final DateTime createdAt;
24
+ final String imageUrl;
22
25
23
26
User .fromJson (Map <String , dynamic > json)
24
27
: name = json['name' ],
25
28
email = json['email' ],
26
- createdAt = DateTime .tryParse (json['created_at' ]) ?? new DateTime .now ();
29
+ createdAt = DateTime .tryParse (json['created_at' ]) ?? new DateTime .now (),
30
+ imageUrl = json['image_url' ];
31
+
32
+ @override
33
+ String toString () => '$name , $email , $imageUrl ' ;
27
34
}
28
35
29
36
class MyHttpException extends HttpException {
@@ -54,13 +61,13 @@ class ApiService {
54
61
Future <Response > registerUser (
55
62
String name, String email, String password) async {
56
63
final url = new Uri .https (baseUrl, '/users' );
57
- final body = {
64
+ final body = < String , String > {
58
65
'name' : name,
59
66
'email' : email,
60
67
'password' : password,
61
68
};
62
- final json = await NetworkUtils .post (url, body: body);
63
- return Response .fromJson (json );
69
+ final decoded = await NetworkUtils .post (url, body: body);
70
+ return new Response .fromJson (decoded );
64
71
}
65
72
66
73
Future <User > getUserProfile (String email, String token) async {
@@ -85,7 +92,8 @@ class ApiService {
85
92
// return message
86
93
// special token and newPassword to reset password,
87
94
// otherwise, send an email to email
88
- resetPassword (String email, {String token, String newPassword}) async {
95
+ Future <Response > resetPassword (String email,
96
+ {String token, String newPassword}) async {
89
97
final url = new Uri .https (baseUrl, '/users/$email /password' );
90
98
final task = token != null && newPassword != null
91
99
? NetworkUtils .post (url, body: {
@@ -96,6 +104,28 @@ class ApiService {
96
104
final json = await task;
97
105
return Response .fromJson (json);
98
106
}
107
+
108
+ Future <User > uploadImage (File file, String email) async {
109
+ final url = new Uri .https (baseUrl, '/users/upload' );
110
+ final stream = new http.ByteStream (file.openRead ());
111
+ final length = await file.length ();
112
+ final request = new http.MultipartRequest ('POST' , url)
113
+ ..fields['user' ] = email
114
+ ..files.add (
115
+ new http.MultipartFile ('my_image' , stream, length, filename: path.basename (file.path)),
116
+ );
117
+ final streamedReponse = await request.send ();
118
+ final statusCode = streamedReponse.statusCode;
119
+ final decoded = json.decode (await streamedReponse.stream.bytesToString ());
120
+
121
+ debugPrint ('decoded: $decoded ' );
122
+
123
+ if (statusCode < 200 || statusCode >= 300 ) {
124
+ throw MyHttpException (statusCode, decoded['message' ]);
125
+ }
126
+
127
+ return User .fromJson (decoded);
128
+ }
99
129
}
100
130
101
131
class NetworkUtils {
@@ -114,26 +144,34 @@ class NetworkUtils {
114
144
}
115
145
116
146
static Future post (Uri url,
117
- {Map <String , String > headers, dynamic body}) async {
118
- return _postOrPut (http.post, url, headers: headers);
119
- }
120
-
121
- static Future put (Uri url, {Map <String , String > headers, body}) {
122
- return _postOrPut (http.put, url, headers: headers);
147
+ {Map <String , String > headers, Map <String , String > body}) {
148
+ return _helper ('POST' , url, headers: headers, body: body);
123
149
}
124
150
125
- static Future _postOrPut (function, Uri url,
126
- {Map <String , String > headers, body}) async {
127
- final response = await function (url, body: body, headers: headers);
128
- final responseBody = response.body;
129
- final statusCode = response.statusCode;
130
- if (responseBody == null ) {
131
- throw MyHttpException (statusCode, 'Response body is null' );
151
+ static Future _helper (String method, Uri url,
152
+ {Map <String , String > headers, Map <String , String > body}) async {
153
+ final request = new http.Request (method, url);
154
+ if (body != null ) {
155
+ request.bodyFields = body;
132
156
}
133
- final decoded = json.decode (responseBody);
157
+ if (headers != null ) {
158
+ request.headers.addAll (headers);
159
+ }
160
+ final streamedReponse = await request.send ();
161
+
162
+ final statusCode = streamedReponse.statusCode;
163
+ final decoded = json.decode (await streamedReponse.stream.bytesToString ());
164
+
165
+ debugPrint ('decoded: $decoded ' );
166
+
134
167
if (statusCode < 200 || statusCode >= 300 ) {
135
168
throw MyHttpException (statusCode, decoded['message' ]);
136
169
}
170
+
137
171
return decoded;
138
172
}
173
+
174
+ static Future put (Uri url, {Map <String , String > headers, body}) {
175
+ return _helper ('PUT' , url, headers: headers, body: body);
176
+ }
139
177
}
0 commit comments