fetchId method

Helper method to get the ID from the backend

Implementation

Future<RequestResponse<String?>> fetchId() async {
  final url = config.hostUrl;
  final data = <String, String>{'cmd': 'renovation_bench.get_client_id'};

  final options = Options();
  options.headers = <String, String>{
    'Accept': 'application/json',
    'content-type': 'application/json',
    'x-client-site': 'renovation_manager'
  };
  final dio = Dio();
  Response<String> response;
  Map<String, dynamic>? jsonResponse;
  try {
    response = await dio.post<String>(url, data: data, options: options);

    if (response.statusCode! / 100 == 2) {
      return RequestResponse.success<String?>(
          json.decode(response.data!)['message']);
    }

    jsonResponse = Request.convertToMap(response);
    return RequestResponse.fail<String>(handleError(
        'FetchId',
        ErrorDetail(
            info: Information(
                httpCode: response.statusCode,
                data: jsonResponse ?? response.data))));
  } on DioError catch (e) {
    return RequestResponse.fail<String>(handleError(
        'FetchId',
        ErrorDetail(
            info: Information(
                httpCode: e.response!.statusCode,
                data: Request.isJsonResponse(e.response!)
                    ? Request.convertToMap(e.response!)
                    : e.response!.data,
                rawError: e))));
  }
}