aloisdeniel / aloisdeniel/http_extensions
Support for Multipart requests
- Lingua principale
- Dart
- Stelle
- 35
- Fork
- 15
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
Hey @aloisdeniel
I noticed there is currently no support for Multipart request. I went ahead and added an extension to the extension. :)
``` dart
extension ExtendedClientX on ExtendedClient {
Future formWithOptions(
String url, {
@required Map data,
Map files,
Map headers,
List options,
}) async {
assert(data != null);
final request = http.MultipartRequest("POST", Uri.parse(url));
request.fields..addAll(_encodeFormBody(data));
if (files != null) {
for (final item in files.entries) {
request.files.add(
await http.MultipartFile.fromPath(item.key, item.value.path),
);
}
}
return sendWithOptions(
request,
[
...options,
JsonRequestOptions({...headers, io.HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded"})
],
).then(http.Response.fromStream);
}
String encodeBody(dynamic value) => json.encode(value);
Map _encodeFormBody(Map data) {
return data.map((key, dynamic value) => MapEntry(key, _isPrimitiveValue(value) ? value : encodeBody(value)));
}
bool _isPrimitiveValue(dynamic value) {
return [int, double, bool, String].contains(value.runtimeType);
}
}
```
And another helper extension for sugar.
``` dart
class JsonRequestOptions {
const JsonRequestOptions([this.headers = const {}]);
final Map headers;
}
class JsonRequestExtension extends Extension {
JsonRequestExtension([
JsonRequestOptions defaultOptions = const JsonRequestOptions(
{io.HttpHeaders.acceptHeader: mime, io.HttpHeaders.contentTypeHeader: mime},
),
]) : super(defaultOptions: defaultOptions);
static const mime = "application/json";
@override
Future sendWithOptions(http.BaseRequest request, JsonRequestOptions options) {
final headers = Map.from(defaultOptions.headers);
if (options.headers != null && options.headers.isNotEmpty) {
headers.addAll(options.headers);
}
if (headers.isNotEmpty) {
request.headers.addAll(headers);
}
return super.sendWithOptions(request, options);
}
}
```
This seemed to cover all my cases, tests passed and all.
Then, I also found the retry extension useful and realised I needed it too. Only issue now is the `_copyRequest` method. This eagerly tries to make a copy of the `BaseRequest`. As that is the only supported variant, it completely ignores the `files` and `fields` property of the multipart request. To solve this, we would need a more elegant approach. Hence the long story.
Also, nice work with the extensions. 👍
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Valutazione
Questa issue non è ancora stata valutata.