drupal_request.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Automatic FlutterFlow imports
  2. import '/backend/schema/structs/index.dart';
  3. import '/flutter_flow/flutter_flow_theme.dart';
  4. import '/flutter_flow/flutter_flow_util.dart';
  5. import 'index.dart'; // Imports other custom actions
  6. import '/flutter_flow/custom_functions.dart'; // Imports custom functions
  7. import 'package:flutter/material.dart';
  8. // Begin custom action code
  9. // DO NOT REMOVE OR MODIFY THE CODE ABOVE!
  10. import 'dart:async';
  11. import 'dart:convert';
  12. import 'package:http/http.dart' as http;
  13. Future<dynamic> drupalRequest(
  14. String method,
  15. String url,
  16. String sessionName,
  17. String sessionId,
  18. String token,
  19. String body,
  20. ) async {
  21. try {
  22. //--------------------------------------------------
  23. // Headers
  24. //--------------------------------------------------
  25. final headers = <String, String>{
  26. 'Accept': 'application/json',
  27. 'Content-Type': 'application/json',
  28. };
  29. if (sessionName.trim().isNotEmpty && sessionId.trim().isNotEmpty) {
  30. headers['Cookie'] = '$sessionName=$sessionId';
  31. }
  32. if (token.trim().isNotEmpty) {
  33. headers['X-CSRF-Token'] = token;
  34. }
  35. //--------------------------------------------------
  36. // DEBUG
  37. //--------------------------------------------------
  38. print('========================================');
  39. print('DRUPAL REQUEST');
  40. print('Method : $method');
  41. print('URL : $url');
  42. print('Headers');
  43. headers.forEach((k, v) => print('$k: $v'));
  44. if (body.isNotEmpty) {
  45. print('Body');
  46. print(body);
  47. }
  48. print('========================================');
  49. //--------------------------------------------------
  50. // Request
  51. //--------------------------------------------------
  52. http.Response response;
  53. switch (method.toUpperCase()) {
  54. case 'POST':
  55. response = await http
  56. .post(
  57. Uri.parse(url),
  58. headers: headers,
  59. body: body,
  60. )
  61. .timeout(const Duration(seconds: 30));
  62. break;
  63. case 'PUT':
  64. response = await http
  65. .put(
  66. Uri.parse(url),
  67. headers: headers,
  68. body: body,
  69. )
  70. .timeout(const Duration(seconds: 30));
  71. break;
  72. case 'DELETE':
  73. response = await http
  74. .delete(
  75. Uri.parse(url),
  76. headers: headers,
  77. body: body,
  78. )
  79. .timeout(const Duration(seconds: 30));
  80. break;
  81. default:
  82. response = await http
  83. .get(
  84. Uri.parse(url),
  85. headers: headers,
  86. )
  87. .timeout(const Duration(seconds: 30));
  88. }
  89. //--------------------------------------------------
  90. // DEBUG RESPONSE
  91. //--------------------------------------------------
  92. print('========================================');
  93. print('STATUS : ${response.statusCode}');
  94. print('RESPONSE HEADERS');
  95. response.headers.forEach((k, v) => print('$k: $v'));
  96. print('BODY');
  97. print(response.body);
  98. print('========================================');
  99. //--------------------------------------------------
  100. // JSON of tekst
  101. //--------------------------------------------------
  102. dynamic data;
  103. try {
  104. data = jsonDecode(response.body);
  105. } catch (_) {
  106. data = response.body;
  107. }
  108. if (response.statusCode >= 200 && response.statusCode < 300) {
  109. return data;
  110. }
  111. return [];
  112. } on TimeoutException {
  113. return [];
  114. } catch (e, stack) {
  115. print(e);
  116. print(stack);
  117. return [];
  118. }
  119. }
  120. // Set your action name, define your arguments and return parameter,
  121. // and then add the boilerplate code using the `</>` button on the right!