refactor(github): fetch raw markdown

This commit is contained in:
Rongjian Zhang 2020-01-31 20:57:01 +08:00
parent 14c6944481
commit 0a83206071
2 changed files with 12 additions and 15 deletions

View File

@ -287,14 +287,9 @@ class AuthModel with ChangeNotifier {
return data['data'];
}
Future<dynamic> getWithCredentials(String url, {String contentType}) async {
var headers = _headers;
if (contentType != null) {
// https://developer.github.com/v3/repos/contents/#custom-media-types
headers[HttpHeaders.contentTypeHeader] = contentType;
}
Future<dynamic> getWithCredentials(String url) async {
final res = await http
.get(_apiPrefix + url, headers: headers)
.get(_apiPrefix + url, headers: _headers)
.timeout(_timeoutDuration);
final data = json.decode(res.body);
if (res.statusCode >= 400) {
@ -303,6 +298,15 @@ class AuthModel with ChangeNotifier {
return data;
}
Future<String> getRaw(String url) async {
final res = await http.get(_apiPrefix + url, headers: {
..._headers,
// https://developer.github.com/v3/repos/contents/#custom-media-types
HttpHeaders.acceptHeader: 'application/vnd.github.v3.raw'
}).timeout(_timeoutDuration);
return res.body;
}
Future<void> patchWithCredentials(String url) async {
await http
.patch(_apiPrefix + url, headers: _headers)

View File

@ -1,4 +1,3 @@
import 'dart:convert';
import 'package:filesize/filesize.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
@ -46,12 +45,6 @@ class RepositoryScreen extends StatelessWidget {
return res.data.repository;
}
Future<String> _fetchReadme(BuildContext context) async {
final data = await Provider.of<AuthModel>(context)
.getWithCredentials('/repos/$owner/$name/readme');
return (data['content'] as String)?.dropLineBreak?.base64ToUtf8;
}
Widget _buildLanguages(BuildContext context, GhRepoRepository repo) {
final theme = Provider.of<ThemeModel>(context);
return Container(
@ -106,7 +99,7 @@ class RepositoryScreen extends StatelessWidget {
fetchData: () async {
final rs = await Future.wait([
_query(context),
_fetchReadme(context),
Provider.of<AuthModel>(context).getRaw('/repos/$owner/$name/readme'),
]);
return Tuple2(rs[0] as GhRepoRepository, rs[1] as String);
},