From 8c655b0fd50c4618fa350d8faa017335c6fedb78 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 29 Aug 2023 15:29:00 +0800 Subject: [PATCH] Update --- lib/api/client.dart | 30 ++++++++++++++++++--- lib/api/client.g.dart | 63 +++++++++++++++++++++++++++++++++++++++++++ lib/api/file.dart | 52 +++++++++++++++++++++++++++++++++++ lib/api/file.g.dart | 39 +++++++++++++++++++++++++++ 4 files changed, 181 insertions(+), 3 deletions(-) create mode 100644 lib/api/file.dart create mode 100644 lib/api/file.g.dart diff --git a/lib/api/client.dart b/lib/api/client.dart index 7799d4c..5e3d9bd 100644 --- a/lib/api/client.dart +++ b/lib/api/client.dart @@ -1,6 +1,7 @@ import 'dart:convert'; import 'package:cryptography/cryptography.dart'; import 'package:dio/dio.dart'; +import 'package:eh_downloader_flutter/api/file.dart'; import 'package:retrofit/retrofit.dart'; import 'api_result.dart'; import 'status.dart'; @@ -41,26 +42,41 @@ abstract class _EHApi { Future> getStatus(); @PUT('/token') + // ignore: unused_element Future> _createToken( {@Query("username") required String username, @Query("password") required String password, @Query("t") required int t, + // ignore: unused_element @Query("set_cookie") bool? setCookie, + // ignore: unused_element @Query("http_only") bool? httpOnly, + // ignore: unused_element @Query("secure") bool? secure}); @DELETE('/token') Future> deleteToken({@Query("token") String? token}); @GET('/token') - Future> getToken({@Query("token") String? token}); + Future> getToken( + {@Query("token") String? token}); @GET('/file/{id}') Future getFile(@Path("id") int id); + @GET('/file/{id}') + // ignore: unused_element + Future> _getFileData( + @Path("id") int id, @Query("data") bool data); @GET('/file/random') - Future getRandomFile({@Query("is_nsfw") bool? isNsfw, @Query("is_ad") bool? isAd, @Query("thumb") bool? thumb}); + Future getRandomFile( + {@Query("is_nsfw") bool? isNsfw, + @Query("is_ad") bool? isAd, + @Query("thumb") bool? thumb}); + @GET('/files/{token}') + // ignore: unused_element + Future> _getFiles(@Path("token") String token); } class EHApi extends __EHApi { - EHApi(Dio dio, {required String baseUrl}): super(dio, baseUrl: baseUrl); + EHApi(Dio dio, {required String baseUrl}) : super(dio, baseUrl: baseUrl); Future> createToken( {required String username, required String password, @@ -81,4 +97,12 @@ class EHApi extends __EHApi { httpOnly: httpOnly, secure: secure); } + + Future> getFileData(int id) { + return _getFileData(id, true); + } + + Future> getFiles(List tokens) { + return _getFiles(tokens.join(",")); + } } diff --git a/lib/api/client.g.dart b/lib/api/client.g.dart index 514ca3b..3d8db6c 100644 --- a/lib/api/client.g.dart +++ b/lib/api/client.g.dart @@ -261,6 +261,39 @@ class __EHApi implements _EHApi { return httpResponse; } + @override + Future> _getFileData( + int id, + bool data, + ) async { + const _extra = {}; + final queryParameters = {r'data': data}; + final _headers = {}; + final Map? _data = null; + final _result = await _dio.fetch>( + _setStreamType>(Options( + method: 'GET', + headers: _headers, + extra: _extra, + ) + .compose( + _dio.options, + '/file/${id}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + )))); + final value = ApiResult.fromJson( + _result.data!, + (json) => EhFileExtend.fromJson(json as Map), + ); + return value; + } + @override Future> getRandomFile({ bool? isNsfw, @@ -298,6 +331,36 @@ class __EHApi implements _EHApi { return httpResponse; } + @override + Future> _getFiles(String token) async { + const _extra = {}; + final queryParameters = {}; + final _headers = {}; + final Map? _data = null; + final _result = await _dio + .fetch>(_setStreamType>(Options( + method: 'GET', + headers: _headers, + extra: _extra, + ) + .compose( + _dio.options, + '/files/${token}', + queryParameters: queryParameters, + data: _data, + ) + .copyWith( + baseUrl: _combineBaseUrls( + _dio.options.baseUrl, + baseUrl, + )))); + final value = ApiResult.fromJson( + _result.data!, + (json) => EhFiles.fromJson(json as Map), + ); + return value; + } + RequestOptions _setStreamType(RequestOptions requestOptions) { if (T != dynamic && !(requestOptions.responseType == ResponseType.bytes || diff --git a/lib/api/file.dart b/lib/api/file.dart new file mode 100644 index 0000000..6404db4 --- /dev/null +++ b/lib/api/file.dart @@ -0,0 +1,52 @@ +import 'package:json_annotation/json_annotation.dart'; + +part 'file.g.dart'; + +@JsonSerializable() +class EhFileBasic { + const EhFileBasic({ + required this.id, + required this.width, + required this.height, + required this.isOriginal, + }); + final int id; + final int width; + final int height; + @JsonKey(name: 'is_original') + final bool isOriginal; + factory EhFileBasic.fromJson(Map json) => + _$EhFileBasicFromJson(json); + Map toJson() => _$EhFileBasicToJson(this); +} + +@JsonSerializable() +class EhFileExtend { + const EhFileExtend({ + required this.id, + required this.width, + required this.height, + required this.isOriginal, + required this.token, + }); + final int id; + final int width; + final int height; + @JsonKey(name: 'is_original') + final bool isOriginal; + final String token; + factory EhFileExtend.fromJson(Map json) => + _$EhFileExtendFromJson(json); + Map toJson() => _$EhFileExtendToJson(this); +} + +class EhFiles { + const EhFiles({required this.files}); + final Map files; + factory EhFiles.fromJson(Map json) => EhFiles( + files: (json).map( + (k, e) => + MapEntry(k, EhFileBasic.fromJson(e as Map)), + ), + ); +} diff --git a/lib/api/file.g.dart b/lib/api/file.g.dart new file mode 100644 index 0000000..2bcabf5 --- /dev/null +++ b/lib/api/file.g.dart @@ -0,0 +1,39 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'file.dart'; + +// ************************************************************************** +// JsonSerializableGenerator +// ************************************************************************** + +EhFileBasic _$EhFileBasicFromJson(Map json) => EhFileBasic( + id: json['id'] as int, + width: json['width'] as int, + height: json['height'] as int, + isOriginal: json['is_original'] as bool, + ); + +Map _$EhFileBasicToJson(EhFileBasic instance) => + { + 'id': instance.id, + 'width': instance.width, + 'height': instance.height, + 'is_original': instance.isOriginal, + }; + +EhFileExtend _$EhFileExtendFromJson(Map json) => EhFileExtend( + id: json['id'] as int, + width: json['width'] as int, + height: json['height'] as int, + isOriginal: json['is_original'] as bool, + token: json['token'] as String, + ); + +Map _$EhFileExtendToJson(EhFileExtend instance) => + { + 'id': instance.id, + 'width': instance.width, + 'height': instance.height, + 'is_original': instance.isOriginal, + 'token': instance.token, + };