|
| 1 | +import 'dart:convert'; |
| 2 | +import 'package:github/github.dart'; |
| 3 | +import 'helper.dart'; |
| 4 | + |
| 5 | +class GithubBox { |
| 6 | + final String name; |
| 7 | + final String _username; |
| 8 | + final String _personalAccessToken; |
| 9 | + final String _repository; |
| 10 | + final String _folder; |
| 11 | + |
| 12 | + GithubBox( |
| 13 | + this._username, |
| 14 | + this._personalAccessToken, |
| 15 | + this._repository, |
| 16 | + this._folder, { |
| 17 | + required this.name, |
| 18 | + }); |
| 19 | + |
| 20 | + /// Example put("user", "flutter") |
| 21 | + /// or |
| 22 | + /// put("user": {"user: "flutter", "isDart": true}) |
| 23 | + Future<void> put(String key, dynamic value) async { |
| 24 | + final box = await GitHub( |
| 25 | + auth: Authentication.withToken(_personalAccessToken)) |
| 26 | + .repositories |
| 27 | + .getContents(RepositorySlug(_username, _repository), "$_folder/$name"); |
| 28 | + Map<String, dynamic> content = json.decode(box.file!.text); |
| 29 | + content.addAll({key: value}); |
| 30 | + await GitHub(auth: Authentication.withToken(_personalAccessToken)) |
| 31 | + .repositories |
| 32 | + .updateFile(RepositorySlug(_username, _repository), "$_folder/$name", |
| 33 | + "Modified key: $key", prettyByte(content), box.file!.sha!, |
| 34 | + branch: "main"); |
| 35 | + } |
| 36 | + |
| 37 | + /// Return single value of key |
| 38 | + Future<dynamic> get(String key) async { |
| 39 | + final box = await GitHub( |
| 40 | + auth: Authentication.withToken(_personalAccessToken)) |
| 41 | + .repositories |
| 42 | + .getContents(RepositorySlug(_username, _repository), "$_folder/$name"); |
| 43 | + Map<String, dynamic> content = json.decode(box.file!.text); |
| 44 | + return content[key]; |
| 45 | + } |
| 46 | + |
| 47 | + /// Return Map of all box/file |
| 48 | + Future<Map<String, dynamic>> getRawData() async { |
| 49 | + final box = await GitHub( |
| 50 | + auth: Authentication.withToken(_personalAccessToken)) |
| 51 | + .repositories |
| 52 | + .getContents(RepositorySlug(_username, _repository), "$_folder/$name"); |
| 53 | + return json.decode(box.file!.text); |
| 54 | + } |
| 55 | + |
| 56 | + /// Delete key |
| 57 | + Future<void> remove(String key) async { |
| 58 | + final box = await GitHub( |
| 59 | + auth: Authentication.withToken(_personalAccessToken)) |
| 60 | + .repositories |
| 61 | + .getContents(RepositorySlug(_username, _repository), "$_folder/$name"); |
| 62 | + Map<String, dynamic> content = json.decode(box.file!.text); |
| 63 | + content.remove(key); |
| 64 | + await GitHub(auth: Authentication.withToken(_personalAccessToken)) |
| 65 | + .repositories |
| 66 | + .updateFile(RepositorySlug(_username, _repository), "$_folder/$name", |
| 67 | + "Modified key: $key", prettyByte(content), box.file!.sha!, |
| 68 | + branch: "main"); |
| 69 | + } |
| 70 | +} |
0 commit comments