| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- // 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<dynamic> drupalRequest(
- String method,
- String url,
- String sessionName,
- String sessionId,
- String token,
- String body,
- ) async {
- try {
- //--------------------------------------------------
- // Headers
- //--------------------------------------------------
- final headers = <String, String>{
- '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 [];
- } on TimeoutException {
- return [];
- } catch (e, stack) {
- print(e);
- print(stack);
- return [];
- }
- }
- // Set your action name, define your arguments and return parameter,
- // and then add the boilerplate code using the `</>` button on the right!
|