mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-15 07:56:11 +08:00
GitHub API: Refactor it into a class
This commit is contained in:
@ -7,11 +7,39 @@ 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 _platform = const MethodChannel('gitjournal.io/git');
|
||||
var _accessCode = "";
|
||||
|
||||
void init(Function callback) {
|
||||
Future _handleMessages(MethodCall call) async {
|
||||
if (call.method != "onURL") {
|
||||
print("GitHub Unknown Call: " + call.method);
|
||||
return;
|
||||
}
|
||||
|
||||
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?");
|
||||
callback();
|
||||
}
|
||||
|
||||
this._accessCode = await _getAccessCode(authCode);
|
||||
callback();
|
||||
}
|
||||
|
||||
_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}";
|
||||
|
||||
@ -29,31 +57,6 @@ Future<String> getAccessCode(String authCode) async {
|
||||
return map["access_token"];
|
||||
}
|
||||
|
||||
void initGitHub(Function callback) {
|
||||
Future _handleMessages(MethodCall call) async {
|
||||
if (call.method != "onURL") {
|
||||
print("GitHub Unknown Call: " + call.method);
|
||||
return;
|
||||
}
|
||||
|
||||
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?");
|
||||
callback();
|
||||
}
|
||||
|
||||
var acesssCode = await getAccessCode(authCode);
|
||||
callback(acesssCode);
|
||||
}
|
||||
|
||||
_platform.setMethodCallHandler(_handleMessages);
|
||||
print("GitHub: Installed Handler");
|
||||
}
|
||||
|
||||
Future launchOAuthScreen() async {
|
||||
// FIXME: Add some 'state' over here!
|
||||
|
||||
@ -63,23 +66,13 @@ Future launchOAuthScreen() async {
|
||||
return launch(url);
|
||||
}
|
||||
|
||||
class Repo {
|
||||
String fullName;
|
||||
|
||||
Repo({this.fullName});
|
||||
factory Repo.fromJson(Map<String, dynamic> parsedJson) {
|
||||
return new Repo(fullName: parsedJson['full_name']);
|
||||
Future<List<Repo>> listRepos() async {
|
||||
if (_accessCode.isEmpty) {
|
||||
throw "GitHub Access Code Missing";
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
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}";
|
||||
"https://api.github.com/user/repos?page=1&per_page=100&access_token=${_accessCode}";
|
||||
|
||||
var response = await http.get(url);
|
||||
if (response.statusCode != 200) {
|
||||
@ -102,8 +95,13 @@ Future<List<Repo>> listRepos(String accessCode) async {
|
||||
return repos;
|
||||
}
|
||||
|
||||
Future createRepo(String accessCode, String name) async {
|
||||
var url = "https://api.github.com/user/repos?access_token=${accessCode}";
|
||||
// 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,
|
||||
@ -124,12 +122,17 @@ Future createRepo(String accessCode, String name) async {
|
||||
}
|
||||
|
||||
print("GitHub createRepo: " + response.body);
|
||||
return json.decode(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";
|
||||
}
|
||||
|
||||
Future addDeployKey(String accessCode, String sshPublicKey, String repo) async {
|
||||
var url =
|
||||
"https://api.github.com/repos/$repo/keys?access_token=${accessCode}";
|
||||
"https://api.github.com/repos/$repo/keys?access_token=${_accessCode}";
|
||||
|
||||
Map<String, dynamic> data = {
|
||||
'title': "GitJournal",
|
||||
@ -154,3 +157,18 @@ Future addDeployKey(String accessCode, String sshPublicKey, String repo) async {
|
||||
print("GitHub addDeployKey: " + response.body);
|
||||
return json.decode(response.body);
|
||||
}
|
||||
}
|
||||
|
||||
class Repo {
|
||||
String fullName;
|
||||
|
||||
Repo({this.fullName});
|
||||
factory Repo.fromJson(Map<String, dynamic> parsedJson) {
|
||||
return new Repo(fullName: parsedJson['full_name']);
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return 'Repo{fulleName: $fullName}';
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,13 @@ var key =
|
||||
'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+VAh8r+vn0c+M+DacOo/szXcdMpxO1kIO3USkzgE5XdO83kQdDwh4Xc4P3dcc+FFSfVcEl3mSXGKbYC3G0ZoVcWd4ed40Gt3sLHSfNRQlRv+obnqKbzDLuOGfq65EkaJ90vrWBo/k7K8tBC2j1FZ/PUYy3DxeQkPEZXCMZDSG5P/+XoHn5IPcaxDpvlZjtOrx4H3pQ/YVI+XmyFAsZe+/Shy5sg4ilsdo4BQN2nODuBLwmgYu/hHmCcd8t4OxgBANVN8TMqHnZfRLixRSuXn0DbV4YOa/b2lBFQNvjkoBF6KhXOxZ+awyjyTpNp4AgF5c+3xptkNwUlwiQDCzcUmH your_email@example.com';
|
||||
|
||||
class OAuthAppState extends State<OAuthApp> {
|
||||
var github = new GitHub();
|
||||
String githubAccessCode = "";
|
||||
|
||||
void initState() {
|
||||
super.initState();
|
||||
initGitHub((String accessCode) {
|
||||
print("Got accessCode " + accessCode);
|
||||
githubAccessCode = accessCode;
|
||||
github.init(() {
|
||||
print("GitHub initialized and has access code");
|
||||
});
|
||||
}
|
||||
|
||||
@ -35,13 +35,13 @@ class OAuthAppState extends State<OAuthApp> {
|
||||
RaisedButton(
|
||||
child: Text("Open OAuth URL"),
|
||||
onPressed: () {
|
||||
launchOAuthScreen();
|
||||
github.launchOAuthScreen();
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("List Repos"),
|
||||
onPressed: () async {
|
||||
var repos = await listRepos(githubAccessCode);
|
||||
var repos = await github.listRepos();
|
||||
for (var repo in repos) {
|
||||
print(repo.fullName);
|
||||
}
|
||||
@ -51,7 +51,7 @@ class OAuthAppState extends State<OAuthApp> {
|
||||
child: Text("Create Repo"),
|
||||
onPressed: () async {
|
||||
try {
|
||||
await createRepo(githubAccessCode, "journal_test2");
|
||||
await github.createRepo("journal_test2");
|
||||
} catch (err) {
|
||||
print("Create Repo: " + err.toString());
|
||||
}
|
||||
@ -61,8 +61,7 @@ class OAuthAppState extends State<OAuthApp> {
|
||||
child: Text("Add Deploy Key"),
|
||||
onPressed: () async {
|
||||
try {
|
||||
await addDeployKey(
|
||||
githubAccessCode, key, "vhanda/journal_test2");
|
||||
await github.addDeployKey(key, "vhanda/journal_test2");
|
||||
} catch (err) {
|
||||
print("Deploy Key: " + err.toString());
|
||||
}
|
||||
|
Reference in New Issue
Block a user