2018-04-21 20:50:46 +08:00
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import 'token.dart';
|
2019-01-22 19:41:12 +08:00
|
|
|
import 'models/user.dart';
|
2018-04-21 20:50:46 +08:00
|
|
|
|
|
|
|
final prefix = 'https://api.github.com';
|
2019-01-23 19:52:51 +08:00
|
|
|
final endpoint = '/graphql';
|
2018-04-21 20:50:46 +08:00
|
|
|
|
2019-01-22 23:39:44 +08:00
|
|
|
Future<dynamic> getWithCredentials(String url) async {
|
2018-04-21 20:50:46 +08:00
|
|
|
final res = await http.get(
|
2019-01-22 23:39:44 +08:00
|
|
|
prefix + url,
|
2019-01-22 19:41:12 +08:00
|
|
|
headers: {HttpHeaders.authorizationHeader: 'token $token'},
|
2018-04-21 20:50:46 +08:00
|
|
|
);
|
2019-01-22 23:39:44 +08:00
|
|
|
final data = json.decode(res.body);
|
|
|
|
return data;
|
|
|
|
}
|
2019-01-22 19:41:12 +08:00
|
|
|
|
2019-01-23 19:52:51 +08:00
|
|
|
Future<dynamic> postWithCredentials(String url, String body) async {
|
|
|
|
final res = await http.post(
|
|
|
|
prefix + url,
|
|
|
|
headers: {HttpHeaders.authorizationHeader: 'token $token'},
|
|
|
|
body: body,
|
|
|
|
);
|
|
|
|
final data = json.decode(res.body);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<dynamic> query(String query) async {
|
|
|
|
final data =
|
|
|
|
await postWithCredentials('/graphql', json.encode({'query': query}));
|
2019-01-23 22:55:16 +08:00
|
|
|
if (data['errors'] != null) {
|
|
|
|
throw new Exception(data['errors'].toString());
|
2019-01-23 19:52:51 +08:00
|
|
|
}
|
|
|
|
print(data);
|
|
|
|
return data['data'];
|
|
|
|
}
|
|
|
|
|
2018-04-22 01:07:22 +08:00
|
|
|
Future<User> fetchUser(String login) async {
|
2019-01-22 23:39:44 +08:00
|
|
|
Map<String, dynamic> data = await getWithCredentials('/users/$login');
|
2018-07-05 22:08:19 +08:00
|
|
|
return User.fromJson(data);
|
2018-04-22 01:07:22 +08:00
|
|
|
}
|