Files
AppCardsStarter_Modelo/lib/videos/api/youtube_api.dart
T
2021-05-04 16:12:48 -03:00

63 lines
1.9 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../../funcoes_gerais.dart';
import 'youtube_classes.dart';
class YouTubeApi{
Future<YouTubeSearchResponse> getChannelVideos({String nextPageToken = ''}) async {
var retorno = new YouTubeSearchResponse(
errorCode: 0,
errorMessage: '',
etag: '',
items: [],
kind: '',
nextPageToken: '',
);
if (await FuncoesGerais.checkInternetConnection() == false) {
retorno.errorCode = 1;
retorno.errorMessage = "Sem conexão com a internet";
return retorno;
}
String url = "https://www.googleapis.com/youtube/v3/search?key=${FuncoesGerais.youtubeApiKey}&channelId=${FuncoesGerais.channelId}&part=snippet,id&order=date&maxResults=10";
if(nextPageToken.isNotEmpty) url += "&pageToken=$nextPageToken";
try{
var response = await http.get(url);
if (response != null) {
if (response.statusCode == 200) {
var data = jsonDecode(response.body);
retorno = YouTubeSearchResponse.fromJson(data);
} else {
var data = jsonDecode(response.body);
retorno.errorCode = 2;
retorno.errorMessage = "Erro inesperado, tente novamente mais tarde. #${response.statusCode}";
if(data != null && data['error'] != null){
if(data['error']['message'] != null && data['error']['message'].toString().isNotEmpty){
retorno.errorMessage = data['error']['message'].toString();
}
}
return retorno;
}
} else {
retorno.errorCode = 2;
retorno.errorMessage = "Erro inesperado, tente novamente mais tarde";
return retorno;
}
} catch (e) {
retorno.errorCode = 2;
retorno.errorMessage = "Falha interna no App, tente novamenre mais tarde.";
return retorno;
}
return retorno;
}
}