Skip to content

Commit f62d5db

Browse files
committed
Created a new required Torrents API.
1 parent 667971d commit f62d5db

File tree

2 files changed

+222
-6
lines changed

2 files changed

+222
-6
lines changed

lib/Api/client_api.dart

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class ClientApi {
126126
try {
127127
String url =
128128
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
129-
ApiEndpoints.getClientSettingsUrl;
129+
ApiEndpoints.checkClientConeection;
130130
print('---CHECK CLIENT ONLINE---');
131131
print(url);
132132
Response response;
@@ -141,10 +141,11 @@ class ClientApi {
141141
url,
142142
);
143143
if (response.statusCode == 200) {
144-
return true;
145-
} else {
146-
return false;
144+
if (response.data['isConnected'] == true) {
145+
return true;
146+
}
147147
}
148+
return false;
148149
} catch (error) {
149150
print('--ERROR IN CHECK CLIENT ONLINE--');
150151
print(error.toString());

lib/Api/torrent_api.dart

Lines changed: 217 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class TorrentApi {
368368
}
369369

370370
static Future<void> setTags(
371-
{required List<String> tagLits,
371+
{required List<String> tagList,
372372
required String hashes,
373373
required BuildContext context}) async {
374374
try {
@@ -387,7 +387,7 @@ class TorrentApi {
387387
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
388388
Map<String, dynamic> mp = Map();
389389
mp['hashes'] = [hashes];
390-
mp['tags'] = tagLits;
390+
mp['tags'] = tagList;
391391
String rawBody = json.encode(mp);
392392
response = await dio.patch(
393393
url,
@@ -401,4 +401,219 @@ class TorrentApi {
401401
print(error);
402402
}
403403
}
404+
405+
static Future<void> setTrackers({
406+
required List<String> trackersList,
407+
required String hashes,
408+
required BuildContext context,
409+
}) async {
410+
try {
411+
String url =
412+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
413+
ApiEndpoints.setTrackers;
414+
print('---SET TRACKERS---');
415+
print(url);
416+
Response response;
417+
Dio dio = new Dio();
418+
//Headers
419+
dio.options.headers['Accept'] = "application/json";
420+
dio.options.headers['Content-Type'] = "application/json";
421+
dio.options.headers['Connection'] = "keep-alive";
422+
dio.options.headers['Cookie'] =
423+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
424+
Map<String, dynamic> mp = Map();
425+
mp['trackers'] = trackersList;
426+
mp['hashes'] = [hashes];
427+
String rawBody = json.encode(mp);
428+
response = await dio.patch(
429+
url,
430+
data: rawBody,
431+
);
432+
if (response.statusCode == 200) {
433+
print('--TRACKERS ADDED--');
434+
}
435+
} catch (error) {
436+
print('--ERROR IN TRACKERS ADD--');
437+
print(error);
438+
}
439+
}
440+
441+
static Future<void> updateInitialSeeding({
442+
required String hash,
443+
required bool updatedValue,
444+
required BuildContext context,
445+
}) async {
446+
try {
447+
String url =
448+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
449+
ApiEndpoints.updateInitialSeeding;
450+
print('---UPDATE INITIAL SEEDING---');
451+
print(url);
452+
Response response;
453+
Dio dio = new Dio();
454+
//Headers
455+
dio.options.headers['Accept'] = "application/json";
456+
dio.options.headers['Content-Type'] = "application/json";
457+
dio.options.headers['Connection'] = "keep-alive";
458+
dio.options.headers['Cookie'] =
459+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
460+
Map<String, dynamic> mp = Map();
461+
mp['hashes'] = [hash];
462+
mp['isInitialSeeding'] = updatedValue;
463+
String rawBody = json.encode(mp);
464+
response = await dio.patch(
465+
url,
466+
data: rawBody,
467+
);
468+
if (response.statusCode == 200) {
469+
print('--INITIAL SEEDING UPDATED--');
470+
}
471+
} catch (error) {
472+
print('--ERROR IN INITIAL SEEDING UPDATE--');
473+
print(error);
474+
}
475+
}
476+
477+
static Future<void> updateSequentialMode({
478+
required String hash,
479+
required bool updatedValue,
480+
required BuildContext context,
481+
}) async {
482+
try {
483+
String url =
484+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
485+
ApiEndpoints.updateSequential;
486+
print('---UPDATE SEQUENTIAL MODE---');
487+
print(url);
488+
Response response;
489+
Dio dio = new Dio();
490+
//Headers
491+
dio.options.headers['Accept'] = "application/json";
492+
dio.options.headers['Content-Type'] = "application/json";
493+
dio.options.headers['Connection'] = "keep-alive";
494+
dio.options.headers['Cookie'] =
495+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
496+
Map<String, dynamic> mp = Map();
497+
mp['hashes'] = [hash];
498+
mp['isSequential'] = updatedValue;
499+
String rawBody = json.encode(mp);
500+
response = await dio.patch(
501+
url,
502+
data: rawBody,
503+
);
504+
if (response.statusCode == 200) {
505+
print('--SEQUENTIAL MODE UPDATED--');
506+
}
507+
} catch (error) {
508+
print('--ERROR IN UPDATE SEQUENTIAL MODE--');
509+
print(error);
510+
}
511+
}
512+
513+
static Future<bool> reannounceTorrents({
514+
required List<String> hashes,
515+
required BuildContext context,
516+
}) async {
517+
try {
518+
String url =
519+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
520+
ApiEndpoints.reannouncesTorrents;
521+
print('--TORRENT REANNOUNCES--');
522+
print(url);
523+
Response response;
524+
Dio dio = new Dio();
525+
//Headers
526+
dio.options.headers['Accept'] = "application/json";
527+
dio.options.headers['Content-Type'] = "application/json";
528+
dio.options.headers['Connection'] = "keep-alive";
529+
dio.options.headers['Cookie'] =
530+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
531+
Map<String, dynamic> mp = Map();
532+
mp['hashes'] = hashes;
533+
String rawBody = json.encode(mp);
534+
response = await dio.post(
535+
url,
536+
data: rawBody,
537+
);
538+
if (response.statusCode == 200) {
539+
print('--TORRENT REANNOUNCED--');
540+
return true;
541+
}
542+
return false;
543+
} catch (error) {
544+
print('--ERROR IN TORRENT REANNOUNCES--');
545+
print(error);
546+
return false;
547+
}
548+
}
549+
550+
static Future<void> updatePriority({
551+
required List<String> hashes,
552+
required BuildContext context,
553+
required int priority,
554+
}) async {
555+
try {
556+
String url =
557+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
558+
ApiEndpoints.updatePriority;
559+
print('--UPDATE PRIORITY--');
560+
print(url);
561+
Response response;
562+
Dio dio = new Dio();
563+
//Headers
564+
dio.options.headers['Accept'] = "application/json";
565+
dio.options.headers['Content-Type'] = "application/json";
566+
dio.options.headers['Connection'] = "keep-alive";
567+
dio.options.headers['Cookie'] =
568+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
569+
Map<String, dynamic> mp = Map();
570+
mp['hashes'] = hashes;
571+
mp['priority'] = priority;
572+
String rawBody = json.encode(mp);
573+
response = await dio.patch(
574+
url,
575+
data: rawBody,
576+
);
577+
if (response.statusCode == 200) {
578+
print('--UPDATED PRIORITY--');
579+
}
580+
} catch (error) {
581+
print('--ERROR IN UPDATED PRIORITY--');
582+
print(error);
583+
}
584+
}
585+
586+
static Future<List<String>> getTrackersList(
587+
{required BuildContext context, required String hash}) async {
588+
try {
589+
print('--GET TRACKERS LIST--');
590+
Response response;
591+
Dio dio = new Dio();
592+
String url =
593+
BlocProvider.of<ApiBloc>(context, listen: false).state.baseUrl +
594+
ApiEndpoints.getTrackersList +
595+
hash +
596+
'/trackers';
597+
dio.options.headers['Accept'] = "application/json";
598+
dio.options.headers['Content-Type'] = "application/json";
599+
dio.options.headers['Connection'] = "keep-alive";
600+
dio.options.headers['Cookie'] =
601+
BlocProvider.of<UserDetailBloc>(context, listen: false).token;
602+
response = await dio.get(url);
603+
List<dynamic> responseData = [];
604+
List<String> trackers = <String>[];
605+
if (response.statusCode == 200) {
606+
responseData = response.data;
607+
trackers = responseData
608+
.where((item) => item['type'] == 1 || item['type'] == 2)
609+
.map<String>((item) => item['url'])
610+
.toList();
611+
}
612+
return trackers;
613+
} catch (error) {
614+
print('--ERROR IN GET TRACKERS LIST--');
615+
print(error);
616+
return [];
617+
}
618+
}
404619
}

0 commit comments

Comments
 (0)