mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 04:49:21 +00:00
Use http package for requests
This commit is contained in:
parent
779333f20a
commit
ac48270303
@ -5,14 +5,13 @@ import 'package:comunic/helpers/account_credentials_helper.dart';
|
|||||||
import 'package:comunic/models/api_request.dart';
|
import 'package:comunic/models/api_request.dart';
|
||||||
import 'package:comunic/models/api_response.dart';
|
import 'package:comunic/models/api_response.dart';
|
||||||
import 'package:comunic/models/config.dart';
|
import 'package:comunic/models/config.dart';
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
|
|
||||||
/// API Helper
|
/// API Helper
|
||||||
///
|
///
|
||||||
/// @author Pierre HUBERT
|
/// @author Pierre HUBERT
|
||||||
|
|
||||||
class APIHelper {
|
class APIHelper {
|
||||||
final _httpClient = HttpClient();
|
|
||||||
|
|
||||||
/// Execute a [request] on the server and returns a [APIResponse]
|
/// Execute a [request] on the server and returns a [APIResponse]
|
||||||
///
|
///
|
||||||
/// This method should never throw but the response code of the [APIResponse]
|
/// This method should never throw but the response code of the [APIResponse]
|
||||||
@ -31,16 +30,6 @@ class APIHelper {
|
|||||||
request.addString("userToken2", tokens.tokenTwo);
|
request.addString("userToken2", tokens.tokenTwo);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare request body
|
|
||||||
String requestBody = "";
|
|
||||||
request.args.forEach((key, value) => requestBody +=
|
|
||||||
Uri.encodeQueryComponent(key) +
|
|
||||||
"=" +
|
|
||||||
Uri.encodeQueryComponent(value) +
|
|
||||||
"&");
|
|
||||||
|
|
||||||
List<int> bodyBytes = utf8.encode(requestBody);
|
|
||||||
|
|
||||||
// Determine server URL
|
// Determine server URL
|
||||||
final path = config().apiServerUri + request.uri;
|
final path = config().apiServerUri + request.uri;
|
||||||
Uri url;
|
Uri url;
|
||||||
@ -49,22 +38,16 @@ class APIHelper {
|
|||||||
else
|
else
|
||||||
url = Uri.https(config().apiServerName, path);
|
url = Uri.https(config().apiServerName, path);
|
||||||
|
|
||||||
//Connect to server
|
final response = await http.post(
|
||||||
final connection = await _httpClient.postUrl(url);
|
url.toString(),
|
||||||
connection.headers.set("Content-Length", bodyBytes.length.toString());
|
body: request.args,
|
||||||
connection.headers
|
encoding: utf8
|
||||||
.set("Content-Type", "application/x-www-form-urlencoded");
|
);
|
||||||
connection.add(bodyBytes);
|
|
||||||
|
|
||||||
final response = await connection.close();
|
|
||||||
|
|
||||||
if (response.statusCode != HttpStatus.ok)
|
if (response.statusCode != HttpStatus.ok)
|
||||||
return APIResponse(response.statusCode, null);
|
return APIResponse(response.statusCode, null);
|
||||||
|
|
||||||
//Save & return response
|
return APIResponse(response.statusCode, utf8.decode(response.bodyBytes));
|
||||||
final responseBody = await response.transform(utf8.decoder).join();
|
|
||||||
|
|
||||||
return APIResponse(response.statusCode, responseBody);
|
|
||||||
} on Exception catch (e) {
|
} on Exception catch (e) {
|
||||||
print(e.toString());
|
print(e.toString());
|
||||||
print("Could not execute a request!");
|
print("Could not execute a request!");
|
||||||
|
14
pubspec.lock
14
pubspec.lock
@ -46,6 +46,20 @@ packages:
|
|||||||
description: flutter
|
description: flutter
|
||||||
source: sdk
|
source: sdk
|
||||||
version: "0.0.0"
|
version: "0.0.0"
|
||||||
|
http:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: http
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "0.12.0+2"
|
||||||
|
http_parser:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: http_parser
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "3.1.3"
|
||||||
image_picker:
|
image_picker:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description:
|
description:
|
||||||
|
@ -33,6 +33,9 @@ dependencies:
|
|||||||
# Image picker is used whenever the user wants to send an image
|
# Image picker is used whenever the user wants to send an image
|
||||||
image_picker: ^0.5.4+3
|
image_picker: ^0.5.4+3
|
||||||
|
|
||||||
|
# The HTTP client is used to make requests on the Comunic API
|
||||||
|
http: ^0.12.0+2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
|
Loading…
Reference in New Issue
Block a user