Skip to content

Commit 38d14b0

Browse files
committed
Changes
1 parent 6baf608 commit 38d14b0

File tree

7 files changed

+820
-170
lines changed

7 files changed

+820
-170
lines changed

assets/user.png

1.74 KB
Loading

lib/api_service.dart

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import 'dart:async';
22
import 'dart:convert';
33
import 'dart:io';
44

5+
import 'package:flutter/foundation.dart' show debugPrint;
56
import 'package:http/http.dart' as http;
7+
import 'package:path/path.dart' as path;
68

79
class Response {
810
final String token;
@@ -19,11 +21,16 @@ class User {
1921
final String name;
2022
final String email;
2123
final DateTime createdAt;
24+
final String imageUrl;
2225

2326
User.fromJson(Map<String, dynamic> json)
2427
: name = json['name'],
2528
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';
2734
}
2835

2936
class MyHttpException extends HttpException {
@@ -54,13 +61,13 @@ class ApiService {
5461
Future<Response> registerUser(
5562
String name, String email, String password) async {
5663
final url = new Uri.https(baseUrl, '/users');
57-
final body = {
64+
final body = <String, String>{
5865
'name': name,
5966
'email': email,
6067
'password': password,
6168
};
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);
6471
}
6572

6673
Future<User> getUserProfile(String email, String token) async {
@@ -85,7 +92,8 @@ class ApiService {
8592
// return message
8693
// special token and newPassword to reset password,
8794
// 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 {
8997
final url = new Uri.https(baseUrl, '/users/$email/password');
9098
final task = token != null && newPassword != null
9199
? NetworkUtils.post(url, body: {
@@ -96,6 +104,28 @@ class ApiService {
96104
final json = await task;
97105
return Response.fromJson(json);
98106
}
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+
}
99129
}
100130

101131
class NetworkUtils {
@@ -114,26 +144,34 @@ class NetworkUtils {
114144
}
115145

116146
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);
123149
}
124150

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;
132156
}
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+
134167
if (statusCode < 200 || statusCode >= 300) {
135168
throw MyHttpException(statusCode, decoded['message']);
136169
}
170+
137171
return decoded;
138172
}
173+
174+
static Future put(Uri url, {Map<String, String> headers, body}) {
175+
return _helper('PUT', url, headers: headers, body: body);
176+
}
139177
}

0 commit comments

Comments
 (0)