2022-04-02 10:31:53 -07:00
|
|
|
import 'dart:async';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:hive/hive.dart';
|
2022-06-25 11:46:51 -07:00
|
|
|
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
2022-02-03 09:06:44 -07:00
|
|
|
import 'package:immich_mobile/constants/hive_box.dart';
|
|
|
|
import 'package:immich_mobile/utils/dio_http_interceptor.dart';
|
2022-06-25 11:46:51 -07:00
|
|
|
|
|
|
|
final networkServiceProvider = Provider((_) => NetworkService());
|
2022-02-03 09:06:44 -07:00
|
|
|
|
|
|
|
class NetworkService {
|
2022-06-22 21:14:14 -07:00
|
|
|
late final Dio dio;
|
|
|
|
|
|
|
|
NetworkService() {
|
|
|
|
dio = Dio();
|
|
|
|
dio.interceptors.add(AuthenticatedRequestInterceptor());
|
|
|
|
}
|
|
|
|
|
2022-02-13 14:10:42 -07: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 08:56:36 -07:00
|
|
|
debugPrint("ERROR deleteRequest: ${e.toString()}");
|
2022-02-13 14:10:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-22 21:14:14 -07:00
|
|
|
Future<dynamic> getRequest(
|
|
|
|
{required String url,
|
|
|
|
bool isByteResponse = false,
|
|
|
|
bool isStreamReponse = false}) async {
|
2022-02-03 09:06:44 -07:00
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
|
|
|
|
2022-04-02 10:31:53 -07: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 09:06:44 -07: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-22 21:14:14 -07:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.post(validUrl, data: data);
|
2022-02-03 09:06:44 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
2022-04-23 19:08:45 -07:00
|
|
|
return null;
|
2022-02-03 09:06:44 -07:00
|
|
|
} catch (e) {
|
2022-06-18 08:56:36 -07: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-22 21:14:14 -07:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.put(validUrl, data: data);
|
2022-06-18 08:56:36 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
|
|
|
debugPrint("ERROR PutRequest: $e");
|
2022-04-23 19:08:45 -07:00
|
|
|
return null;
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> patchRequest({required String url, dynamic data}) async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-22 21:14:14 -07:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/$url').toString();
|
|
|
|
var res = await dio.patch(validUrl, data: data);
|
2022-02-03 09:06:44 -07:00
|
|
|
|
|
|
|
return res;
|
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("DioError: ${e.response}");
|
|
|
|
} catch (e) {
|
2022-06-18 08:56:36 -07:00
|
|
|
debugPrint("ERROR PatchRequest: $e");
|
2022-02-03 09:06:44 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> pingServer() async {
|
|
|
|
try {
|
|
|
|
var savedEndpoint = Hive.box(userInfoBox).get(serverEndpointKey);
|
2022-06-22 21:14:14 -07:00
|
|
|
var validUrl = Uri.parse('$savedEndpoint/server-info/ping').toString();
|
2022-02-03 09:06:44 -07:00
|
|
|
|
2022-06-20 16:10:23 -07:00
|
|
|
debugPrint("ping server at url $validUrl");
|
2022-06-22 21:14:14 -07:00
|
|
|
|
|
|
|
var res = await dio.get(validUrl);
|
2022-02-03 09:06:44 -07:00
|
|
|
var jsonRespsonse = jsonDecode(res.toString());
|
|
|
|
|
2022-06-22 21:14:14 -07:00
|
|
|
return jsonRespsonse["res"] == "pong";
|
2022-02-03 09:06:44 -07:00
|
|
|
} on DioError catch (e) {
|
|
|
|
debugPrint("[PING SERVER] DioError: ${e.response} - $e");
|
|
|
|
return false;
|
|
|
|
} catch (e) {
|
2022-06-18 08:56:36 -07:00
|
|
|
debugPrint("ERROR PingServer: $e");
|
2022-02-03 09:06:44 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|