carga inicial
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
class YouTubeSearchResponse {
|
||||
String kind;
|
||||
String etag;
|
||||
String nextPageToken;
|
||||
String regionCode;
|
||||
PageInfo pageInfo;
|
||||
List<YouTubeItems> items;
|
||||
int errorCode;
|
||||
String errorMessage;
|
||||
|
||||
YouTubeSearchResponse({this.kind, this.etag, this.nextPageToken, this.regionCode, this.pageInfo, this.items,this.errorCode = 0, this.errorMessage = ''});
|
||||
|
||||
YouTubeSearchResponse.fromJson(Map<String, dynamic> json) {
|
||||
kind = json['kind'] ?? '';
|
||||
etag = json['etag'] ?? '';
|
||||
nextPageToken = json['nextPageToken'] ?? '';
|
||||
regionCode = json['regionCode'] ?? '';
|
||||
pageInfo = json['pageInfo'] != null ? new PageInfo.fromJson(json['pageInfo']) : null;
|
||||
if (json['items'] != null) {
|
||||
items = new List<YouTubeItems>();
|
||||
json['items'].forEach((v) { items.add(new YouTubeItems.fromJson(v)); });
|
||||
}
|
||||
errorCode = 0;
|
||||
errorMessage = '';
|
||||
}
|
||||
}
|
||||
|
||||
class PageInfo {
|
||||
int totalResults;
|
||||
int resultsPerPage;
|
||||
|
||||
PageInfo({this.totalResults, this.resultsPerPage});
|
||||
|
||||
PageInfo.fromJson(Map<String, dynamic> json) {
|
||||
totalResults = json['totalResults'];
|
||||
resultsPerPage = json['resultsPerPage'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['totalResults'] = this.totalResults;
|
||||
data['resultsPerPage'] = this.resultsPerPage;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class YouTubeItems {
|
||||
String kind;
|
||||
String etag;
|
||||
Id id;
|
||||
Snippet snippet;
|
||||
|
||||
YouTubeItems({this.kind, this.etag, this.id, this.snippet});
|
||||
|
||||
YouTubeItems.fromJson(Map<String, dynamic> json) {
|
||||
kind = json['kind'];
|
||||
etag = json['etag'];
|
||||
id = json['id'] != null ? new Id.fromJson(json['id']) : null;
|
||||
snippet = json['snippet'] != null ? new Snippet.fromJson(json['snippet']) : null;
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['kind'] = this.kind;
|
||||
data['etag'] = this.etag;
|
||||
if (this.id != null) {
|
||||
data['id'] = this.id.toJson();
|
||||
}
|
||||
if (this.snippet != null) {
|
||||
data['snippet'] = this.snippet.toJson();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Id {
|
||||
String kind;
|
||||
String videoId;
|
||||
|
||||
Id({this.kind, this.videoId});
|
||||
|
||||
Id.fromJson(Map<String, dynamic> json) {
|
||||
kind = json['kind'];
|
||||
videoId = json['videoId'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['kind'] = this.kind;
|
||||
data['videoId'] = this.videoId;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Snippet {
|
||||
String publishedAt;
|
||||
String channelId;
|
||||
String title;
|
||||
String description;
|
||||
Thumbnails thumbnails;
|
||||
String channelTitle;
|
||||
String liveBroadcastContent;
|
||||
String publishTime;
|
||||
|
||||
Snippet({this.publishedAt, this.channelId, this.title, this.description, this.thumbnails, this.channelTitle, this.liveBroadcastContent, this.publishTime});
|
||||
|
||||
Snippet.fromJson(Map<String, dynamic> json) {
|
||||
publishedAt = json['publishedAt'];
|
||||
channelId = json['channelId'];
|
||||
title = json['title'];
|
||||
description = json['description'];
|
||||
thumbnails = json['thumbnails'] != null ? new Thumbnails.fromJson(json['thumbnails']) : null;
|
||||
channelTitle = json['channelTitle'];
|
||||
liveBroadcastContent = json['liveBroadcastContent'];
|
||||
publishTime = json['publishTime'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['publishedAt'] = this.publishedAt;
|
||||
data['channelId'] = this.channelId;
|
||||
data['title'] = this.title;
|
||||
data['description'] = this.description;
|
||||
if (this.thumbnails != null) {
|
||||
data['thumbnails'] = this.thumbnails.toJson();
|
||||
}
|
||||
data['channelTitle'] = this.channelTitle;
|
||||
data['liveBroadcastContent'] = this.liveBroadcastContent;
|
||||
data['publishTime'] = this.publishTime;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class Thumbnails {
|
||||
ThumbDefault thumbDefault;
|
||||
ThumbDefault medium;
|
||||
ThumbDefault high;
|
||||
|
||||
Thumbnails({this.thumbDefault, this.medium, this.high});
|
||||
|
||||
Thumbnails.fromJson(Map<String, dynamic> json) {
|
||||
thumbDefault = json['default'] != null ? new ThumbDefault.fromJson(json['default']) : null;
|
||||
medium = json['medium'] != null ? new ThumbDefault.fromJson(json['medium']) : null;
|
||||
high = json['high'] != null ? new ThumbDefault.fromJson(json['high']) : null;
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
if (this.thumbDefault != null) {
|
||||
data['default'] = this.thumbDefault.toJson();
|
||||
}
|
||||
if (this.medium != null) {
|
||||
data['medium'] = this.medium.toJson();
|
||||
}
|
||||
if (this.high != null) {
|
||||
data['high'] = this.high.toJson();
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
class ThumbDefault {
|
||||
String url;
|
||||
int width;
|
||||
int height;
|
||||
|
||||
ThumbDefault({this.url, this.width, this.height});
|
||||
|
||||
ThumbDefault.fromJson(Map<String, dynamic> json) {
|
||||
url = json['url'];
|
||||
width = json['width'];
|
||||
height = json['height'];
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
final Map<String, dynamic> data = new Map<String, dynamic>();
|
||||
data['url'] = this.url;
|
||||
data['width'] = this.width;
|
||||
data['height'] = this.height;
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user