-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathgists_service.dart
More file actions
205 lines (177 loc) · 5.82 KB
/
gists_service.dart
File metadata and controls
205 lines (177 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import 'dart:async';
import 'dart:convert';
import 'package:github/src/common.dart';
import 'package:github/src/common/util/pagination.dart';
/// The [GistsService] handles communication with gist
/// methods of the GitHub API.
///
/// API docs: https://developer.github.com/v3/gists/
class GistsService extends Service {
GistsService(GitHub github) : super(github);
/// lists gists for a user.
///
/// API docs: https://developer.github.com/v3/gists/#list-gists
Stream<Gist> listUserGists(String username) {
return PaginationHelper(github)
.objects('GET', '/users/$username/gists', (i) => Gist.fromJson(i));
}
/// Fetches the gists for the currently authenticated user.
/// If the user is not authenticated, this returns all public gists.
///
/// API docs: https://developer.github.com/v3/gists/#list-gists
Stream<Gist> listCurrentUserGists() {
return PaginationHelper(github)
.objects('GET', '/gists', (i) => Gist.fromJson(i));
}
/// Fetches the currently authenticated user's public gists.
///
/// API docs: https://developer.github.com/v3/gists/#list-gists
Stream<Gist> listCurrentUserPublicGists() {
return PaginationHelper(github)
.objects('GET', '/gists/public', (i) => Gist.fromJson(i));
}
/// Fetches the currently authenticated user's starred gists.
///
/// API docs: https://developer.github.com/v3/gists/#list-gists
Stream<Gist> listCurrentUserStarredGists() {
return PaginationHelper(github)
.objects('GET', '/gists/starred', (i) => Gist.fromJson(i));
}
/// Fetches a Gist by the specified [id].
///
/// API docs: https://developer.github.com/v3/gists/#get-a-single-gist
Future<Gist> getGist(String id) => github.getJSON('/gists/$id',
statusCode: StatusCodes.OK, convert: (i) => Gist.fromJson(i));
/// Creates a Gist
///
/// API docs: https://developer.github.com/v3/gists/#create-a-gist
Future<Gist> createGist(
Map<String, String> files, {
String description,
bool public = false,
}) {
final map = <String, dynamic>{'files': {}};
if (description != null) {
map['description'] = description;
}
map['public'] = public;
final f = {};
for (final key in files.keys) {
f[key] = {'content': files[key]};
}
map['files'] = f;
return github.postJSON(
'/gists',
statusCode: 201,
body: GitHubJson.encode(map),
convert: (i) => Gist.fromJson(i),
);
}
/// Deletes the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#delete-a-gist
Future<bool> deleteGist(String id) async {
await github.request(
'DELETE',
'/gists/$id',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}
/// Edits a Gist.
///
/// API docs: https://developer.github.com/v3/gists/#edit-a-gist
Future<Gist> editGist(
String id, {
String description,
Map<String, String> files,
}) {
final map = <String, dynamic>{};
if (description != null) {
map['description'] = description;
}
if (files != null) {
final f = <String, dynamic>{};
for (final key in files.keys) {
f[key] = files[key] == null ? null : {'content': files[key]};
}
map['files'] = f;
}
return github.postJSON(
'/gists/$id',
statusCode: 200,
body: GitHubJson.encode(map),
convert: (i) => Gist.fromJson(i),
);
}
// TODO: Implement listGistCommits: https://developer.github.com/v3/gists/#list-gist-commits
/// Star the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#star-a-gist
Future<bool> starGist(String id) async {
await github.request(
'POST',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}
/// Unstar the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#star-a-gist
Future<bool> unstarGist(String id) async {
await github.request(
'DELETE',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
}
/// Checks if the specified Gist is starred.
///
/// API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
Future<bool> isGistStarred(String id) async {
try {
await github.request(
'GET',
'/gists/$id/star',
statusCode: StatusCodes.NO_CONTENT,
);
return true;
} on NotFound {
return false;
}
}
/// Forks the specified Gist.
///
/// API docs: https://developer.github.com/v3/gists/#check-if-a-gist-is-starred
Future<Gist> forkGist(String id) {
return github
.request('POST', '/gists/$id/forks', statusCode: 201)
.then((response) {
return Gist.fromJson(jsonDecode(response.body) as Map<String, dynamic>);
});
}
// TODO: Implement listGistForks: https://developer.github.com/v3/gists/#list-gist-forks
/// Lists all comments for a gist.
///
/// API docs: https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
Stream<GistComment> listComments(String gistId) {
return PaginationHelper(github).objects(
'GET', '/gists/$gistId/comments', (i) => GistComment.fromJson(i));
}
// TODO: Implement getComment: https://developer.github.com/v3/gists/comments/#get-a-single-comment
/// Creates a comment for a gist.
///
/// API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
Future<GistComment> createComment(String gistId, CreateGistComment request) {
return github.postJSON(
'/gists/$gistId/comments',
body: GitHubJson.encode(request),
statusCode: StatusCodes.CREATED,
convert: (i) => GistComment.fromJson(i),
);
}
// TODO: Implement editComment: https://developer.github.com/v3/gists/comments/#edit-a-comment
// TODO: Implement deleteComment: https://developer.github.com/v3/gists/comments/#delete-a-comment
}