GitHub API: Refactor it into a class

This commit is contained in:
Vishesh Handa
2019-01-24 13:58:17 +01:00
parent fbdb4d61ed
commit d87085ac29
2 changed files with 145 additions and 128 deletions

View File

@ -7,60 +7,156 @@ import 'package:flutter/services.dart';
import 'package:url_launcher/url_launcher.dart';
const _clientID = "aa3072cbfb02b1db14ed";
const _clientSecret = "010d303ea99f82330f2b228977cef9ddbf7af2cd";
const _platform = const MethodChannel('gitjournal.io/git');
class GitHub {
static const _clientID = "aa3072cbfb02b1db14ed";
static const _clientSecret = "010d303ea99f82330f2b228977cef9ddbf7af2cd";
Future<String> getAccessCode(String authCode) async {
var url =
"https://github.com/login/oauth/access_token?client_id=${_clientID}&client_secret=${_clientSecret}&code=${authCode}";
var _platform = const MethodChannel('gitjournal.io/git');
var _accessCode = "";
var response = await http.post(url);
if (response.statusCode != 200) {
print("Github getAccessCode: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GithubResponse: " + response.body);
void init(Function callback) {
Future _handleMessages(MethodCall call) async {
if (call.method != "onURL") {
print("GitHub Unknown Call: " + call.method);
return;
}
var map = Uri.splitQueryString(response.body);
return map["access_token"];
}
print("GitHub: Called onUrl with " + call.arguments.toString());
void initGitHub(Function callback) {
Future _handleMessages(MethodCall call) async {
if (call.method != "onURL") {
print("GitHub Unknown Call: " + call.method);
return;
}
var url = call.arguments["URL"];
var uri = Uri.parse(url);
var authCode = uri.queryParameters['code'];
if (authCode == null) {
print("GitHub: Missing auth code. Now what?");
callback();
}
print("GitHub: Called onUrl with " + call.arguments.toString());
var url = call.arguments["URL"];
var uri = Uri.parse(url);
var authCode = uri.queryParameters['code'];
if (authCode == null) {
print("GitHub: Missing auth code. Now what?");
this._accessCode = await _getAccessCode(authCode);
callback();
}
var acesssCode = await getAccessCode(authCode);
callback(acesssCode);
_platform.setMethodCallHandler(_handleMessages);
print("GitHub: Installed Handler");
}
_platform.setMethodCallHandler(_handleMessages);
print("GitHub: Installed Handler");
}
Future<String> _getAccessCode(String authCode) async {
var url =
"https://github.com/login/oauth/access_token?client_id=${_clientID}&client_secret=${_clientSecret}&code=${authCode}";
Future launchOAuthScreen() async {
// FIXME: Add some 'state' over here!
var response = await http.post(url);
if (response.statusCode != 200) {
print("Github getAccessCode: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GithubResponse: " + response.body);
var url = "https://github.com/login/oauth/authorize?client_id=" +
_clientID +
"&scope=repo";
return launch(url);
var map = Uri.splitQueryString(response.body);
return map["access_token"];
}
Future launchOAuthScreen() async {
// FIXME: Add some 'state' over here!
var url = "https://github.com/login/oauth/authorize?client_id=" +
_clientID +
"&scope=repo";
return launch(url);
}
Future<List<Repo>> listRepos() async {
if (_accessCode.isEmpty) {
throw "GitHub Access Code Missing";
}
var url =
"https://api.github.com/user/repos?page=1&per_page=100&access_token=${_accessCode}";
var response = await http.get(url);
if (response.statusCode != 200) {
print("Github listRepos: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
List<dynamic> list = jsonDecode(response.body);
List<Repo> repos = new List<Repo>();
list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d);
var repo = Repo.fromJson(map);
repos.add(repo);
});
// FIXME: Sort these based on some criteria
return repos;
}
// FIXME: Proper error when the repo exists!
Future<Repo> createRepo(String name) async {
if (_accessCode.isEmpty) {
throw "GitHub Access Code Missing";
}
var url = "https://api.github.com/user/repos?access_token=${_accessCode}";
Map<String, dynamic> data = {
'name': name,
'private': true,
};
var headers = {
HttpHeaders.contentTypeHeader: "application/json",
};
var response =
await http.post(url, headers: headers, body: json.encode(data));
if (response.statusCode != 201) {
print("Github createRepo: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GitHub createRepo: " + response.body);
var map = json.decode(response.body);
return Repo.fromJson(map);
}
Future addDeployKey(String sshPublicKey, String repo) async {
if (_accessCode.isEmpty) {
throw "GitHub Access Code Missing";
}
var url =
"https://api.github.com/repos/$repo/keys?access_token=${_accessCode}";
Map<String, dynamic> data = {
'title': "GitJournal",
'key': sshPublicKey,
'read_only': false,
};
var headers = {
HttpHeaders.contentTypeHeader: "application/json",
};
var response =
await http.post(url, headers: headers, body: json.encode(data));
if (response.statusCode != 201) {
print("Github addDeployKey: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GitHub addDeployKey: " + response.body);
return json.decode(response.body);
}
}
class Repo {
@ -76,81 +172,3 @@ class Repo {
return 'Repo{fulleName: $fullName}';
}
}
Future<List<Repo>> listRepos(String accessCode) async {
var url =
"https://api.github.com/user/repos?page=1&per_page=100&access_token=${accessCode}";
var response = await http.get(url);
if (response.statusCode != 200) {
print("Github listRepos: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
List<dynamic> list = jsonDecode(response.body);
List<Repo> repos = new List<Repo>();
list.forEach((dynamic d) {
var map = Map<String, dynamic>.from(d);
var repo = Repo.fromJson(map);
repos.add(repo);
});
// FIXME: Sort these based on some criteria
return repos;
}
Future createRepo(String accessCode, String name) async {
var url = "https://api.github.com/user/repos?access_token=${accessCode}";
Map<String, dynamic> data = {
'name': name,
'private': true,
};
var headers = {
HttpHeaders.contentTypeHeader: "application/json",
};
var response =
await http.post(url, headers: headers, body: json.encode(data));
if (response.statusCode != 201) {
print("Github createRepo: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GitHub createRepo: " + response.body);
return json.decode(response.body);
}
Future addDeployKey(String accessCode, String sshPublicKey, String repo) async {
var url =
"https://api.github.com/repos/$repo/keys?access_token=${accessCode}";
Map<String, dynamic> data = {
'title': "GitJournal",
'key': sshPublicKey,
'read_only': false,
};
var headers = {
HttpHeaders.contentTypeHeader: "application/json",
};
var response =
await http.post(url, headers: headers, body: json.encode(data));
if (response.statusCode != 201) {
print("Github addDeployKey: Invalid response " +
response.statusCode.toString() +
": " +
response.body);
return null;
}
print("GitHub addDeployKey: " + response.body);
return json.decode(response.body);
}