fetchQRCode method

Future<Uint8List> fetchQRCode()

Implementation

Future<Uint8List> fetchQRCode() async {
  String requestTime = DateTime.now().toString();
  String? partnerId = SecureStorageService.readSecureData(
    SecureStorageService.partnerIdLogin,
  );
  try {
    // Read the Partner ID from secure storage

    // Construct the API URL
    final String url =
        '${ApiConstant.ffmBaseUrl}ffm-visit-svc/qrCode/generate?partnerid=$partnerId';

    // Log the URL being hit
    debugPrint("Fetching QR Code from URL: $url");

    // Make the API request
    final response = await Dio().get(
      url,
      options: Options(
        headers: {
          'Authorization': "Bearer ${SecureStorageService.readSecureData(
            SecureStorageService.accessToken,
          )}",
          'Content-Type': 'application/json',
          'X-User-Id': SecureStorageService.readSecureData(
                SecureStorageService.xUserId,
              ) ??
              "",
          'X-UserId': SecureStorageService.readSecureData(
                SecureStorageService.xUserId,
              ) ??
              "",
          'X-Channel': Constants.xChannel,
        },
        responseType: ResponseType.bytes, // Expect binary data
      ),
    );

    // Check the response status
    if (response.statusCode == 200) {
      UDID.setTraceId(response.headers.map[Constants.traceIdKey]?[0] ?? "");

      Helper.logEvent(
        "RESPONSE_EVENT",
        success: true,
        endPoint: "ffm-visit-svc/qrCode/generate?partnerid=$partnerId",
        responseDate: DateTime.now().toString(),
        screenName: "qrGenerate",
        requestDate: requestTime,
      );
      ConditionalLogs().customLog(
          "QR Code fetched successfully for Partner ID: $partnerId");
      var headers = response.headers;
      ConditionalLogs()
          .customLog("QR Code fetched successfully for header : $headers");
      qrcode_generate_count.value =
          int.parse(headers["qr-expiry"]?.first ?? "60");
      ConditionalLogs().customLog("Expiry:$qrcode_generate_count");
      startApiPolling();
      return response.data; // Return binary data as Uint8List
    } else {
      qrcode_generate_count.value = 60;
      ConditionalLogs().customLog(
          "Failed to fetch QR Code. Status Code: ${response.statusCode}");
    }
  } on DioError catch (dioError) {
    qrcode_generate_count.value = 60;
    if (dioError is DioException) {
      UDID.setTraceId(
        dioError.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "ERROR_EVENT",
        failure: true,
        requestDate: requestTime,
        endPoint: "ffm-visit-svc/qrCode/generate?partnerid=$partnerId",
        responseDate: DateTime.now().toString(),
        screenName: "qrGenerate",
        error: dioError,
      );
    }
    // Log details about the DioError
    if (dioError.response != null) {
      print("DioError: ${dioError.message}");
      print("Response status: ${dioError.response?.statusCode}");
      print("Response data: ${dioError.response?.data}");
      print("Request headers: ${dioError.response?.headers}");
    } else {
      print("DioError without response: ${dioError.message}");
    }
  } catch (error) {
    qrcode_generate_count.value = 60;
    if (error is DioException) {
      UDID.setTraceId(
        error.response?.headers.map[Constants.traceIdKey]?[0] ?? "",
      );
      Helper.logEvent(
        "ERROR_EVENT",
        failure: true,
        requestDate: requestTime,
        endPoint: "ffm-visit-svc/qrCode/generate?partnerid=$partnerId",
        responseDate: DateTime.now().toString(),
        screenName: "qrGenerate",
        error: error,
      );
    }
    // Handle any other errors
  }

  return Uint8List(0);
}