120 lines
No EOL
3.7 KiB
Dart
120 lines
No EOL
3.7 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:gitea_client/model/ApiAccess.dart';
|
|
import 'package:gitea_client/model/issues.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';
|
|
import '../model/SearchResult.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<Issue>> getRepoIssues({required String owner,required String repo,required String state,int page = 1, int limit = 10}) async{
|
|
if(state != "all" && state != "closed" && state != "open") {
|
|
throw Exception("Wrong state provided: $state");
|
|
}
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/repos/$owner/$repo/issues", {
|
|
"token": apiAccess.token,
|
|
"state": state,
|
|
"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 Issue.fromJson(json);
|
|
}).toList();
|
|
|
|
result.sort((a,b) => -1*a.updatedAt!.compareTo(b.updatedAt!));
|
|
return result;
|
|
}
|
|
throw Exception('error fetching posts: ${response.statusCode}');
|
|
}
|
|
|
|
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 repos');
|
|
}
|
|
|
|
Future<SearchResult> searchRepo({required String query,int page = 1, int limit = 10}) async{
|
|
var response = await http.get(
|
|
Uri.https(apiAccess.instance, "api/v1/repos/search", {
|
|
"token": apiAccess.token,
|
|
"q": query,
|
|
"page" : page.toString(),
|
|
"limit" : limit.toString(),
|
|
}),
|
|
);
|
|
if (response.statusCode == 200) {
|
|
|
|
final body = json.decode(response.body);
|
|
var result = SearchResult.fromJson(body);
|
|
|
|
return result;
|
|
}
|
|
throw Exception('error fetching posts');
|
|
}
|
|
} |