Fakultas Ilmu Komputer UI

Commit e0cfb026 authored by Firriyal Bin Yahya's avatar Firriyal Bin Yahya
Browse files

benerin conflict

parent 2c94c75a
Pipeline #40939 failed with stages
in 3 minutes and 12 seconds
import 'dart:convert';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:ppl_disabilitas/config/strings.dart';
class CookiesInterface {
Future<bool> checkCookieFileAvailability({String fileName}) async {
Directory dir;
await getApplicationDocumentsDirectory().then((Directory directory) {
dir = directory;
});
File cookieFile = File("${dir.path}/$fileName.json");
bool cookiesExist = cookieFile.existsSync();
return cookiesExist;
}
Future<File> createSignInCookie({
Map<String, dynamic> responseHeaders}) async {
try {
String setCookie;
String csrfToken;
String sessionId;
String userKey;
List<String> cookiesList;
Directory dir;
await getApplicationDocumentsDirectory().then((Directory directory) {
dir = directory;
});
File cookieFile = File("${dir.path}/usercookies.json");
cookieFile.createSync();
setCookie = responseHeaders["set-cookie"];
if (setCookie != null) {
csrfToken = setCookie.split(";")[0].split("=")[1];
sessionId = setCookie.split(";")[4].split(",")[1].split("=")[1];
userKey = key;
}
cookiesList = <String>[
csrfToken,
sessionId,
userKey,
];
cookieFile.writeAsStringSync(json.encode(cookiesList));
return cookieFile;
} on Exception catch (e) {
print(e.toString());
rethrow;
}
}
Future<File> createSearchHistoryCookie({
Map<String, dynamic> recentSearch}) async {
print("recent searrch $recentSearch");
Directory dir;
List currentSearchHistory;
try {
await getApplicationDocumentsDirectory().then((Directory directory) {
dir = directory;
});
File cookieFile = File('${dir.path}/searchhistory.json');
cookieFile.createSync();
await checkCookieFileAvailability(fileName: "searchhistory").then((available) async {
if (available) {
await getCookieFile(fileName: "searchhistory").then((cookie) {
if (cookie == null) {
currentSearchHistory = [];
} else {
currentSearchHistory = cookie == "" ? [] : json.decode(cookie);
}
currentSearchHistory.insert(0, recentSearch);
});
} else {
currentSearchHistory = [];
}
await cookieFile.writeAsString(json.encode(currentSearchHistory));
});
return cookieFile;
} on Exception catch (e) {
print(e.toString());
rethrow;
}
}
Future<dynamic> getCookieFile({String fileName}) async {
Directory dir;
await getApplicationDocumentsDirectory().then((Directory directory) {
dir = directory;
});
File file = File("${dir.path}/$fileName.json");
dynamic res;
try {
res = file.readAsStringSync();
} on Exception {
res = [];
}
return res;
}
}
\ No newline at end of file
class NetworkModel<T> {
Status status;
T data;
String message;
NetworkModel.loading(this.message) : status = Status.LOADING;
NetworkModel.completed(this.data) : status = Status.COMPLETED;
NetworkModel.error(this.message) : status = Status.ERROR;
@override
String toString() {
return "Status : $status \n Message : $message \n Data : $data";
}
}
enum Status { LOADING, COMPLETED, ERROR }
import 'dart:convert';
import 'package:ppl_disabilitas/network/CustomException.dart';
import 'package:http/http.dart' as http;
import 'dart:io';
class NetworkInterface {
//String key = KEY;
// POST request
Future<dynamic> post({
String url, //url nya apa
dynamic bodyParams, //data apa yang mau dikasih
bool isLogin, //dia login apa ngga
}) async {
var responseJson;
Map<String, String> headersJson =
await _buildRequestHeader(isLogin); //butuh header apa ngga
try {
final response = await http.post(
"$url",
body: json.encode(bodyParams),
headers: headersJson,
);
responseJson = _response(response);
} on SocketException {
throw FetchDataException("No Internet Connection");
}
return responseJson;
}
// GET request
Future<dynamic> get({
String url,
bool isLogin,
}) async {
var responseJson;
Map<String, dynamic> headersJson = await _buildRequestHeader(isLogin);
try {
final response = await http.get(
"$url",
headers: headersJson,
);
responseJson = _response(response);
} on SocketException {
throw FetchDataException("No Internet Connection");
}
return responseJson;
}
// buildRequestHeader: untuk nentuin pake header apa aja berdasarkan login apa ngga
Future<Map<String, dynamic>> _buildRequestHeader(bool isLogin) async {
Map<String, String> headers = Map<String, String>();
headers.putIfAbsent("Content-Type", () => "application/json");
//if (isLogin) {
//List<dynamic> cookieFile = await CookiesInterface().getCookieFile(
// fileName:
// "userCookies"); //ngambil data dari yg udh disimpen di cookie
//print("cookieFile list --> ${cookieFile.toString()}");
//print("check key here >>> $key");
//setKey(cookieFile[2]);
//key = cookieFile[2];
//headers.putIfAbsent(
// "Authorization",
// () =>
// 'Token $key'); //ini kalau authorization nya ngga ada baru taro token nya
//headers.putIfAbsent("X-CSRFToken", () => cookieFile[0]); //csrf token
//headers.putIfAbsent(
// "Cookie",
// () =>
// "csrftoken=${cookieFile[0]};sessionid=${cookieFile[1]}"); //cookie file
//print("headers --> ${headers}");
//}
return headers;
}
dynamic _response(http.Response response) {
switch (response.statusCode) {
case 200:
var responseJson = json.decode(response.body.toString());
return responseJson;
case 400:
throw BadRequestException(response.body.toString());
case 401:
case 403:
throw UnauthorisedException(response.body.toString());
case 500:
default:
throw FetchDataException(
'Error occured while Communication with Server with status : ${response.statusCode}');
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment