From 01b4e21fc787570d25001a5cb119b475ced1b0fb Mon Sep 17 00:00:00 2001 From: Jacopo Date: Sat, 2 Mar 2024 19:55:14 +0100 Subject: [PATCH] api --- lib/bloc/versions/app_cubit.dart | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/lib/bloc/versions/app_cubit.dart b/lib/bloc/versions/app_cubit.dart index 85b6eed..b72481f 100644 --- a/lib/bloc/versions/app_cubit.dart +++ b/lib/bloc/versions/app_cubit.dart @@ -1,6 +1,6 @@ import 'dart:convert'; +import 'dart:html'; -import 'package:chaleno/chaleno.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; import 'package:http/http.dart' as http; import 'package:statuspage/bloc/versions/app_state.dart'; @@ -38,11 +38,30 @@ class AppCubit extends Cubit { } Future addRepo(project) async { - var parser = await Chaleno() - .load('https://github.com/pagopa/${project['repository']}'); - List? results = parser?.getElementsByClassName( - 'css-truncate css-truncate-target text-bold mr-2'); - var version = results?.first.text ?? 'ERROR'; + // var parser = await Chaleno() + // .load('https://github.com/pagopa/${project['repository']}'); + // List? results = parser?.getElementsByClassName( + // 'css-truncate css-truncate-target text-bold mr-2'); + // var version = results?.first.text ?? 'ERROR'; + + final Storage storage = window.localStorage; + + http.Response response; + if (storage['gh_token'] != null) { + response = await http.get( + Uri.parse( + 'https://api.github.com/repos/pagopa/${project['repository']}/releases/latest'), + headers: { + 'X-GitHub-Api-Version': '2022-11-28', + 'Authorization': 'Bearer ${storage['gh_token']}', + 'Accept': 'application/vnd.github+json' + }, + ); // there are limitation 60 requests per hour + } else { + response = await http.get(Uri.parse( + 'https://api.github.com/repos/pagopa/${project['repository']}/releases/latest')); // there are limitation 60 requests per hour + } + var version = jsonDecode(response.body)['tag_name'] ?? 'No Release'; state.repoVersion.update(project['product'], (value) => version, ifAbsent: () => version);