Skip to content

Commit a8c568e

Browse files
committed
large refactor
- all static methods are now factory constructors - fromJSON is now fromJson everywhere - toJSON is now toJson everywhere - Use JsonSerializable everywhere - removed deprecated items - renamed some fields with ID at the end to be Id
1 parent c3ad34a commit a8c568e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+3216
-1735
lines changed

analysis_options.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ linter:
582582
# http://dart-lang.github.io/linter/lints/prefer_final_fields.html
583583
# recommendation: optional
584584
# 0 issues
585-
- prefer_final_fields
585+
# - prefer_final_fields
586586

587587
# Prefer final in for-each loop variable if reference is not reassigned.
588588
# http://dart-lang.github.io/linter/lints/prefer_final_in_for_each.html
@@ -595,7 +595,7 @@ linter:
595595
# recommendation: optional
596596
# reason: Generates a lot of lint since people use var a lot for local variables.
597597
# 0 issues
598-
- prefer_final_locals
598+
# - prefer_final_locals
599599

600600
# Prefer for elements when building maps from iterables.
601601
# http://dart-lang.github.io/linter/lints/prefer_for_elements_to_map_fromIterable.html

example/readme.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DivElement readmeDiv;
1010
Future<void> main() async {
1111
await initViewSourceButton('readme.dart');
1212
readmeDiv = querySelector('#readme');
13-
const repo = RepositorySlug('SpinlockLabs', 'github.dart');
13+
var repo = RepositorySlug('SpinlockLabs', 'github.dart');
1414
final readme = await github.repositories.getReadme(repo);
1515
String markdown = readme.content;
1616
if (readme.encoding == 'base64') {

example/releases.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Future<void> main() async {
1414

1515
void loadReleases() {
1616
github.repositories
17-
.listReleases(const RepositorySlug('twbs', 'bootstrap'))
17+
.listReleases(RepositorySlug('twbs', 'bootstrap'))
1818
.toList()
1919
.then((releases) {
2020
for (final release in releases) {

lib/src/common/activity_service.dart

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class ActivityService extends Service {
131131
Stream<Notification> listNotifications(
132132
{bool all = false, bool participating = false}) {
133133
return PaginationHelper(github).objects(
134-
'GET', '/notifications', Notification.fromJSON,
134+
'GET', '/notifications', (i) => Notification.fromJson(i),
135135
params: {'all': all, 'participating': participating});
136136
}
137137

@@ -140,8 +140,10 @@ class ActivityService extends Service {
140140
/// API docs: https://developer.github.com/v3/activity/notifications/#list-your-notifications-in-a-repository
141141
Stream<Notification> listRepositoryNotifications(RepositorySlug repository,
142142
{bool all = false, bool participating = false}) {
143-
return PaginationHelper(github).objects('GET',
144-
'/repos/${repository.fullName}/notifications', Notification.fromJSON,
143+
return PaginationHelper(github).objects(
144+
'GET',
145+
'/repos/${repository.fullName}/notifications',
146+
(i) => Notification.fromJson(i),
145147
params: {'all': all, 'participating': participating});
146148
}
147149

@@ -185,7 +187,7 @@ class ActivityService extends Service {
185187
/// API docs: https://developer.github.com/v3/activity/notifications/#view-a-single-thread
186188
Future<Notification> getThread(String threadId) =>
187189
github.getJSON('/notification/threads/$threadId',
188-
statusCode: StatusCodes.OK, convert: Notification.fromJSON);
190+
statusCode: StatusCodes.OK, convert: (i) => Notification.fromJson(i));
189191

190192
/// Mark the specified notification thread as read.
191193
///
@@ -206,8 +208,8 @@ class ActivityService extends Service {
206208
///
207209
/// API docs: https://developer.github.com/v3/activity/starring/#list-stargazers
208210
Stream<User> listStargazers(RepositorySlug slug) {
209-
return PaginationHelper(github)
210-
.objects('GET', '/repos/${slug.fullName}/stargazers', User.fromJson);
211+
return PaginationHelper(github).objects(
212+
'GET', '/repos/${slug.fullName}/stargazers', (i) => User.fromJson(i));
211213
}
212214

213215
/// Lists all the repos starred by a user.
@@ -261,8 +263,8 @@ class ActivityService extends Service {
261263
///
262264
/// API docs: https://developer.github.com/v3/activity/watching/#list-watchers
263265
Stream<User> listWatchers(RepositorySlug slug) {
264-
return PaginationHelper(github)
265-
.objects('GET', '/repos/${slug.fullName}/subscribers', User.fromJson);
266+
return PaginationHelper(github).objects(
267+
'GET', '/repos/${slug.fullName}/subscribers', (i) => User.fromJson(i));
266268
}
267269

268270
/// Lists the repositories the specified user is watching.

lib/src/common/gists_service.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ class GistsService extends Service {
166166
///
167167
/// API docs: https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
168168
Stream<GistComment> listComments(String gistId) {
169-
return PaginationHelper(github)
170-
.objects('GET', '/gists/$gistId/comments', GistComment.fromJSON);
169+
return PaginationHelper(github).objects(
170+
'GET', '/gists/$gistId/comments', (i) => GistComment.fromJson(i));
171171
}
172172

173173
// TODO: Implement getComment: https://developer.github.com/v3/gists/comments/#get-a-single-comment
@@ -177,7 +177,7 @@ class GistsService extends Service {
177177
/// API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
178178
Future<GistComment> createComment(String gistId, CreateGistComment request) {
179179
return github.postJSON('/gists/$gistId/comments',
180-
body: request.toJSON(), convert: GistComment.fromJSON);
180+
body: jsonEncode(request), convert: (i) => GistComment.fromJson(i));
181181
}
182182

183183
// TODO: Implement editComment: https://developer.github.com/v3/gists/comments/#edit-a-comment

lib/src/common/git_service.dart

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ class GitService extends Service {
1515
/// API docs: https://developer.github.com/v3/git/blobs/#get-a-blob
1616
Future<GitBlob> getBlob(RepositorySlug slug, String sha) =>
1717
github.getJSON('/repos/${slug.fullName}/git/blobs/$sha',
18-
convert: GitBlob.fromJSON, statusCode: StatusCodes.OK);
18+
convert: (i) => GitBlob.fromJson(i), statusCode: StatusCodes.OK);
1919

2020
/// Creates a blob with specified [blob] content.
2121
///
2222
/// API docs: https://developer.github.com/v3/git/blobs/#create-a-blob
2323
Future<GitBlob> createBlob(RepositorySlug slug, CreateGitBlob blob) {
2424
return github.postJSON('/repos/${slug.fullName}/git/blobs',
25-
convert: GitBlob.fromJSON,
25+
convert: (i) => GitBlob.fromJson(i),
2626
statusCode: StatusCodes.CREATED,
2727
body: jsonEncode(blob));
2828
}
@@ -32,14 +32,14 @@ class GitService extends Service {
3232
/// API docs: https://developer.github.com/v3/git/commits/#get-a-commit
3333
Future<GitCommit> getCommit(RepositorySlug slug, String sha) =>
3434
github.getJSON('/repos/${slug.fullName}/git/commits/$sha',
35-
convert: GitCommit.fromJSON, statusCode: StatusCodes.OK);
35+
convert: (i) => GitCommit.fromJson(i), statusCode: StatusCodes.OK);
3636

3737
/// Creates a new commit in a repository.
3838
///
3939
/// API docs: https://developer.github.com/v3/git/commits/#create-a-commit
4040
Future<GitCommit> createCommit(RepositorySlug slug, CreateGitCommit commit) {
4141
return github.postJSON('/repos/${slug.fullName}/git/commits',
42-
convert: GitCommit.fromJSON,
42+
convert: (i) => GitCommit.fromJson(i),
4343
statusCode: StatusCodes.CREATED,
4444
body: jsonEncode(commit));
4545
}
@@ -51,7 +51,7 @@ class GitService extends Service {
5151
/// API docs: https://developer.github.com/v3/git/refs/#get-a-reference
5252
Future<GitReference> getReference(RepositorySlug slug, String ref) =>
5353
github.getJSON('/repos/${slug.fullName}/git/refs/$ref',
54-
convert: GitReference.fromJSON, statusCode: StatusCodes.OK);
54+
convert: (i) => GitReference.fromJson(i), statusCode: StatusCodes.OK);
5555

5656
/// Lists the references in a repository.
5757
///
@@ -66,7 +66,8 @@ class GitService extends Service {
6666
path += '/$type';
6767
}
6868

69-
return PaginationHelper(github).objects('GET', path, GitReference.fromJSON);
69+
return PaginationHelper(github)
70+
.objects('GET', path, (i) => GitReference.fromJson(i));
7071
}
7172

7273
/// Creates a new reference in a repository.
@@ -78,7 +79,7 @@ class GitService extends Service {
7879
Future<GitReference> createReference(
7980
RepositorySlug slug, String ref, String sha) {
8081
return github.postJSON('/repos/${slug.fullName}/git/refs',
81-
convert: GitReference.fromJSON,
82+
convert: (i) => GitReference.fromJson(i),
8283
statusCode: StatusCodes.CREATED,
8384
body: jsonEncode({'ref': ref, 'sha': sha}));
8485
}
@@ -100,7 +101,7 @@ class GitService extends Service {
100101
.request('PATCH', '/repos/${slug.fullName}/git/refs/$ref',
101102
body: body, headers: headers)
102103
.then((response) {
103-
return GitReference.fromJSON(
104+
return GitReference.fromJson(
104105
jsonDecode(response.body) as Map<String, dynamic>);
105106
});
106107
}
@@ -119,16 +120,16 @@ class GitService extends Service {
119120
/// API docs: https://developer.github.com/v3/git/tags/#get-a-tag
120121
Future<GitTag> getTag(RepositorySlug slug, String sha) =>
121122
github.getJSON('/repos/${slug.fullName}/git/tags/$sha',
122-
convert: GitTag.fromJSON, statusCode: StatusCodes.OK);
123+
convert: (i) => GitTag.fromJson(i), statusCode: StatusCodes.OK);
123124

124125
/// Creates a new tag in a repository.
125126
///
126127
/// API docs: https://developer.github.com/v3/git/tags/#create-a-tag-object
127128
Future<GitTag> createTag(RepositorySlug slug, CreateGitTag tag) =>
128129
github.postJSON('/repos/${slug.fullName}/git/tags',
129-
convert: GitTag.fromJSON,
130+
convert: (i) => GitTag.fromJson(i),
130131
statusCode: StatusCodes.CREATED,
131-
body: tag.toJSON());
132+
body: jsonEncode(tag));
132133

133134
/// Fetches a tree from a repository for the given ref [sha].
134135
///
@@ -154,6 +155,6 @@ class GitService extends Service {
154155
return github.postJSON('/repos/${slug.fullName}/git/trees',
155156
convert: (j) => GitTree.fromJson(j),
156157
statusCode: StatusCodes.CREATED,
157-
body: tree.toJSON());
158+
body: jsonEncode(tree));
158159
}
159160
}

lib/src/common/issues_service.dart

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class IssuesService extends Service {
124124
return PaginationHelper(github).objects(
125125
'GET',
126126
pathSegment,
127-
Issue.fromJSON,
127+
(i) => Issue.fromJson(i),
128128
params: params,
129129
);
130130
}
@@ -136,9 +136,9 @@ class IssuesService extends Service {
136136
RepositorySlug slug, int issueNumber, IssueRequest issue) async {
137137
return github
138138
.request('PATCH', '/repos/${slug.fullName}/issues/$issueNumber',
139-
body: issue.toJSON())
139+
body: jsonEncode(issue))
140140
.then<Issue>((response) {
141-
return Issue.fromJSON(jsonDecode(response.body) as Map<String, dynamic>);
141+
return Issue.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
142142
});
143143
}
144144

@@ -147,7 +147,7 @@ class IssuesService extends Service {
147147
/// API docs: https://developer.github.com/v3/issues/#get-a-single-issue
148148
Future<Issue> get(RepositorySlug slug, int issueNumber) =>
149149
github.getJSON('/repos/${slug.fullName}/issues/$issueNumber',
150-
convert: Issue.fromJSON);
150+
convert: (i) => Issue.fromJson(i));
151151

152152
/// Create an issue.
153153
///
@@ -156,24 +156,24 @@ class IssuesService extends Service {
156156
final response = await github.request(
157157
'POST',
158158
'/repos/${slug.fullName}/issues',
159-
body: issue.toJSON(),
159+
body: jsonEncode(issue),
160160
);
161161

162162
if (StatusCodes.isClientError(response.statusCode)) {
163163
//TODO: throw a more friendly error – better this than silent failure
164164
throw GitHubError(github, response.body);
165165
}
166166

167-
return Issue.fromJSON(jsonDecode(response.body) as Map<String, dynamic>);
167+
return Issue.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
168168
}
169169

170170
/// Lists all available assignees (owners and collaborators) to which issues
171171
/// may be assigned.
172172
///
173173
/// API docs: https://developer.github.com/v3/issues/assignees/#list-assignees
174174
Stream<User> listAssignees(RepositorySlug slug) {
175-
return PaginationHelper(github)
176-
.objects('GET', '/repos/${slug.fullName}/assignees', User.fromJson);
175+
return PaginationHelper(github).objects(
176+
'GET', '/repos/${slug.fullName}/assignees', (i) => User.fromJson(i));
177177
}
178178

179179
/// Checks if a user is an assignee for the specified repository.
@@ -193,23 +193,25 @@ class IssuesService extends Service {
193193
return PaginationHelper(github).objects(
194194
'GET',
195195
'/repos/${slug.fullName}/issues/$issueNumber/comments',
196-
IssueComment.fromJSON);
196+
(i) => IssueComment.fromJson(i));
197197
}
198198

199199
/// Lists all comments in a repository.
200200
///
201201
/// API docs: https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
202202
Stream<IssueComment> listCommentsByRepo(RepositorySlug slug) {
203-
return PaginationHelper(github).objects('GET',
204-
'/repos/${slug.fullName}/issues/comments', IssueComment.fromJSON);
203+
return PaginationHelper(github).objects(
204+
'GET',
205+
'/repos/${slug.fullName}/issues/comments',
206+
(i) => IssueComment.fromJson(i));
205207
}
206208

207209
/// Fetches the specified issue comment.
208210
///
209211
/// API docs: https://developer.github.com/v3/issues/comments/#get-a-single-comment
210212
Future<IssueComment> getComment(RepositorySlug slug, int id) =>
211213
github.getJSON('/repos/${slug.fullName}/issues/comments/$id',
212-
convert: IssueComment.fromJSON);
214+
convert: (i) => IssueComment.fromJson(i));
213215

214216
/// Creates a new comment on the specified issue
215217
///
@@ -220,7 +222,7 @@ class IssuesService extends Service {
220222
return github.postJSON(
221223
'/repos/${slug.fullName}/issues/$issueNumber/comments',
222224
body: it,
223-
convert: IssueComment.fromJSON,
225+
convert: (i) => IssueComment.fromJson(i),
224226
statusCode: StatusCodes.CREATED,
225227
);
226228
}
@@ -242,16 +244,16 @@ class IssuesService extends Service {
242244
///
243245
/// API docs: https://developer.github.com/v3/issues/labels/#list-all-labels-for-this-repository
244246
Stream<IssueLabel> listLabels(RepositorySlug slug) {
245-
return PaginationHelper(github)
246-
.objects('GET', '/repos/${slug.fullName}/labels', IssueLabel.fromJSON);
247+
return PaginationHelper(github).objects(
248+
'GET', '/repos/${slug.fullName}/labels', (i) => IssueLabel.fromJson(i));
247249
}
248250

249251
/// Fetches a single label.
250252
///
251253
/// API docs: https://developer.github.com/v3/issues/labels/#get-a-single-label
252254
Future<IssueLabel> getLabel(RepositorySlug slug, String name) =>
253255
github.getJSON('/repos/${slug.fullName}/labels/$name',
254-
convert: IssueLabel.fromJSON, statusCode: StatusCodes.OK);
256+
convert: (i) => IssueLabel.fromJson(i), statusCode: StatusCodes.OK);
255257

256258
/// Creates a new label on the specified repository.
257259
///
@@ -260,7 +262,7 @@ class IssuesService extends Service {
260262
RepositorySlug slug, String name, String color) {
261263
return github.postJSON('/repos/${slug.fullName}/labels',
262264
body: jsonEncode({'name': name, 'color': color}),
263-
convert: IssueLabel.fromJSON);
265+
convert: (i) => IssueLabel.fromJson(i));
264266
}
265267

266268
/// Edits a label.
@@ -269,7 +271,7 @@ class IssuesService extends Service {
269271
Future<IssueLabel> editLabel(RepositorySlug slug, String name, String color) {
270272
return github.postJSON('/repos/${slug.fullName}/labels/$name',
271273
body: jsonEncode({'name': name, 'color': color}),
272-
convert: IssueLabel.fromJSON);
274+
convert: (i) => IssueLabel.fromJson(i));
273275
}
274276

275277
/// Deletes a label.
@@ -289,7 +291,7 @@ class IssuesService extends Service {
289291
return PaginationHelper(github).objects(
290292
'GET',
291293
'/repos/${slug.fullName}/issues/$issueNumber/labels',
292-
IssueLabel.fromJSON);
294+
(i) => IssueLabel.fromJson(i));
293295
}
294296

295297
/// Adds labels to an issue.
@@ -300,8 +302,10 @@ class IssuesService extends Service {
300302
return github.postJSON<List<dynamic>, List<IssueLabel>>(
301303
'/repos/${slug.fullName}/issues/$issueNumber/labels',
302304
body: jsonEncode(labels),
303-
convert: (input) =>
304-
input.cast<Map<String, dynamic>>().map(IssueLabel.fromJSON).toList(),
305+
convert: (input) => input
306+
.cast<Map<String, dynamic>>()
307+
.map((i) => IssueLabel.fromJson(i))
308+
.toList(),
305309
);
306310
}
307311

@@ -315,7 +319,7 @@ class IssuesService extends Service {
315319
body: jsonEncode(labels))
316320
.then((response) {
317321
return jsonDecode(response.body)
318-
.map((Map<String, dynamic> it) => IssueLabel.fromJSON(it));
322+
.map((Map<String, dynamic> it) => IssueLabel.fromJson(it));
319323
});
320324
}
321325

@@ -345,8 +349,8 @@ class IssuesService extends Service {
345349
///
346350
/// API docs: https://developer.github.com/v3/issues/milestones/#list-milestones-for-a-repository
347351
Stream<Milestone> listMilestones(RepositorySlug slug) {
348-
return PaginationHelper(github).objects(
349-
'GET', '/repos/${slug.fullName}/milestones', Milestone.fromJSON);
352+
return PaginationHelper(github).objects('GET',
353+
'/repos/${slug.fullName}/milestones', (i) => Milestone.fromJson(i));
350354
}
351355

352356
// TODO: Implement getMilestone: https://developer.github.com/v3/issues/milestones/#get-a-single-milestone
@@ -357,7 +361,7 @@ class IssuesService extends Service {
357361
Future<Milestone> createMilestone(
358362
RepositorySlug slug, CreateMilestone request) {
359363
return github.postJSON('/repos/${slug.fullName}/milestones',
360-
body: jsonEncode(request.toJSON()), convert: Milestone.fromJSON);
364+
body: jsonEncode(request), convert: (i) => Milestone.fromJson(i));
361365
}
362366

363367
// TODO: Implement editMilestone: https://developer.github.com/v3/issues/milestones/#update-a-milestone

0 commit comments

Comments
 (0)