Skip to content

Commit c3ad34a

Browse files
committed
updates to immutable, more json_serializable
1 parent e4146ac commit c3ad34a

File tree

9 files changed

+294
-170
lines changed

9 files changed

+294
-170
lines changed

lib/src/common/authorizations_service.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ class AuthorizationsService extends Service {
1717
/// API docs: https://developer.github.com/v3/oauth_authorizations/#list-your-authorizations
1818
Stream<Authorization> listAuthorizations() {
1919
return PaginationHelper(github)
20-
.objects('GET', '/authorizations', Authorization.fromJSON);
20+
.objects('GET', '/authorizations', (i) => Authorization.fromJson(i));
2121
}
2222

2323
/// Fetches an authorization specified by [id].
2424
///
2525
/// API docs: https://developer.github.com/v3/oauth_authorizations/#get-a-single-authorization
2626
Future<Authorization> getAuthorization(int id) =>
2727
github.getJSON('/authorizations/$id',
28-
statusCode: 200, convert: Authorization.fromJSON);
28+
statusCode: 200, convert: (i) => Authorization.fromJson(i));
2929

3030
// TODO: Implement remaining API methods of authorizations:
3131
// See https://developer.github.com/v3/oauth_authorizations/

lib/src/common/gists_service.dart

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,39 @@ class GistsService extends Service {
1515
/// API docs: https://developer.github.com/v3/gists/#list-gists
1616
Stream<Gist> listUserGists(String username) {
1717
return PaginationHelper(github)
18-
.objects('GET', '/users/$username/gists', Gist.fromJSON);
18+
.objects('GET', '/users/$username/gists', (i) => Gist.fromJson(i));
1919
}
2020

2121
/// Fetches the gists for the currently authenticated user.
2222
/// If the user is not authenticated, this returns all public gists.
2323
///
2424
/// API docs: https://developer.github.com/v3/gists/#list-gists
2525
Stream<Gist> listCurrentUserGists() {
26-
return PaginationHelper(github).objects('GET', '/gists', Gist.fromJSON);
26+
return PaginationHelper(github)
27+
.objects('GET', '/gists', (i) => Gist.fromJson(i));
2728
}
2829

2930
/// Fetches the currently authenticated user's public gists.
3031
///
3132
/// API docs: https://developer.github.com/v3/gists/#list-gists
3233
Stream<Gist> listCurrentUserPublicGists() {
3334
return PaginationHelper(github)
34-
.objects('GET', '/gists/public', Gist.fromJSON);
35+
.objects('GET', '/gists/public', (i) => Gist.fromJson(i));
3536
}
3637

3738
/// Fetches the currently authenticated user's starred gists.
3839
///
3940
/// API docs: https://developer.github.com/v3/gists/#list-gists
4041
Stream<Gist> listCurrentUserStarredGists() {
4142
return PaginationHelper(github)
42-
.objects('GET', '/gists/starred', Gist.fromJSON);
43+
.objects('GET', '/gists/starred', (i) => Gist.fromJson(i));
4344
}
4445

4546
/// Fetches a Gist by the specified [id].
4647
///
4748
/// API docs: https://developer.github.com/v3/gists/#get-a-single-gist
4849
Future<Gist> getGist(String id) => github.getJSON('/gists/$id',
49-
statusCode: StatusCodes.OK, convert: Gist.fromJSON);
50+
statusCode: StatusCodes.OK, convert: (i) => Gist.fromJson(i));
5051

5152
/// Creates a Gist
5253
///
@@ -76,7 +77,7 @@ class GistsService extends Service {
7677
'/gists',
7778
statusCode: 201,
7879
body: jsonEncode(map),
79-
convert: Gist.fromJSON,
80+
convert: (i) => Gist.fromJson(i),
8081
);
8182
}
8283

@@ -115,7 +116,7 @@ class GistsService extends Service {
115116
'/gists/$id',
116117
statusCode: 200,
117118
body: jsonEncode(map),
118-
convert: Gist.fromJSON,
119+
convert: (i) => Gist.fromJson(i),
119120
);
120121
}
121122

@@ -155,7 +156,7 @@ class GistsService extends Service {
155156
return github
156157
.request('POST', '/gists/$id/forks', statusCode: 201)
157158
.then((response) {
158-
return Gist.fromJSON(jsonDecode(response.body) as Map<String, dynamic>);
159+
return Gist.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
159160
});
160161
}
161162

lib/src/common/model/activity.dart

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,53 @@
11
import 'package:github/src/common.dart';
22
import 'package:github/src/common/model/users.dart';
33
import 'package:json_annotation/json_annotation.dart';
4+
import 'package:meta/meta.dart';
45

56
part 'activity.g.dart';
67

78
/// Model class for an event.
9+
@immutable
810
@JsonSerializable(createToJson: false)
911
class Event {
10-
Event();
11-
String id;
12-
String type;
13-
Repository repo;
14-
User actor;
15-
Organization org;
16-
Map<String, dynamic> payload;
12+
const Event({
13+
@required this.id,
14+
@required this.type,
15+
@required this.repo,
16+
@required this.actor,
17+
@required this.org,
18+
@required this.payload,
19+
@required this.createdAt,
20+
});
21+
final String id;
22+
final String type;
23+
final Repository repo;
24+
final User actor;
25+
final Organization org;
26+
final Map<String, dynamic> payload;
1727

1828
@JsonKey(name: 'created_at')
19-
DateTime createdAt;
29+
final DateTime createdAt;
2030

21-
factory Event.fromJson(Map<String, dynamic> input) {
22-
return _$EventFromJson(input);
23-
}
31+
factory Event.fromJson(Map<String, dynamic> input) => _$EventFromJson(input);
2432
}
2533

