nc-photos/np_api/lib/src/api.dart

121 lines
3.2 KiB
Dart
Raw Normal View History

2022-12-08 16:39:13 +01:00
import 'package:flutter/foundation.dart';
2021-04-10 06:28:12 +02: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 16:01:04 +01:00
import 'package:np_codegen/np_codegen.dart';
2024-07-30 19:34:06 +02:00
import 'package:np_http/np_http.dart';
2021-04-10 06:28:12 +02:00
import 'package:xml/xml.dart';
2022-12-08 16:39:13 +01:00
part 'api.g.dart';
2023-01-07 10:57:57 +01:00
part 'direct_api.dart';
part 'face_recognition_api.dart';
part 'files_api.dart';
part 'files_sharing_api.dart';
part 'photos_api.dart';
part 'recognize_api.dart';
part 'status_api.dart';
2023-01-07 10:57:57 +01:00
part 'systemtag_api.dart';
2022-12-08 16:39:13 +01:00
2022-12-16 16:01:04 +01:00
@npLog
2021-04-10 06:28:12 +02:00
class Api {
const Api(this.baseUrl, BasicAuth this.auth);
const Api.fromBaseUrl(this.baseUrl) : auth = null;
2021-04-10 06:28:12 +02:00
2022-07-08 16:52:18 +02:00
ApiFiles files() => ApiFiles(this);
2022-07-08 16:52:18 +02:00
ApiOcs ocs() => ApiOcs(this);
ApiPhotos photos(String userId) => ApiPhotos(this, userId);
ApiRecognize recognize(String userId) => ApiRecognize(this, userId);
ApiStatus status() => ApiStatus(this);
2022-07-08 16:52:18 +02:00
ApiSystemtags systemtags() => ApiSystemtags(this);
2022-07-08 16:52:18 +02:00
ApiSystemtagsRelations systemtagsRelations() => ApiSystemtagsRelations(this);
2021-04-10 06:28:12 +02:00
Future<Response> request(
String method,
String endpoint, {
2021-07-23 22:05:57 +02:00
Map<String, String>? header,
2021-08-05 06:50:36 +02:00
Map<String, String>? queryParameters,
2021-07-23 22:05:57 +02:00
String? body,
Uint8List? bodyBytes,
2021-04-10 06:28:12 +02:00
bool isResponseString = true,
}) async {
2021-08-05 06:50:36 +02: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 06:28:12 +02:00
});
}
2021-04-10 06:28:12 +02: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;
}
2023-05-20 10:00:18 +02:00
_log.finer(req.url);
2021-04-10 06:28:12 +02:00
final response =
await http.Response.fromStream(await getHttpClient().send(req));
if (!isHttpStatusGood(response.statusCode)) {
2021-05-23 19:26:28 +02: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 06:28:12 +02:00
}
return Response(response.statusCode, response.headers,
isResponseString ? response.body : response.bodyBytes);
}
2021-08-05 06:50:36 +02: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 07:26:26 +02:00
} else {
return Uri.https(baseUrl.authority, path, queryParameters);
2021-05-06 07:26:26 +02:00
}
}
final Uri baseUrl;
final BasicAuth? auth;
2021-04-10 06:28:12 +02:00
}
2022-07-08 16:52:18 +02:00
class ApiOcs {
ApiOcs(this._api);
2021-08-05 07:48:32 +02:00
2022-07-08 16:52:18 +02:00
ApiOcsDav dav() => ApiOcsDav(this);
2022-07-08 16:52:18 +02:00
ApiOcsFacerecognition facerecognition() => ApiOcsFacerecognition(this);
2022-07-08 16:52:18 +02:00
ApiOcsFilesSharing filesSharing() => ApiOcsFilesSharing(this);
2021-08-05 07:48:32 +02:00
2021-09-15 08:58:06 +02:00
final Api _api;
2021-08-05 07:48:32 +02:00
}
2022-07-08 16:52:18 +02:00
class ApiOcsDav {
ApiOcsDav(this._ocs);
2021-08-05 07:48:32 +02:00
2022-07-08 16:52:18 +02:00
ApiOcsDavDirect direct() => ApiOcsDavDirect(this);
2021-08-05 07:48:32 +02:00
2022-07-08 16:52:18 +02:00
final ApiOcs _ocs;
2021-08-05 07:48:32 +02:00
}