@@ -3,6 +3,7 @@ import 'dart:convert';
3
3
import 'package:functions_client/src/constants.dart' ;
4
4
import 'package:functions_client/src/types.dart' ;
5
5
import 'package:http/http.dart' as http;
6
+ import 'package:http/http.dart' show MultipartRequest;
6
7
import 'package:yet_another_json_isolate/yet_another_json_isolate.dart' ;
7
8
8
9
class FunctionsClient {
@@ -38,11 +39,17 @@ class FunctionsClient {
38
39
39
40
/// Invokes a function
40
41
///
41
- /// [functionName] - the name of the function to invoke
42
+ /// [functionName] is the name of the function to invoke
42
43
///
43
- /// [headers] : object representing the headers to send with the request
44
+ /// [headers] to send with the request
45
+ ///
46
+ /// [body] of the request when [files] is null and can be of type String
47
+ /// or an Object that is encodable to JSON with `jsonEncode` .
48
+ /// If [files] is not null, [body] represents the fields of the
49
+ /// [MultipartRequest] and must be be of type `Map<String, String>` .
50
+ ///
51
+ /// [files] to send in a `MultipartRequest` . [body] is used for the fields.
44
52
///
45
- /// [body] : the body of the request
46
53
///
47
54
/// ```dart
48
55
/// // Call a standard function
@@ -70,12 +77,11 @@ class FunctionsClient {
70
77
Future <FunctionResponse > invoke (
71
78
String functionName, {
72
79
Map <String , String >? headers,
73
- Map <String , dynamic >? body,
80
+ Object ? body,
81
+ Iterable <http.MultipartFile >? files,
74
82
Map <String , dynamic >? queryParameters,
75
83
HttpMethod method = HttpMethod .post,
76
84
}) async {
77
- final bodyStr = body == null ? null : await _isolate.encode (body);
78
-
79
85
final uri = Uri .parse ('$_url /$functionName ' )
80
86
.replace (queryParameters: queryParameters);
81
87
@@ -84,12 +90,36 @@ class FunctionsClient {
84
90
if (headers != null ) ...headers
85
91
};
86
92
87
- final request = http.Request (method.name, uri);
93
+ final http.BaseRequest request;
94
+ if (files != null ) {
95
+ assert (
96
+ body == null || body is Map <String , String >,
97
+ 'body must be of type Map' ,
98
+ );
99
+ final fields = body as Map <String , String >? ;
100
+
101
+ request = http.MultipartRequest (method.name, uri)
102
+ ..fields.addAll (fields ?? {})
103
+ ..files.addAll (files);
104
+ } else {
105
+ final bodyRequest = http.Request (method.name, uri);
106
+
107
+ final String ? bodyStr;
108
+ if (body == null ) {
109
+ bodyStr = null ;
110
+ } else if (body is String ) {
111
+ bodyStr = body;
112
+ } else {
113
+ bodyStr = await _isolate.encode (body);
114
+ }
115
+ if (bodyStr != null ) bodyRequest.body = bodyStr;
116
+ request = bodyRequest;
117
+ }
88
118
89
119
finalHeaders.forEach ((key, value) {
90
120
request.headers[key] = value;
91
121
});
92
- if (bodyStr != null ) request.body = bodyStr;
122
+
93
123
final response = await (_httpClient? .send (request) ?? request.send ());
94
124
final responseType = (response.headers['Content-Type' ] ??
95
125
response.headers['content-type' ] ??
0 commit comments