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';
|
2023-02-23 15:49:17 +01:00
|
|
|
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';
|
2023-04-13 17:32:31 +02:00
|
|
|
part 'photos_api.dart';
|
2023-06-15 17:31:22 +02:00
|
|
|
part 'recognize_api.dart';
|
2023-04-13 17:32:31 +02:00
|
|
|
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 {
|
2023-02-23 15:49:17 +01:00
|
|
|
const Api(this.baseUrl, BasicAuth this.auth);
|
2022-11-20 16:35:53 +01:00
|
|
|
|
2023-02-23 15:49:17 +01:00
|
|
|
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-11-20 16:35:53 +01:00
|
|
|
|
2022-07-08 16:52:18 +02:00
|
|
|
ApiOcs ocs() => ApiOcs(this);
|
2022-11-20 16:35:53 +01:00
|
|
|
|
2023-04-13 17:32:31 +02:00
|
|
|
ApiPhotos photos(String userId) => ApiPhotos(this, userId);
|
|
|
|
|
2023-06-18 19:14:47 +02:00
|
|
|
ApiRecognize recognize(String userId) => ApiRecognize(this, userId);
|
|
|
|
|
2023-05-10 18:42:56 +02:00
|
|
|
ApiStatus status() => ApiStatus(this);
|
|
|
|
|
2022-07-08 16:52:18 +02:00
|
|
|
ApiSystemtags systemtags() => ApiSystemtags(this);
|
2022-11-20 16:35:53 +01:00
|
|
|
|
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);
|
2022-11-20 16:35:53 +01:00
|
|
|
final req = http.Request(method, url);
|
2023-02-23 15:49:17 +01:00
|
|
|
if (auth != null) {
|
2022-11-20 16:35:53 +01:00
|
|
|
req.headers.addAll({
|
2023-02-23 15:49:17 +01:00
|
|
|
"authorization": auth!.toHeaderValue(),
|
2021-04-10 06:28:12 +02:00
|
|
|
});
|
2022-11-20 16:35:53 +01: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 =
|
2024-07-31 16:53:50 +02:00
|
|
|
await http.Response.fromStream(await getHttpClient().send(req));
|
2023-02-23 15:49:17 +01:00
|
|
|
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,
|
|
|
|
}) {
|
2023-02-23 15:49:17 +01:00
|
|
|
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 {
|
2023-02-23 15:49:17 +01:00
|
|
|
return Uri.https(baseUrl.authority, path, queryParameters);
|
2021-05-06 07:26:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-23 15:49:17 +01: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-11-20 16:35:53 +01:00
|
|
|
|
2022-07-08 16:52:18 +02:00
|
|
|
ApiOcsFacerecognition facerecognition() => ApiOcsFacerecognition(this);
|
2022-11-20 16:35:53 +01:00
|
|
|
|
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
|
|
|
}
|