1
0
Fork 0
mirror of https://gitlab.com/nkming2/nc-photos.git synced 2025-03-29 02:11:37 +01:00
nc-photos/np_api/lib/src/api.dart

110 lines
2.8 KiB
Dart
Raw Normal View History

2022-12-08 23:39:13 +08:00
import 'package:flutter/foundation.dart';
2021-04-10 12:28:12 +08:00
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:np_api/src/type.dart';
import 'package:np_api/src/util.dart';
2022-12-16 23:01:04 +08:00
import 'package:np_codegen/np_codegen.dart';
2021-04-10 12:28:12 +08:00
import 'package:xml/xml.dart';
2022-12-08 23:39:13 +08:00
part 'api.g.dart';
2023-01-07 17:57:57 +08:00
part 'direct_api.dart';
part 'face_recognition_api.dart';
part 'files_api.dart';
part 'files_sharing_api.dart';
part 'systemtag_api.dart';
2022-12-08 23:39:13 +08:00
2022-12-16 23:01:04 +08:00
@npLog
2021-04-10 12:28:12 +08:00
class Api {
const Api(this.baseUrl, BasicAuth this.auth);
const Api.fromBaseUrl(this.baseUrl) : auth = null;
2021-04-10 12:28:12 +08:00
2022-07-08 22:52:18 +08:00
ApiFiles files() => ApiFiles(this);
2022-07-08 22:52:18 +08:00
ApiOcs ocs() => ApiOcs(this);
2022-07-08 22:52:18 +08:00
ApiSystemtags systemtags() => ApiSystemtags(this);
2022-07-08 22:52:18 +08:00
ApiSystemtagsRelations systemtagsRelations() => ApiSystemtagsRelations(this);
2021-04-10 12:28:12 +08:00
Future<Response> request(
String method,
String endpoint, {
2021-07-24 04:05:57 +08:00
Map<String, String>? header,
2021-08-05 12:50:36 +08:00
Map<String, String>? queryParameters,
2021-07-24 04:05:57 +08:00
String? body,
Uint8List? bodyBytes,
2021-04-10 12:28:12 +08:00
bool isResponseString = true,
}) async {
2021-08-05 12:50:36 +08:00
final url = _makeUri(endpoint, queryParameters: queryParameters);
final req = http.Request(method, url);
if (auth != null) {
req.headers.addAll({
"authorization": auth!.toHeaderValue(),
2021-04-10 12:28:12 +08:00
});
}
2021-04-10 12:28:12 +08:00
if (header != null) {
// turn all to lower case, since HTTP headers are case-insensitive, this
// smooths our processing (if any)
req.headers.addEntries(
header.entries.map((e) => MapEntry(e.key.toLowerCase(), e.value)));
}
if (body != null) {
req.body = body;
} else if (bodyBytes != null) {
req.bodyBytes = bodyBytes;
}
final response =
await http.Response.fromStream(await http.Client().send(req));
if (!isHttpStatusGood(response.statusCode)) {
2021-05-24 01:26:28 +08:00
if (response.statusCode == 404) {
_log.severe(
"[request] HTTP $method (${response.statusCode}): $endpoint",
);
} else {
_log.severe(
"[request] HTTP $method (${response.statusCode}): $endpoint",
response.body,
);
}
2021-04-10 12:28:12 +08:00
}
return Response(response.statusCode, response.headers,
isResponseString ? response.body : response.bodyBytes);
}
2021-08-05 12:50:36 +08:00
Uri _makeUri(
String endpoint, {
Map<String, String>? queryParameters,
}) {
final path = "${baseUrl.path}/$endpoint";
if (baseUrl.scheme == "http") {
return Uri.http(baseUrl.authority, path, queryParameters);
2021-05-06 13:26:26 +08:00
} else {
return Uri.https(baseUrl.authority, path, queryParameters);
2021-05-06 13:26:26 +08:00
}
}
final Uri baseUrl;
final BasicAuth? auth;
2021-04-10 12:28:12 +08:00
}
2022-07-08 22:52:18 +08:00
class ApiOcs {
ApiOcs(this._api);
2021-08-05 13:48:32 +08:00
2022-07-08 22:52:18 +08:00
ApiOcsDav dav() => ApiOcsDav(this);
2022-07-08 22:52:18 +08:00
ApiOcsFacerecognition facerecognition() => ApiOcsFacerecognition(this);
2022-07-08 22:52:18 +08:00
ApiOcsFilesSharing filesSharing() => ApiOcsFilesSharing(this);
2021-08-05 13:48:32 +08:00
2021-09-15 14:58:06 +08:00
final Api _api;
2021-08-05 13:48:32 +08:00
}
2022-07-08 22:52:18 +08:00
class ApiOcsDav {
ApiOcsDav(this._ocs);
2021-08-05 13:48:32 +08:00
2022-07-08 22:52:18 +08:00
ApiOcsDavDirect direct() => ApiOcsDavDirect(this);
2021-08-05 13:48:32 +08:00
2022-07-08 22:52:18 +08:00
final ApiOcs _ocs;
2021-08-05 13:48:32 +08:00
}