Skip to content

Commit

Permalink
feat: Multipart requests: ensure to always get the full response (#779)
Browse files Browse the repository at this point in the history
* Multipart requests: Ensure to always get the full response

* Update lib/src/utils/http_helper.dart

Co-authored-by: Pierre Slamich <[email protected]>

---------

Co-authored-by: Pierre Slamich <[email protected]>
  • Loading branch information
g123k and teolemon authored Aug 9, 2023
1 parent 4043044 commit e53c7fd
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions lib/src/utils/http_helper.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:meta/meta.dart';
import 'package:path/path.dart';

import '../model/status.dart';
import '../model/user.dart';
import 'open_food_api_configuration.dart';
import 'query_type.dart';
import 'uri_reader.dart';
import '../model/status.dart';
import '../model/user.dart';

/// General functions for sending http requests (post, get, multipart, ...)
class HttpHelper {
/// Gets the instance
static HttpHelper get instance => _instance ??= HttpHelper.internal();
static HttpHelper? _instance;

@visibleForTesting
static set instance(HttpHelper value) => _instance = value;

Expand Down Expand Up @@ -173,22 +176,32 @@ class HttpHelper {
}

// get the response status
Status status = await request.send().then((response) {
if (response.statusCode == 200) {
return response.stream.first.then((responseBody) {
try {
return Status.fromJson(jsonDecode(utf8.decode(responseBody)));
} catch (e) {
//When the server returns html instead of json
return Status(status: 200, body: utf8.decode(responseBody));
}
});
} else {
return Status(
status: response.statusCode, error: response.reasonPhrase);
http.StreamedResponse response = await request.send();

if (response.statusCode == 200) {
String responseBody = await _extractResponseAsString(response);
try {
return Status.fromJson(jsonDecode(responseBody));
} catch (e) {
//When the server returns html instead of JSON
return Status(status: 200, body: responseBody);
}
});
return status;
} else {
return Status(
status: response.statusCode,
error: response.reasonPhrase,
);
}
}

Future<String> _extractResponseAsString(
http.StreamedResponse response) async {
final Completer<String> completer = Completer<String>();
final StringBuffer contents = StringBuffer();
response.stream.transform(utf8.decoder).listen((data) {
contents.write(data);
}, onDone: () => completer.complete(contents.toString()));
return completer.future;
}

/// Returns the request headers.
Expand Down

0 comments on commit e53c7fd

Please sign in to comment.