2019-01-26 15:10:18 +01:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'package:github/server.dart';
|
|
|
|
export 'package:github/server.dart';
|
|
|
|
import '../token.dart';
|
|
|
|
|
|
|
|
var ghClient = createGitHubClient(auth: Authentication.withToken(token));
|
|
|
|
|
|
|
|
final prefix = 'https://api.github.com';
|
|
|
|
final endpoint = '/graphql';
|
|
|
|
|
2019-02-04 14:38:29 +01:00
|
|
|
Future<dynamic> getWithCredentials(String url, {String contentType}) async {
|
|
|
|
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
|
|
|
|
if (contentType != null) {
|
|
|
|
// https://developer.github.com/v3/repos/contents/#custom-media-types
|
|
|
|
headers[HttpHeaders.contentTypeHeader] = contentType;
|
|
|
|
}
|
2019-01-26 15:10:18 +01:00
|
|
|
final res = await http.get(
|
|
|
|
prefix + url,
|
2019-02-04 14:38:29 +01:00
|
|
|
headers: headers,
|
2019-01-26 15:10:18 +01:00
|
|
|
);
|
|
|
|
final data = json.decode(res.body);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-02-04 14:38:29 +01:00
|
|
|
Future<dynamic> postWithCredentials(String url, String body,
|
|
|
|
{String contentType}) async {
|
|
|
|
var headers = {HttpHeaders.authorizationHeader: 'token $token'};
|
|
|
|
if (contentType != null) {
|
|
|
|
headers[HttpHeaders.contentTypeHeader] = contentType;
|
|
|
|
}
|
|
|
|
final res = await http.post(prefix + url, headers: headers, body: body);
|
2019-01-26 15:10:18 +01:00
|
|
|
final data = json.decode(res.body);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> query(String query) async {
|
2019-01-27 17:37:44 +01:00
|
|
|
final res =
|
2019-01-26 15:10:18 +01:00
|
|
|
await postWithCredentials('/graphql', json.encode({'query': query}));
|
2019-01-27 17:37:44 +01:00
|
|
|
if (res['errors'] != null) {
|
|
|
|
throw new Exception(res['errors'].toString());
|
2019-01-26 15:10:18 +01:00
|
|
|
}
|
2019-01-28 14:32:01 +01:00
|
|
|
// print(res);
|
2019-01-27 17:37:44 +01:00
|
|
|
return res['data'];
|
2019-01-26 15:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<List<Event>> fetchEvents(int page) async {
|
|
|
|
List data = await getWithCredentials(
|
|
|
|
'/users/pd4d10/received_events/public?page=$page',
|
|
|
|
);
|
|
|
|
return data.map<Event>((item) => Event.fromJSON(item)).toList();
|
|
|
|
}
|