All checks were successful
ci/woodpecker/push/flutterBuild Pipeline was successful
95 lines
No EOL
2.8 KiB
Dart
95 lines
No EOL
2.8 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:gitea_client/model/ApiAccess.dart';
|
|
import 'package:gitea_client/model/repository.dart';
|
|
import 'package:gitea_client/model/user.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../model/File.dart';
|
|
|
|
User _parseAuthenticatedUserResponse(String message){
|
|
return User.fromJson(jsonDecode(message));
|
|
}
|
|
|
|
class GiteaService {
|
|
final ApiAccess apiAccess;
|
|
|
|
const GiteaService({ required this.apiAccess});
|
|
|
|
Future<User> getAuthenticatedUser() async {
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/user", {
|
|
"token": apiAccess.token,
|
|
}),
|
|
);
|
|
return compute(_parseAuthenticatedUserResponse, response.body);
|
|
}
|
|
|
|
Future<RepoFile> getFile(String owner,String repo,String path) async{
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/repos/$owner/$repo/contents/$path", {
|
|
"token": apiAccess.token,
|
|
}),
|
|
);
|
|
|
|
return RepoFile.fromJson(jsonDecode(response.body));
|
|
}
|
|
|
|
Future<List<RepoFile>> getFolder(String owner,String repo,String path) async{
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/repos/$owner/$repo/contents/$path", {
|
|
"token": apiAccess.token,
|
|
}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final body = json.decode(response.body) as List;
|
|
var result = body.map((dynamic json) {
|
|
return RepoFile.fromJson(json);
|
|
}).toList();
|
|
result.sort((a,b) => a.type.compareTo(b.type));
|
|
return result;
|
|
}
|
|
throw Exception('error fetching files');
|
|
}
|
|
//
|
|
Future<List<Repository>> getUserRepositories([int page = 1, int limit = 10]) async{
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/user/repos", {
|
|
"token": apiAccess.token,
|
|
"page" : page.toString(),
|
|
"limit" : limit.toString(),
|
|
}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final body = json.decode(response.body) as List;
|
|
var result = body.map((dynamic json) {
|
|
return Repository.fromJson(json);
|
|
}).toList();
|
|
|
|
result.sort((a,b) => -1*a.updatedAt!.compareTo(b.updatedAt!));
|
|
return result;
|
|
}
|
|
throw Exception('error fetching posts');
|
|
}
|
|
|
|
Future<List<Repository>> searchRepo({required String query,int page = 1, int limit = 10}) async{
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/user/repos/search", {
|
|
"token": apiAccess.token,
|
|
"q": query,
|
|
"page" : page.toString(),
|
|
"limit" : limit.toString(),
|
|
}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
final body = json.decode(response.body) as List;
|
|
var result = body.map((dynamic json) {
|
|
return Repository.fromJson(json);
|
|
}).toList();
|
|
|
|
return result;
|
|
}
|
|
throw Exception('error fetching posts');
|
|
}
|
|
} |