// Automatic FlutterFlow imports import '/backend/schema/structs/index.dart'; import '/flutter_flow/flutter_flow_theme.dart'; import '/flutter_flow/flutter_flow_util.dart'; import 'index.dart'; // Imports other custom actions import '/flutter_flow/custom_functions.dart'; // Imports custom functions import 'package:flutter/material.dart'; // Begin custom action code // DO NOT REMOVE OR MODIFY THE CODE ABOVE! import 'dart:async'; import 'dart:convert'; import 'package:http/http.dart' as http; Future drupalRequest( String method, String url, String sessionName, String sessionId, String token, String body, ) async { try { //-------------------------------------------------- // Headers //-------------------------------------------------- final headers = { 'Accept': 'application/json', 'Content-Type': 'application/json', }; if (sessionName.trim().isNotEmpty && sessionId.trim().isNotEmpty) { headers['Cookie'] = '$sessionName=$sessionId'; } if (token.trim().isNotEmpty) { headers['X-CSRF-Token'] = token; } //-------------------------------------------------- // DEBUG //-------------------------------------------------- print('========================================'); print('DRUPAL REQUEST'); print('Method : $method'); print('URL : $url'); print('Headers'); headers.forEach((k, v) => print('$k: $v')); if (body.isNotEmpty) { print('Body'); print(body); } print('========================================'); //-------------------------------------------------- // Request //-------------------------------------------------- http.Response response; switch (method.toUpperCase()) { case 'POST': response = await http .post( Uri.parse(url), headers: headers, body: body, ) .timeout(const Duration(seconds: 30)); break; case 'PUT': response = await http .put( Uri.parse(url), headers: headers, body: body, ) .timeout(const Duration(seconds: 30)); break; case 'DELETE': response = await http .delete( Uri.parse(url), headers: headers, body: body, ) .timeout(const Duration(seconds: 30)); break; default: response = await http .get( Uri.parse(url), headers: headers, ) .timeout(const Duration(seconds: 30)); } //-------------------------------------------------- // DEBUG RESPONSE //-------------------------------------------------- print('========================================'); print('STATUS : ${response.statusCode}'); print('RESPONSE HEADERS'); response.headers.forEach((k, v) => print('$k: $v')); print('BODY'); print(response.body); print('========================================'); //-------------------------------------------------- // JSON of tekst //-------------------------------------------------- dynamic data; try { data = jsonDecode(response.body); } catch (_) { data = response.body; } if (response.statusCode >= 200 && response.statusCode < 300) { return data; } return { 'success': false, 'statusCode': response.statusCode, 'body': data, }; } on TimeoutException { return { 'success': false, 'statusCode': 408, 'error': 'Timeout', }; } catch (e, stack) { print(e); print(stack); return { 'success': false, 'statusCode': 500, 'error': e.toString(), }; } } // Set your action name, define your arguments and return parameter, // and then add the boilerplate code using the `` button on the right!