2634
/// Model class for a repository subscription.
35+
@immutable
2736
@JsonSerializable(createToJson: false)
2837
class RepositorySubscription {
29-
RepositorySubscription();
30-
bool subscribed;
31-
bool ignored;
32-
String reason;
38+
const RepositorySubscription({
39+
@required this.subscribed,
40+
@required this.ignored,
41+
@required this.reason,
42+
@required this.createdAt,
43+
});
44+
final bool subscribed;
45+
final bool ignored;
46+
final String reason;
3347

3448
@JsonKey(name: 'created_at')
35-
DateTime createdAt;
49+
final DateTime createdAt;
3650

37-
factory RepositorySubscription.fromJson(Map<String, dynamic> input) {
38-
return _$RepositorySubscriptionFromJson(input);
39-
}
51+
factory RepositorySubscription.fromJson(Map<String, dynamic> input) =>
52+
_$RepositorySubscriptionFromJson(input);
4053
}

lib/src/common/model/activity.g.dart

Lines changed: 20 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,60 @@
11
import 'dart:convert';
22
import 'package:github/src/common.dart';
33
import 'package:github/src/common/model/users.dart';
4-
import 'package:github/src/util.dart';
54
import 'package:json_annotation/json_annotation.dart';
5+
import 'package:meta/meta.dart';
6+
7+
part 'authorizations.g.dart';
68

79
/// Model class for an authorization.
10+
@immutable
11+
@JsonSerializable(createToJson: false)
812
class Authorization {
9-
int id;
10-
11-
List<String> scopes;
12-
String token;
13-
AuthorizationApplication app;
14-
String note;
15-
String noteUrl;
16-
DateTime createdAt;
17-
DateTime updatedAt;
18-
User user;
13+
const Authorization(
14+
{@required this.id,
15+
@required this.scopes,
16+
@required this.token,
17+
@required this.app,
18+
@required this.note,
19+
@required this.noteUrl,
20+
@required this.createdAt,
21+
@required this.updatedAt,
22+
@required this.user});
1923

20-
Map<String, dynamic> json;
21-
22-
static Authorization fromJSON(Map<String, dynamic> input) {
23-
if (input == null) return null;
24+
final int id;
25+
final List<String> scopes;
26+
final String token;
27+
final AuthorizationApplication app;
28+
final String note;
29+
@JsonKey(name: 'note_url')
30+
final String noteUrl;
31+
@JsonKey(name: 'created_at')
32+
final DateTime createdAt;
33+
@JsonKey(name: 'updated_at')
34+
final DateTime updatedAt;
35+
final User user;
2436

25-
return Authorization()
26-
..id = input['id']
27-
..scopes = input['scopes'] as List<String>
28-
..token = input['token']
29-
..app = AuthorizationApplication.fromJSON(
30-
input['app'] as Map<String, dynamic>)
31-
..note = input['note']
32-
..noteUrl = input['note_url']
33-
..createdAt = parseDateTime(input['created_at'])
34-
..updatedAt = parseDateTime(input['updated_at'])
35-
..json = input
36-
..user = User.fromJson(input['user'] as Map<String, dynamic>);
37-
}
37+
factory Authorization.fromJson(Map<String, dynamic> input) =>
38+
_$AuthorizationFromJson(input);
3839
}
3940

4041
/// Model class for an application of an [Authorization].
42+
@immutable
43+
@JsonSerializable(createToJson: false)
4144
class AuthorizationApplication {
42-
String url;
43-
String name;
45+
const AuthorizationApplication({this.url, this.name, this.clientID});
4446

45-
@JsonKey(name: 'client_id')
46-
String clientID;
47-
48-
AuthorizationApplication();
47+
final String url;
48+
final String name;
4949

50-
static AuthorizationApplication fromJSON(Map<String, dynamic> input) {
51-
if (input == null) return null;
50+
@JsonKey(name: 'client_id')
51+
final String clientID;
5252

53-
return AuthorizationApplication()
54-
..url = input['url']
55-
..name = input['name']
56-
..clientID = input['client_id'];
57-
}
53+
factory AuthorizationApplication.fromJson(Map<String, dynamic> input) =>
54+
_$AuthorizationApplicationFromJson(input);
5855
}
5956

57+
@JsonSerializable()
6058
class CreateAuthorization {
6159
final String note;
6260

@@ -67,12 +65,11 @@ class CreateAuthorization {
6765

6866
CreateAuthorization(this.note);
6967

68+
factory CreateAuthorization.fromJson(Map<String, dynamic> json) =>
69+
_$CreateAuthorizationFromJson(json);
70+
Map<String, dynamic> toJson() => _$CreateAuthorizationToJson(this);
71+
7072
String toJSON() {
71-
final map = <String, dynamic>{};
72-
putValue('note', note, map);
73-
putValue('note_url', noteUrl, map);
74-
putValue('client_id', clientID, map);
75-
putValue('client_secret', clientSecret, map);
76-
return jsonEncode(map);
73+
return jsonEncode(toJson());
7774
}
7875
}

lib/src/common/model/authorizations.g.dart

Lines changed: 59 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)