http_service.dart 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import 'dart:convert';
  2. import 'package:flutter/foundation.dart';
  3. import 'package:http/http.dart' as http;
  4. import '../models/platform_model.dart';
  5. export 'package:http/http.dart' show Response;
  6. enum HttpMethod { get, post, put, delete }
  7. class HttpService {
  8. Future<http.Response> sendRequest(
  9. Uri url,
  10. HttpMethod method, {
  11. Map<String, String>? headers,
  12. dynamic body,
  13. }) async {
  14. headers ??= {'Content-Type': 'application/json'};
  15. // Determine if there is currently a proxy setting, and if so, use FFI to call the Rust HTTP method.
  16. final isProxy = await bind.mainGetProxyStatus();
  17. if (!isProxy) {
  18. return await _pollFultterHttp(url, method, headers: headers, body: body);
  19. }
  20. String headersJson = jsonEncode(headers);
  21. String methodName = method.toString().split('.').last;
  22. await bind.mainHttpRequest(
  23. url: url.toString(),
  24. method: methodName.toLowerCase(),
  25. body: body,
  26. header: headersJson);
  27. var resJson = await _pollForResponse(url.toString());
  28. return _parseHttpResponse(resJson);
  29. }
  30. Future<http.Response> _pollFultterHttp(
  31. Uri url,
  32. HttpMethod method, {
  33. Map<String, String>? headers,
  34. dynamic body,
  35. }) async {
  36. var response = http.Response('', 400);
  37. switch (method) {
  38. case HttpMethod.get:
  39. response = await http.get(url, headers: headers);
  40. break;
  41. case HttpMethod.post:
  42. response = await http.post(url, headers: headers, body: body);
  43. break;
  44. case HttpMethod.put:
  45. response = await http.put(url, headers: headers, body: body);
  46. break;
  47. case HttpMethod.delete:
  48. response = await http.delete(url, headers: headers, body: body);
  49. break;
  50. default:
  51. throw Exception('Unsupported HTTP method');
  52. }
  53. return response;
  54. }
  55. Future<String> _pollForResponse(String url) async {
  56. String? responseJson = " ";
  57. while (responseJson == " ") {
  58. responseJson = await bind.mainGetHttpStatus(url: url);
  59. if (responseJson == null) {
  60. throw Exception('The HTTP request failed');
  61. }
  62. if (responseJson == " ") {
  63. await Future.delayed(const Duration(milliseconds: 100));
  64. }
  65. }
  66. return responseJson!;
  67. }
  68. http.Response _parseHttpResponse(String responseJson) {
  69. try {
  70. var parsedJson = jsonDecode(responseJson);
  71. String body = parsedJson['body'];
  72. Map<String, String> headers = {};
  73. for (var key in parsedJson['headers'].keys) {
  74. headers[key] = parsedJson['headers'][key];
  75. }
  76. int statusCode = parsedJson['status_code'];
  77. return http.Response(body, statusCode, headers: headers);
  78. } catch (e) {
  79. throw Exception('Failed to parse response: $e');
  80. }
  81. }
  82. }
  83. Future<http.Response> get(Uri url, {Map<String, String>? headers}) async {
  84. return await HttpService().sendRequest(url, HttpMethod.get, headers: headers);
  85. }
  86. Future<http.Response> post(Uri url,
  87. {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
  88. return await HttpService()
  89. .sendRequest(url, HttpMethod.post, body: body, headers: headers);
  90. }
  91. Future<http.Response> put(Uri url,
  92. {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
  93. return await HttpService()
  94. .sendRequest(url, HttpMethod.put, body: body, headers: headers);
  95. }
  96. Future<http.Response> delete(Uri url,
  97. {Map<String, String>? headers, Object? body, Encoding? encoding}) async {
  98. return await HttpService()
  99. .sendRequest(url, HttpMethod.delete, body: body, headers: headers);
  100. }