-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_helper.dart
90 lines (70 loc) · 2.72 KB
/
http_helper.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import 'dart:convert';
import 'package:http/http.dart' as http;
class FxNetwork {
static Future<NetworkResponse> post(String url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
http.Response response = await http.post(
NetworkUtil.parseToUri(url, format: true),
headers: headers,
body: body,
encoding: encoding);
return NetworkResponse(response.body, response.statusCode, response);
}
static Future<NetworkResponse> get(String url,
{Map<String, String>? headers}) async {
http.Response response = await http.get(
NetworkUtil.parseToUri(url, format: true),
headers: headers,
);
return NetworkResponse(response.body, response.statusCode, response);
}
static Future<NetworkResponse> delete(String url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
http.Response response = await http
.delete(NetworkUtil.parseToUri(url, format: true), headers: headers);
return NetworkResponse(response.body, response.statusCode, response);
}
static Future<NetworkResponse> put(String url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
http.Response response = await http
.put(NetworkUtil.parseToUri(url, format: true), headers: headers);
return NetworkResponse(response.body, response.statusCode, response);
}
static Future<NetworkResponse> patch(String url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
http.Response response = await http
.patch(NetworkUtil.parseToUri(url, format: true), headers: headers);
return NetworkResponse(response.body, response.statusCode, response);
}
static Future<NetworkResponse> head(String url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) async {
http.Response response = await http
.head(NetworkUtil.parseToUri(url, format: true), headers: headers);
return NetworkResponse(response.body, response.statusCode, response);
}
}
class NetworkUtil {
static String formatUrl(String url) {
if (url[url.length - 1] == '/') return url.substring(0, url.length - 1);
return url;
}
static Uri parseToUri(String url, {bool format = false}) {
if (format) {
return Uri.parse(formatUrl(url));
}
return Uri.parse(url);
}
}
class NetworkResponse {
late String _body;
late int _statusCode;
late http.Response _baseResponse;
NetworkResponse(String body, int statusCode, http.Response baseResponse) {
_body = body;
_statusCode = statusCode;
_baseResponse = baseResponse;
}
int get statusCode => _statusCode;
String get body => _body;
http.Response get baseResponse => _baseResponse;
}