Untitled

 avatar
unknown
plain_text
10 months ago
1.6 kB
4
Indexable
import 'package:diamond_lease/support/config.dart';
import 'package:dio/dio.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ApiAgent {
  static final BaseOptions baseOptions = BaseOptions(
    responseType: ResponseType.json,
    receiveDataWhenStatusError: true,
    followRedirects: true,
    headers: {
      "Accept": "application/json",
    },
    validateStatus: (status) {
      return status == 200 || status == 422 || status == 413;
    },
  );
  static post({
    required String url,
    required params,
    bool? authorization = false,
    bool? hasFile = false,
  }) async {
    baseOptions.baseUrl = Config.BASE_URL;
    var token = '';
    if (authorization == true) {
      SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      token = sharedPreferences.getString('token').toString();
      baseOptions.headers = {
        "Accept": "application/json",
        "Authorization": "Bearer $token",
      };
    }
    if (hasFile!) {
      baseOptions.headers = {
        "Content-type": "multipart/form-data",
        "Accept": "application/json",
        "Authorization": "Bearer $token",
      };
    }
    Dio dio = Dio(baseOptions);
    var response = await dio.post(
      url,
      data: params,
    );
    return response;
  }

  static get({required String url}) async {
    Dio dio = Dio(baseOptions);
    var response = await dio.get(
      url,
    );
    return response;
  }

  static download({required String url, required String filePath}) async {
    Dio dio = Dio(baseOptions);
    var response = await dio.download(url, filePath);
    return response;
  }
}
Editor is loading...
Leave a Comment