Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ae00cfbe66a0c44ec5cabd5dd1b25c3d14d04049 (plain) (blame)
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*******************************************************************************
 *  Copyright (c) 2011 GitHub Inc.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License 2.0
 *  which accompanies this distribution, and is available at
 *  https://www.eclipse.org/legal/epl-2.0/
 *
 *  SPDX-License-Identifier: EPL-2.0
 *
 *  Contributors:
 *    Kevin Sawicki (GitHub Inc.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.egit.github.core.service;

import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_COMMENTS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_FORK;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_GISTS;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_PUBLIC;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_STAR;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_STARRED;
import static org.eclipse.egit.github.core.client.IGitHubConstants.SEGMENT_USERS;
import static org.eclipse.egit.github.core.client.PagedRequest.PAGE_FIRST;
import static org.eclipse.egit.github.core.client.PagedRequest.PAGE_SIZE;

import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.eclipse.egit.github.core.Comment;
import org.eclipse.egit.github.core.Gist;
import org.eclipse.egit.github.core.client.GitHubClient;
import org.eclipse.egit.github.core.client.GitHubRequest;
import org.eclipse.egit.github.core.client.PageIterator;
import org.eclipse.egit.github.core.client.PagedRequest;

/**
 * Service class for interacting with Gists and Gist comments.
 *
 * @see <a href="http://developer.github.com/v3/gists">GitHub Gist API
 *      documentation</a>
 * @see <a href="http://developer.github.com/v3/gists/comments">GitHub Gist
 *      comments API documentation</a>
 */
public class GistService extends GitHubService {

	/**
	 * Create gist service
	 */
	public GistService() {
		super();
	}

	/**
	 * Create gist service
	 *
	 * @param client
	 */
	public GistService(GitHubClient client) {
		super(client);
	}

	/**
	 * Check that gist id is non-null and non-empty
	 *
	 * @param gistId
	 * @return gist id
	 */
	protected String checkGistId(String gistId) {
		if (gistId == null)
			throw new IllegalArgumentException("Gist id cannot be null"); //$NON-NLS-1$
		if (gistId.length() == 0)
			throw new IllegalArgumentException("Gist id cannot be empty"); //$NON-NLS-1$
		return gistId;
	}

	/**
	 * Get gist
	 *
	 * @param id
	 * @return gist
	 * @throws IOException
	 */
	public Gist getGist(String id) throws IOException {
		checkGistId(id);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(id);
		GitHubRequest request = createRequest();
		request.setUri(uri);
		request.setType(Gist.class);
		return (Gist) client.get(request).getBody();
	}

	/**
	 * Create page iterator for the current user's starred gists
	 *
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageStarredGists() {
		return pageStarredGists(PAGE_SIZE);
	}

	/**
	 * Create page iterator for the current user's starred gists
	 *
	 * @param size
	 *            size of page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageStarredGists(final int size) {
		return pageStarredGists(PAGE_FIRST, size);
	}

	/**
	 * Create page iterator for the current user's starred gists
	 *
	 * @param size
	 *            size of page
	 * @param start
	 *            starting page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageStarredGists(final int start, final int size) {
		PagedRequest<Gist> request = createPagedRequest(start, size);
		request.setUri(SEGMENT_GISTS + SEGMENT_STARRED);
		request.setType(new TypeToken<List<Gist>>() {
		}.getType());
		return createPageIterator(request);
	}

	/**
	 * Get starred gists for currently authenticated user
	 *
	 * @return list of gists
	 * @throws IOException
	 */
	public List<Gist> getStarredGists() throws IOException {
		return getAll(pageStarredGists());
	}

	/**
	 * Create user gist paged request
	 *
	 * @param user
	 * @param start
	 * @param size
	 * @return request
	 */
	protected PagedRequest<Gist> createUserGistRequest(String user, int start,
			int size) {
		if (user == null)
			throw new IllegalArgumentException("User cannot be null"); //$NON-NLS-1$
		if (user.length() == 0)
			throw new IllegalArgumentException("User cannot be empty"); //$NON-NLS-1$

		StringBuilder uri = new StringBuilder(SEGMENT_USERS);
		uri.append('/').append(user);
		uri.append(SEGMENT_GISTS);
		PagedRequest<Gist> request = createPagedRequest(start, size);
		request.setUri(uri).setType(new TypeToken<List<Gist>>() {
		}.getType());
		return request;
	}

	/**
	 * Get gists for specified user
	 *
	 * @param user
	 * @return list of gists
	 * @throws IOException
	 */
	public List<Gist> getGists(String user) throws IOException {
		return getAll(pageGists(user));
	}

	/**
	 * Create page iterator for given user's gists
	 *
	 * @param user
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageGists(final String user) {
		return pageGists(user, PAGE_SIZE);
	}

	/**
	 * Create page iterator for given user's gists
	 *
	 * @param user
	 * @param size
	 *            size of page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageGists(final String user, final int size) {
		return pageGists(user, PAGE_FIRST, size);
	}

	/**
	 * Create page iterator for given user's gists
	 *
	 * @param user
	 * @param size
	 *            size of page
	 * @param start
	 *            starting page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pageGists(final String user, final int start,
			final int size) {
		PagedRequest<Gist> request = createUserGistRequest(user, start, size);
		return createPageIterator(request);
	}

	/**
	 * Create page iterator for all public gists
	 *
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pagePublicGists() {
		return pagePublicGists(PAGE_SIZE);
	}

	/**
	 * Create page iterator for all public gists
	 *
	 * @param size
	 *            size of page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pagePublicGists(final int size) {
		return pagePublicGists(PAGE_FIRST, size);
	}

	/**
	 * Create page iterator for all public gists
	 *
	 * @param start
	 *            starting page number
	 * @param size
	 *            size of page
	 * @return gist page iterator
	 */
	public PageIterator<Gist> pagePublicGists(final int start, final int size) {
		PagedRequest<Gist> request = createPagedRequest(start, size);
		request.setUri(SEGMENT_GISTS + SEGMENT_PUBLIC);
		request.setType(new TypeToken<List<Gist>>() {
		}.getType());
		return createPageIterator(request);
	}

	/**
	 * Create a gist
	 *
	 * @param gist
	 * @return created gist
	 * @throws IOException
	 */
	public Gist createGist(Gist gist) throws IOException {
		if (gist == null)
			throw new IllegalArgumentException("Gist cannot be null"); //$NON-NLS-1$

		return client.post(SEGMENT_GISTS, gist, Gist.class);
	}

	/**
	 * Update a gist
	 *
	 * @param gist
	 * @return updated gist
	 * @throws IOException
	 */
	public Gist updateGist(Gist gist) throws IOException {
		if (gist == null)
			throw new IllegalArgumentException("Gist cannot be null"); //$NON-NLS-1$
		String id = gist.getId();
		checkGistId(id);

		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(id);
		return client.post(uri.toString(), gist, Gist.class);
	}

	/**
	 * Create comment on specified gist id
	 *
	 * @param gistId
	 * @param comment
	 * @return created issue
	 * @throws IOException
	 */
	public Comment createComment(String gistId, String comment)
			throws IOException {
		checkGistId(gistId);
		if (comment == null)
			throw new IllegalArgumentException("Gist comment cannot be null"); //$NON-NLS-1$

		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_COMMENTS);

		Map<String, String> params = Collections.singletonMap(
				IssueService.FIELD_BODY, comment);
		return client.post(uri.toString(), params, Comment.class);
	}

	/**
	 * Get comments for specified gist id
	 *
	 * @param gistId
	 * @return list of comments
	 * @throws IOException
	 */
	public List<Comment> getComments(String gistId) throws IOException {
		checkGistId(gistId);

		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_COMMENTS);
		PagedRequest<Comment> request = createPagedRequest();
		request.setUri(uri).setType(new TypeToken<List<Comment>>() {
		}.getType());
		return getAll(request);
	}

	/**
	 * Delete the Gist with the given id
	 *
	 * @param gistId
	 * @throws IOException
	 */
	public void deleteGist(String gistId) throws IOException {
		checkGistId(gistId);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		client.delete(uri.toString());
	}

	/**
	 * Get gist comment with id
	 *
	 * @param commentId
	 * @return comment
	 * @throws IOException
	 */
	public Comment getComment(long commentId) throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS + SEGMENT_COMMENTS);
		uri.append('/').append(commentId);
		GitHubRequest request = createRequest();
		request.setUri(uri);
		request.setType(Comment.class);
		return (Comment) client.get(request).getBody();
	}

	/**
	 * Edit gist comment
	 *
	 * @param comment
	 * @return edited comment
	 * @throws IOException
	 */
	public Comment editComment(Comment comment) throws IOException {
		if (comment == null)
			throw new IllegalArgumentException("Comment cannot be null"); //$NON-NLS-1$

		StringBuilder uri = new StringBuilder(SEGMENT_GISTS + SEGMENT_COMMENTS);
		uri.append('/').append(comment.getId());
		return client.post(uri.toString(), comment, Comment.class);
	}

	/**
	 * Delete the Gist comment with the given id
	 *
	 * @param commentId
	 * @throws IOException
	 */
	public void deleteComment(long commentId) throws IOException {
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS + SEGMENT_COMMENTS);
		uri.append('/').append(commentId);
		client.delete(uri.toString());
	}

	/**
	 * Star the gist with the given id
	 *
	 * @param gistId
	 * @throws IOException
	 */
	public void starGist(String gistId) throws IOException {
		checkGistId(gistId);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_STAR);
		client.put(uri.toString());
	}

	/**
	 * Unstar the gist with the given id
	 *
	 * @param gistId
	 * @throws IOException
	 */
	public void unstarGist(String gistId) throws IOException {
		checkGistId(gistId);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_STAR);
		client.delete(uri.toString());
	}

	/**
	 * Check if a gist is starred
	 *
	 * @param gistId
	 * @return true if starred, false if not starred
	 * @throws IOException
	 */
	public boolean isStarred(String gistId) throws IOException {
		checkGistId(gistId);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_STAR);
		return check(uri.toString());
	}

	/**
	 * Fork gist with given id
	 *
	 * @param gistId
	 * @return forked gist
	 * @throws IOException
	 */
	public Gist forkGist(String gistId) throws IOException {
		checkGistId(gistId);
		StringBuilder uri = new StringBuilder(SEGMENT_GISTS);
		uri.append('/').append(gistId);
		uri.append(SEGMENT_FORK);
		return client.post(uri.toString(), null, Gist.class);
	}
}

Back to the top