Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
4.0 kB
2
Indexable
Never
import 'dart:convert';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get_utils/src/platform/platform.dart';
import 'package:qloudlite_app/src/network/result.dart';

import 'custom_interceptors.dart';

class DioClient {
  final baseUrl = 'https://qloud.qatarairways.com.qa/QloudService/';

  final Dio dio = Dio(
    BaseOptions(
      baseUrl: 'https://qloud.qatarairways.com.qa/QloudService/',
      receiveDataWhenStatusError: true,
      connectTimeout: 60 * 1000, // 60 seconds
      receiveTimeout: 60 * 1000,
    ),
  )..interceptors.add(CustomInterceptors());

  // Get method
  Future<void> get() async {
    try {
      Response response = await dio.get(baseUrl + '/users');

      debugPrint('Info ${response.data}');
    } on DioError catch (e) {
      if (e.response != null) {
        debugPrint('DIO Error!');
        debugPrint('Status : ${e.response?.statusCode!}');
        debugPrint('DATA : ${e.response?.data!}');
        debugPrint('HEADERS : ${e.response?.headers}');
      } else {
        debugPrint('Error sending request');
        debugPrint(e.message);
      }
    }
  }

  // Post method login
  Future<Result> post(String api, {var data}) async {
    try {
      var url = baseUrl + api;

      dio.options.headers['Content-Type'] = Headers.jsonContentType;
      dio.options.headers["strPlatform"] =
          GetPlatform.isIOS ? "iOS" : "Android";
      dio.options.headers["strAppVersion"] = "2";
      dio.options.headers["strIsFirstTimeLogin"] = true;
      dio.options.headers["strDeviceCode"] = 'C02NK3KAG3QD';
      dio.options.headers["strDeviceName"] = "iPhone";
      dio.options.headers["strAssignmentType"] = "LT";
      dio.options.headers["strSource"] = "QLite";

      Response response = await dio.post(
        url,
        data: data,
      );
      debugPrint('Info ${response.data}');

      return Result.success(response.data);
    } on DioError catch (e) {
      if (e.error is SocketException) {
        debugPrint('Socket Exception occured! $e');
        return Result.error("Socket Exception occurred!");
      } else {
        debugPrint('Error posting data $e');
        return Result.error(e.toString());
      }
    }
  }

  // Post method fetching roster data
  Future<Result> postRoster(String api, {var postData, String? token}) async {
    try {
      var url = baseUrl + api;

      Map<String, dynamic> jsonMap = {
        "additionalAttributes": "",
        "crewListRequired": true,
        "fromDate": "1-AUG-2022 00:00",
        "onChangeAfter": "",
        "staffNumber": 33930,
        "toDate": "31-Aug-2022 23:59",
        "userName": "QLOUD"
      };
      String jsonString = json.encode(jsonMap); // encode map to json
      List<int> bodyBytes = utf8.encode(jsonString); // utf8 encode


      dio.options.headers["Content-Type"] = Headers.jsonContentType;
      dio.options.headers['content-length'] = bodyBytes.length.toString();

      dio.options.headers["SNO"] = "C02MJLQ3FH00";
      dio.options.headers["User-Location"] = "37.785834,-122.406417";
      dio.options.headers["TokenID"] = "93416dc9-cd1c-49c9-98e5-d375e5b31367";
      dio.options.headers["iPad-Model"] = 'arm64';
      dio.options.headers["VID"] = "052DCCC5-AD57-4749-ADD7-DCBBAA986234";
      dio.options.headers["iOS-Version"] = "15.5";
      dio.options.headers["CREWSTAFFNO"] = 33930;
      dio.options.headers["SSID"] = "QRHQ";
      dio.options.headers["ConnectionPriority"] = 2;
      dio.options.headers["User-Agent"] = "Qloud/4.8.2";

      Response response = await dio.post(
        url,
        data: postData,
      );
      debugPrint('Info ${response.data}');

      return Result.success(response.data);
    } on DioError catch (e) {
      if (e.error is SocketException) {
        debugPrint('Socket Exception occured! $e');
        return Result.error("Socket Exception occurred!");
      } else {
        debugPrint('Error posting data $e');
        return Result.error(e.toString());
      }
    }
  }
}