1
+ import 'dart:async' ;
1
2
import 'dart:io' ;
2
3
3
4
import 'package:clerk_auth/src/utils/extensions.dart' ;
4
- import 'package:http/http.dart' ;
5
+ import 'package:http/http.dart' as http ;
5
6
6
7
/// Enum detailing [HttpMethod] s used by the Clerk API
7
8
enum HttpMethod {
@@ -33,10 +34,25 @@ enum HttpMethod {
33
34
/// Abstract class defining the interface to call the
34
35
/// Clerk back-end over http
35
36
///
36
- abstract class HttpService {
37
+ abstract interface class HttpService {
38
+ /// Construct a [HttpService]
39
+ const HttpService ();
40
+
41
+ /// Initialises this instance of the http service
42
+ ///
43
+ /// It is possible that [initialise] will be called
44
+ /// multiple times, and must be prepared for that to happen
45
+ Future <void > initialise ();
46
+
47
+ /// Terminates this instance of the http service
48
+ ///
49
+ /// It is possible that [terminate] will be called
50
+ /// multiple times, and must be prepared for that to happen
51
+ void terminate ();
52
+
37
53
/// [send] data to the back end, and receive a [Response]
38
54
///
39
- Future <Response > send (
55
+ Future <http. Response > send (
40
56
HttpMethod method,
41
57
Uri uri, {
42
58
Map <String , String >? headers,
@@ -46,30 +62,43 @@ abstract class HttpService {
46
62
47
63
/// Upload a [File] to the back end, and receive a [Response]
48
64
///
49
- Future <Response > sendByteStream (
65
+ Future <http. Response > sendByteStream (
50
66
HttpMethod method,
51
67
Uri uri,
52
- ByteStream byteStream,
68
+ http. ByteStream byteStream,
53
69
int length,
54
70
Map <String , String > headers,
55
71
);
56
72
}
57
73
58
74
/// Default implementation of [HttpService]
59
75
///
60
- class DefaultHttpService implements HttpService {
76
+ class DefaultHttpService extends HttpService {
61
77
/// Constructor
62
- const DefaultHttpService ();
78
+ DefaultHttpService ();
79
+
80
+ http.Client ? _client;
81
+
82
+ @override
83
+ Future <void > initialise () async {
84
+ _client ?? = http.Client ();
85
+ }
86
+
87
+ @override
88
+ void terminate () {
89
+ _client? .close ();
90
+ _client = null ;
91
+ }
63
92
64
93
@override
65
- Future <Response > send (
94
+ Future <http. Response > send (
66
95
HttpMethod method,
67
96
Uri uri, {
68
97
Map <String , String >? headers,
69
98
Map <String , dynamic >? params,
70
99
String ? body,
71
100
}) async {
72
- final request = Request (method.toString (), uri);
101
+ final request = http. Request (method.toString (), uri);
73
102
74
103
if (headers case Map <String , String > headers) {
75
104
request.headers.addAll (headers);
@@ -83,30 +112,30 @@ class DefaultHttpService implements HttpService {
83
112
request.body = body;
84
113
}
85
114
86
- final streamedResponse = await request .send ();
87
- return Response .fromStream (streamedResponse);
115
+ final streamedResponse = await _client ! .send (request );
116
+ return http. Response .fromStream (streamedResponse);
88
117
}
89
118
90
119
@override
91
- Future <Response > sendByteStream (
120
+ Future <http. Response > sendByteStream (
92
121
HttpMethod method,
93
122
Uri uri,
94
- ByteStream byteStream,
123
+ http. ByteStream byteStream,
95
124
int length,
96
125
Map <String , String > headers,
97
126
) async {
98
- final request = MultipartRequest (method.toString (), uri);
127
+ final request = http. MultipartRequest (method.toString (), uri);
99
128
request.headers.addAll (headers);
100
129
101
- final multipartFile = MultipartFile (
130
+ final multipartFile = http. MultipartFile (
102
131
'file' ,
103
132
byteStream,
104
133
length,
105
134
filename: byteStream.hashCode.toString (),
106
135
);
107
136
request.files.add (multipartFile);
108
137
109
- final streamedResponse = await request .send ();
110
- return Response .fromStream (streamedResponse);
138
+ final streamedResponse = await _client ! .send (request );
139
+ return http. Response .fromStream (streamedResponse);
111
140
}
112
141
}
0 commit comments