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
|
/*******************************************************************************
* 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 v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Kevin Sawicki (GitHub Inc.) - initial API and implementation
*******************************************************************************/
package org.eclipse.mylyn.github.internal;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.Assert;
import com.google.gson.reflect.TypeToken;
/**
* Service class for getting and list gists.
*/
public class GistService {
private GitHubClient client;
/**
* Create gist service
*
* @param client
*/
public GistService(GitHubClient client) {
Assert.isNotNull(client, "Client cannot be null");
this.client = client;
}
/**
* Get gist
*
* @param id
* @return gist
* @throws IOException
*/
public Gist getGist(String id) throws IOException {
StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_GISTS);
uri.append('/').append(id).append(IGitHubConstants.SUFFIX_JSON);
return this.client.get(uri.toString(), Gist.class);
}
/**
* Get gists for specified user
*
* @param user
* @return list of gists
* @throws IOException
*/
public List<Gist> getGists(String user) throws IOException {
StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_USERS);
uri.append('/').append(user);
uri.append(IGitHubConstants.SEGMENT_GISTS).append(
IGitHubConstants.SUFFIX_JSON);
TypeToken<List<Gist>> gistToken = new TypeToken<List<Gist>>() {
};
return this.client.get(uri.toString(), gistToken.getType());
}
/**
* Create a gist
*
* @param gist
* @return created gist
* @throws IOException
*/
public Gist createGist(Gist gist) throws IOException {
StringBuilder uri = new StringBuilder();
User user = gist.getAuthor();
if (user != null) {
uri.append(IGitHubConstants.SEGMENT_USERS);
uri.append('/').append(user.getLogin());
}
uri.append(IGitHubConstants.SEGMENT_GISTS).append(
IGitHubConstants.SUFFIX_JSON);
return this.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 {
StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_GISTS);
uri.append('/').append(gistId);
uri.append(IGitHubConstants.SEGMENT_COMMENTS).append(
IGitHubConstants.SUFFIX_JSON);
Map<String, String> params = new HashMap<String, String>(1, 1);
params.put(IssueService.FIELD_BODY, comment);
return this.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 {
StringBuilder uri = new StringBuilder(IGitHubConstants.SEGMENT_GISTS);
uri.append('/').append(gistId);
uri.append(IGitHubConstants.SEGMENT_COMMENTS).append(
IGitHubConstants.SUFFIX_JSON);
TypeToken<List<Comment>> commentToken = new TypeToken<List<Comment>>() {
};
return this.client.get(uri.toString(), commentToken.getType());
}
}
|