43 lines
No EOL
1.2 KiB
Dart
43 lines
No EOL
1.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;
|
|
|
|
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<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;
|
|
return body.map((dynamic json) {
|
|
return Repository.fromJson(json);
|
|
}).toList();
|
|
}
|
|
throw Exception('error fetching posts');
|
|
}
|
|
} |