mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-04-10 02:41:30 +03:00
add: Preliminary support for flutter front-end calling rust back-end http request
This commit is contained in:
@@ -9,6 +9,7 @@ import 'package:get/get.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../common.dart';
|
||||
import '../utils/http_service.dart';
|
||||
import 'model.dart';
|
||||
import 'platform_model.dart';
|
||||
|
||||
@@ -135,8 +136,8 @@ class UserModel {
|
||||
/// throw [RequestException]
|
||||
Future<LoginResponse> login(LoginRequest loginRequest) async {
|
||||
final url = await bind.mainGetApiServer();
|
||||
final resp = await http.post(Uri.parse('$url/api/login'),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
final resp = await HttpService().sendRequest(
|
||||
'$url/api/login', HttpMethod.post,
|
||||
body: jsonEncode(loginRequest.toJson()));
|
||||
|
||||
final Map<String, dynamic> body;
|
||||
|
||||
48
flutter/lib/utils/http_service.dart
Normal file
48
flutter/lib/utils/http_service.dart
Normal file
@@ -0,0 +1,48 @@
|
||||
import 'dart:convert';
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../models/platform_model.dart';
|
||||
|
||||
enum HttpMethod { get, post, put, delete }
|
||||
|
||||
class HttpService {
|
||||
Future<http.Response> sendRequest(
|
||||
String url,
|
||||
HttpMethod method, {
|
||||
Map<String, String>? headers,
|
||||
dynamic body,
|
||||
}) async {
|
||||
headers ??= {'Content-Type': 'application/json'};
|
||||
String headersJson = jsonEncode(headers);
|
||||
String methodName = method.toString().split('.').last;
|
||||
|
||||
await bind.mainHttpRequest(url: url, method: methodName.toLowerCase(), body: body, header: headersJson);
|
||||
|
||||
var resJson = await _pollForResponse();
|
||||
return _parseHttpResponse(resJson);
|
||||
}
|
||||
|
||||
Future<String> _pollForResponse() async {
|
||||
String responseJson = await bind.mainGetAsyncStatus();
|
||||
while (responseJson == " ") {
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
responseJson = await bind.mainGetAsyncStatus();
|
||||
}
|
||||
return responseJson;
|
||||
}
|
||||
|
||||
http.Response _parseHttpResponse(String responseJson) {
|
||||
try {
|
||||
var parsedJson = jsonDecode(responseJson);
|
||||
String body = parsedJson['body'];
|
||||
Map<String, String> headers = {};
|
||||
for (var key in parsedJson['headers'].keys) {
|
||||
headers[key] = parsedJson['headers'][key];
|
||||
}
|
||||
int statusCode = parsedJson['status_code'];
|
||||
return http.Response(body, statusCode, headers: headers);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to parse response: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user