GiteaClient/lib/service/gitea_service.dart
Bazsalanszky 182588e7ae
Some checks failed
ci/woodpecker/push/flutterBuild Pipeline failed
Basic code page
2022-05-15 18:51:38 +02:00

75 lines
No EOL
2.2 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');
}
}