mobileApplicationsKonectarApp/lib/utils/mockapi.dart

212 lines
7.5 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:flutter/services.dart';
import 'package:http_mock_adapter/http_mock_adapter.dart';
import 'package:path_provider/path_provider.dart';
import 'package:pwa_ios/model/interaction_config_data.dart';
class MockApiCall {
Future<dynamic> getConfigData() async {
// final dio = Dio(BaseOptions());
final dio = Dio();
(dio.httpClientAdapter as IOHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
//final dioAdapter = DioAdapter(dio: dio);
dynamic jsonResult = jsonDecode(
await rootBundle.loadString("assets/images/interactiondata.json"));
// Set up a mock response for GET requests to "https://example.com"
// dio.get('192.168.2.64/passvault/public/forms'
// (server) => server.reply(
// 200,
// jsonResult,
// // Delay the response by 1 second
// delay: const Duration(seconds: 1),
// ),
// );
// Send a GET request to "https://example.com" using Dio
final response =
await dio.get('http://192.168.2.64/konectar-app/public/forms');
UIDataResponse dataResponse = UIDataResponse.fromJson(response.data);
// The response should contain the mock data we registered
print("response");
print(response.data);
return dataResponse;
}
Future<dynamic> postConfigData(var jsonObj) async {
// final dio = Dio(BaseOptions());
final dio = Dio();
(dio.httpClientAdapter as IOHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
//final dioAdapter = DioAdapter(dio: dio);
dynamic jsonResult = jsonDecode(
await rootBundle.loadString("assets/images/interactiondata.json"));
// Set up a mock response for GET requests to "https://example.com"
// dio.get('192.168.2.64/passvault/public/forms'
// (server) => server.reply(
// 200,
// jsonResult,
// // Delay the response by 1 second
// delay: const Duration(seconds: 1),
// ),
// );
// Send a GET request to "https://example.com" using Dio
final response = await dio
.post('http://192.168.2.64/konectar-app/public/forms', data: jsonObj);
UIDataResponse dataResponse = UIDataResponse.fromJson(response.data);
// The response should contain the mock data we registered
print("response");
print(response.data);
return dataResponse;
}
Future<void> getConfigWidgetData() async {
final dio = Dio(BaseOptions());
final dioAdapter = DioAdapter(dio: dio);
dynamic jsonResult = jsonDecode(
await rootBundle.loadString("assets/images/interactiondata.json"));
// Set up a mock response for GET requests to "https://example.com"
dioAdapter.onGet(
'https://example.com',
(server) => server.reply(
200,
jsonResult,
// Delay the response by 1 second
delay: const Duration(seconds: 1),
),
);
// Send a GET request to "https://example.com" using Dio
final response = await dio.get('https://example.com');
// The response should contain the mock data we registered
print(response.data); // {messa
}
Future<void> postFormData(var jsonObj) async {
final dio = Dio(BaseOptions());
final dioAdapter = DioAdapter(dio: dio);
// final body = json.decode(jsonObj.toString());
// print(body);
// Set up a mock response for GET requests to "https://example.com"
dioAdapter.onPost(
'https://example.com',
data: jsonObj,
(server) => server.reply(
200,
{"message": "success"},
// Delay the response by 1 second
delay: const Duration(seconds: 1),
),
);
// Send a GET request to "https://example.com" using Dio
final response = await dio.post(
'http://192.168.2.64/konectar-app/public/push_data',
data: jsonObj);
// The response should contain the mock data we registered
print(response.data); // {messa
}
Future<dynamic> postSavedData(var jsonObj, String filepath) async {
// final dio = Dio(BaseOptions());
final dio = Dio();
(dio.httpClientAdapter as IOHttpClientAdapter).onHttpClientCreate =
(HttpClient client) {
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => true;
return client;
};
// MultipartFile.fromFileSync("/Users/aissel/Library/Developer/CoreSimulator/Devices/1E435121-7E65-45C6-9E0B-411C8B9915F5/data/Containers/Data/Application/3CBC1CFF-79AD-49FA-A6E0-13D0AA2959D2/tmp/Flutter Questionaire.pdf",
// filename: "upload.txt"),\
// File file = File(filepath);
// String fileName = file.path.split('/').last;
// // Get the application folder directory
// Directory? directory = Platform.isAndroid
// ? await getExternalStorageDirectory() //FOR ANDROID
// : await getApplicationSupportDirectory(); //FOR ios
// // Copy file to the directory
// // if (directory != null) {
// File newFile = await file.copy('${directory!.path}/$fileName');
// // File file = File(filepath);
// List<int> imageBytes = await newFile.readAsBytes();
// Uint8List imageUint8List = Uint8List.fromList(imageBytes);
// String base64Image = base64Encode(imageUint8List);
// var formData = FormData.fromMap({
// "files": [await MultipartFile.fromFile(filepath)],
// "data": jsonObj
// });
// Send a GET request to "https://example.com" using Dio
final response = await dio.get(
'http://192.168.2.64/konectar-app/public/push_data',
data: jsonObj);
//UIDataResponse dataResponse = UIDataResponse.fromJson(response.data);
// The response should contain the mock data we registered
print("response");
print(response.data);
return response.data;
}
Future<bool> uploadFileAndJsonData(var jsonObj) async {
// {required File empFace, required String empCode}
final url = 'http://192.168.2.64/konectar-app/public/push_data';
try {
var formData = FormData.fromMap({
"files": [
MultipartFile.fromFileSync(
"/Users/aissel/Library/Developer/CoreSimulator/Devices/1E435121-7E65-45C6-9E0B-411C8B9915F5/data/Containers/Data/Application/3CBC1CFF-79AD-49FA-A6E0-13D0AA2959D2/tmp/Flutter Questionaire.pdf",
filename: "upload.txt"),
MultipartFile.fromFileSync("./example/upload.txt",
filename: "upload.txt"),
],
"data": jsonObj
});
final response = await Dio().post(
url,
data: jsonObj,
);
if (response.statusCode == 200) {
var map = response.data as Map;
print("result $map");
print('success');
if (map['status'] == 'Successfully registered') {
return true;
} else {
return false;
}
} else if (response.statusCode != 200) {
var map = response.data as Map;
print("result $map");
//BotToast is a package for toasts available on pub.dev
// BotToast.showText(text: 'Validation Error Occurs');
return false;
}
} on DioException catch (error) {
log(error.message!);
throw 'Something Went Wrong';
} catch (_) {
log(_.toString());
throw 'Something Went Wrong';
}
return false;
}
}