Skip to content

Commit 880b483

Browse files
author
Sergey Khomushin
committed
add http.Client prop for testing
1 parent 0154ddf commit 880b483

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

lib/api/send_json.dart

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import 'package:http/http.dart' as http;
22

3-
Future<String> sendJSON(Uri uri, String data) async {
4-
var response = await http.post(
5-
uri,
6-
headers: {
7-
'Content-Type': 'application/json',
8-
},
9-
body: data,
10-
);
3+
Future<String> sendJSON(http.Client client, Uri uri, String data) async {
4+
try {
5+
var response = await client.post(
6+
uri,
7+
headers: {
8+
'Content-Type': 'application/json',
9+
},
10+
body: data,
11+
);
1112

12-
if (response.statusCode == 200) {
13-
return response.body;
14-
} else {
15-
throw response.body;
13+
if (response.statusCode == 200) {
14+
return response.body;
15+
} else {
16+
throw response.body;
17+
}
18+
} finally {
19+
client.close();
1620
}
1721
}

lib/emailjs.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
library emailjs;
22

33
import 'dart:convert';
4+
import 'package:http/http.dart' as http;
45

56
import 'package:emailjs/utils/validate_params.dart';
67
import 'package:emailjs/api/send_json.dart';
78

89
class EmailJS {
910
static String _publicKey = '';
1011
static String _origin = 'api.emailjs.com';
12+
static http.Client _httpClient = http.Client();
1113

12-
static void init(String publicKey, [String? origin]) {
14+
static void init(String publicKey, [String? origin, http.Client? httpClient]) {
1315
EmailJS._publicKey = publicKey;
1416
EmailJS._origin = origin ?? 'api.emailjs.com';
17+
EmailJS._httpClient = httpClient ?? http.Client();
1518
}
1619

1720
static Future<String> send(
@@ -33,6 +36,7 @@ class EmailJS {
3336
};
3437

3538
return await sendJSON(
39+
EmailJS._httpClient,
3640
Uri.https(EmailJS._origin, 'api/v1.0/email/send'),
3741
json.encode(params),
3842
);

0 commit comments

Comments
 (0)