File tree Expand file tree Collapse file tree 4 files changed +67
-0
lines changed
patterns/iterator/github_commit Expand file tree Collapse file tree 4 files changed +67
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Commit {
2
+ String message;
3
+
4
+ Commit (this .message);
5
+ }
Original file line number Diff line number Diff line change
1
+ import 'dart:convert' ;
2
+ import 'dart:io' ;
3
+ import '../pattern/github_repo.dart' ;
4
+
5
+ class GitHubLoader {
6
+ static Future <GitHubRepo > get ({required userName, required repoName}) async {
7
+ final url = Uri .http (
8
+ 'api.github.com' ,
9
+ 'repos/$userName /$repoName /commits' ,
10
+ {'per_page' : '10' },
11
+ );
12
+ final json = await _loadCommits (url);
13
+ return GitHubRepo (json);
14
+ }
15
+
16
+ static Future <List <dynamic >> _loadCommits (Uri url) async {
17
+ final client = HttpClient ();
18
+ try {
19
+ final response = await client.getUrl (url);
20
+ final request = await response.close ();
21
+ final content = await request.transform (utf8.decoder).join ();
22
+ return jsonDecode (content);
23
+ } finally {
24
+ client.close ();
25
+ }
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ import 'github/commit.dart' ;
2
+ import 'github/github_loader.dart' ;
3
+ import 'pattern/github_repo.dart' ;
4
+
5
+ void main () async {
6
+ final GitHubRepo gitHubRepo = await GitHubLoader .get (
7
+ userName: 'RefactoringGuru' ,
8
+ repoName: 'design-patterns-dart' ,
9
+ );
10
+
11
+ print (
12
+ 'Iterate last 10 commits.'
13
+ '\n ----------------------------' ,
14
+ );
15
+
16
+ for (Commit commit in gitHubRepo.commitIterator ()) {
17
+ print (commit.message);
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ import '../github/commit.dart' ;
2
+
3
+ class GitHubRepo {
4
+ GitHubRepo (this ._json);
5
+
6
+ Iterable <Commit > commitIterator () sync * {
7
+ for (final jsonCommit in _json) {
8
+ var message = jsonCommit['commit' ]['message' ] as String ;
9
+ message = message.replaceAll (RegExp (r'\n+' ), ' ' );
10
+
11
+ yield Commit (message);
12
+ }
13
+ }
14
+
15
+ final List <dynamic > _json;
16
+ }
You can’t perform that action at this time.
0 commit comments