2023-03-22 01:14:58 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:logging/logging.dart';
|
2023-05-09 20:42:53 +00:00
|
|
|
import '../globals.dart';
|
2023-03-22 01:14:58 +00:00
|
|
|
import 'package:result_monad/result_monad.dart';
|
|
|
|
|
|
|
|
import '../friendica_client/paged_response.dart';
|
|
|
|
import '../models/exec_error.dart';
|
|
|
|
|
|
|
|
final _logger = Logger('NetworkUtils');
|
|
|
|
|
2023-05-09 20:42:53 +00:00
|
|
|
http.Response requestTimeout() => http.Response('Client side timeout', 408);
|
|
|
|
|
2023-03-22 01:14:58 +00:00
|
|
|
FutureResult<PagedResponse<String>, ExecError> getUrl(
|
|
|
|
Uri url, {
|
|
|
|
Map<String, String>? headers,
|
|
|
|
}) async {
|
|
|
|
_logger.finer('GET: $url');
|
|
|
|
try {
|
2023-05-09 20:42:53 +00:00
|
|
|
final request = http.get(
|
2023-03-22 01:14:58 +00:00
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
);
|
|
|
|
|
2023-05-09 20:42:53 +00:00
|
|
|
final response = await request.timeout(
|
|
|
|
apiCallTimeout,
|
|
|
|
onTimeout: requestTimeout,
|
|
|
|
);
|
|
|
|
|
2023-03-22 01:14:58 +00:00
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.authentication,
|
|
|
|
message: '${response.statusCode}: ${response.reasonPhrase}'));
|
|
|
|
}
|
|
|
|
return PagedResponse.fromLinkHeader(
|
|
|
|
response.headers['link'],
|
|
|
|
utf8.decode(response.bodyBytes),
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
return Result.error(
|
|
|
|
ExecError(type: ErrorType.localError, message: e.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<String, ExecError> postUrl(
|
|
|
|
Uri url,
|
|
|
|
Map<String, dynamic> body, {
|
|
|
|
Map<String, String>? headers,
|
|
|
|
}) async {
|
|
|
|
_logger.finer('POST: $url \n Body: $body');
|
|
|
|
try {
|
2023-05-09 20:42:53 +00:00
|
|
|
final request = http.post(
|
2023-03-22 01:14:58 +00:00
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
body: jsonEncode(body),
|
|
|
|
);
|
|
|
|
|
2023-05-09 20:42:53 +00:00
|
|
|
final response = await request.timeout(
|
|
|
|
apiCallTimeout,
|
|
|
|
onTimeout: requestTimeout,
|
|
|
|
);
|
|
|
|
|
2023-03-22 01:14:58 +00:00
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.authentication,
|
|
|
|
message: '${response.statusCode}: ${response.reasonPhrase}'));
|
|
|
|
}
|
|
|
|
return Result.ok(utf8.decode(response.bodyBytes));
|
|
|
|
} catch (e) {
|
|
|
|
return Result.error(
|
|
|
|
ExecError(type: ErrorType.localError, message: e.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<String, ExecError> putUrl(
|
|
|
|
Uri url,
|
|
|
|
Map<String, dynamic> body, {
|
|
|
|
Map<String, String>? headers,
|
|
|
|
}) async {
|
|
|
|
_logger.finer('PUT: $url \n Body: $body');
|
|
|
|
try {
|
2023-05-09 20:42:53 +00:00
|
|
|
final request = http.put(
|
2023-03-22 01:14:58 +00:00
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
body: jsonEncode(body),
|
|
|
|
);
|
|
|
|
|
2023-05-09 20:42:53 +00:00
|
|
|
final response = await request.timeout(
|
|
|
|
apiCallTimeout,
|
|
|
|
onTimeout: requestTimeout,
|
|
|
|
);
|
|
|
|
|
2023-03-22 01:14:58 +00:00
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.authentication,
|
|
|
|
message: '${response.statusCode}: ${response.reasonPhrase}'));
|
|
|
|
}
|
|
|
|
return Result.ok(utf8.decode(response.bodyBytes));
|
|
|
|
} catch (e) {
|
|
|
|
return Result.error(
|
|
|
|
ExecError(type: ErrorType.localError, message: e.toString()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureResult<String, ExecError> deleteUrl(
|
|
|
|
Uri url,
|
|
|
|
Map<String, dynamic> body, {
|
|
|
|
Map<String, String>? headers,
|
|
|
|
}) async {
|
|
|
|
_logger.finer('DELETE: $url');
|
|
|
|
try {
|
2023-05-09 20:42:53 +00:00
|
|
|
final request = http.delete(
|
2023-03-22 01:14:58 +00:00
|
|
|
url,
|
|
|
|
headers: headers,
|
|
|
|
body: jsonEncode(body),
|
|
|
|
);
|
|
|
|
|
2023-05-09 20:42:53 +00:00
|
|
|
final response = await request.timeout(
|
|
|
|
apiCallTimeout,
|
|
|
|
onTimeout: requestTimeout,
|
|
|
|
);
|
|
|
|
|
2023-03-22 01:14:58 +00:00
|
|
|
if (response.statusCode != 200) {
|
|
|
|
return Result.error(ExecError(
|
|
|
|
type: ErrorType.authentication,
|
|
|
|
message: '${response.statusCode}: ${response.reasonPhrase}'));
|
|
|
|
}
|
|
|
|
return Result.ok(utf8.decode(response.bodyBytes));
|
|
|
|
} catch (e) {
|
|
|
|
return Result.error(
|
|
|
|
ExecError(type: ErrorType.localError, message: e.toString()));
|
|
|
|
}
|
|
|
|
}
|