2022-04-02 19:31:53 +02:00
|
|
|
import 'dart:async';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
2022-06-23 06:14:14 +02:00
|
|
|
import 'package:http_parser/http_parser.dart';
|
|
|
|
import 'package:image_picker/image_picker.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/utils/dio_http_interceptor.dart';
|
2022-06-23 06:14:14 +02:00
|
|
|
import 'package:immich_mobile/utils/files_helper.dart';
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
class NetworkService {
|
2022-06-23 06:14:14 +02:00
|
|
|
late final Dio dio;
|
|
|
|
|
|
|
|
NetworkService() {
|
|
|
|
dio = Dio();
|
|
|
|
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
|
|
|
}
|
|
|
|
|
2022-02-13 23:10:42 +02:00
|
|
|
Future<dynamic> deleteRequest({required String url, dynamic data}) async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
Response res = await dio.delete('$savedEndpoint/$url', data: data);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
} catch (e) {
|
2022-06-18 17:56:36 +02:00
|
|
|
debugPrint("ERROR deleteRequest: ${e.toString()}");
|
2022-02-13 23:10:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 06:14:14 +02:00
|
|
|
Future<dynamic> getRequest(
|
|
|
|
{required String url,
|
|
|
|
bool isByteResponse = false,
|
|
|
|
bool isStreamReponse = false}) async {
|
2022-02-03 18:06:44 +02:00
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
|
2022-04-02 19:31:53 +02:00
|
|
|
if (isByteResponse) {
|
|
|
|
Response<List<int>> res = await dio.get<List<int>>(
|
|
|
|
'$savedEndpoint/$url',
|
|
|
|
options: Options(responseType: ResponseType.bytes),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} else if (isStreamReponse) {
|
|
|
|
Response<ResponseBody> res = await dio.get<ResponseBody>(
|
|
|
|
'$savedEndpoint/$url',
|
|
|
|
options: Options(responseType: ResponseType.stream),
|
|
|
|
);
|
|
|
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Response res = await dio.get('$savedEndpoint/$url');
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
return res;
|
|
|
|
}
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR getRequest: ${e.toString()}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> postRequest({required String url, dynamic data}) async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-23 06:14:14 +02:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.post(validUrl, data: data);
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
2022-04-24 04:08:45 +02:00
|
|
|
return null;
|
2022-02-03 18:06:44 +02:00
|
|
|
} catch (e) {
|
2022-06-18 17:56:36 +02:00
|
|
|
debugPrint("ERROR PostRequest: $e");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> putRequest({required String url, dynamic data}) async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-23 06:14:14 +02:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.put(validUrl, data: data);
|
2022-06-18 17:56:36 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR PutRequest: $e");
|
2022-04-24 04:08:45 +02:00
|
|
|
return null;
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> patchRequest({required String url, dynamic data}) async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-23 06:14:14 +02:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.patch(validUrl, data: data);
|
2022-02-03 18:06:44 +02:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
} catch (e) {
|
2022-06-18 17:56:36 +02:00
|
|
|
debugPrint("ERROR PatchRequest: $e");
|
2022-02-03 18:06:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> pingServer() async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-23 06:14:14 +02:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/server-info/ping').toString();
|
2022-02-03 18:06:44 +02:00
|
|
|
|
2022-06-21 01:10:23 +02:00
|
|
|
debugPrint("ping server at url $validUrl");
|
2022-06-23 06:14:14 +02:00
|
|
|
|
|
|
|
var res = await dio.get(validUrl);
|
2022-02-03 18:06:44 +02:00
|
|
|
var jsonRespsonse = jsonDecode(res.toString());
|
|
|
|
|
2022-06-23 06:14:14 +02:00
|
|
|
return jsonRespsonse["res"] == "pong";
|
2022-02-03 18:06:44 +02:00
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("[PING SERVER] DioError: ${e.response} - $e");
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
2022-06-18 17:56:36 +02:00
|
|
|
debugPrint("ERROR PingServer: $e");
|
2022-02-03 18:06:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|