1 | 2 | |
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 | |
package com.jcabi.github; |
31 | |
|
32 | |
import com.jcabi.aspects.Immutable; |
33 | |
import com.jcabi.aspects.Loggable; |
34 | |
import com.jcabi.http.Request; |
35 | |
import com.jcabi.http.response.JsonResponse; |
36 | |
import com.jcabi.http.response.RestResponse; |
37 | |
import java.io.IOException; |
38 | |
import java.net.HttpURLConnection; |
39 | |
import javax.json.Json; |
40 | |
import javax.json.JsonObject; |
41 | |
import javax.json.JsonStructure; |
42 | |
import javax.json.JsonValue; |
43 | |
import lombok.EqualsAndHashCode; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@Immutable |
53 | |
@Loggable(Loggable.DEBUG) |
54 | 0 | @EqualsAndHashCode(of = { "entry", "request", "owner" }) |
55 | |
@SuppressWarnings("PMD.AvoidDuplicateLiterals") |
56 | 4 | final class RtContents implements Contents { |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private final transient Request entry; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
private final transient Repo owner; |
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
private final transient Request request; |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 10 | public RtContents(final Request req, final Repo repo) { |
79 | 10 | this.entry = req; |
80 | 10 | this.owner = repo; |
81 | 10 | this.request = req.uri() |
82 | 10 | .path("/repos") |
83 | 10 | .path(repo.coordinates().user()) |
84 | 10 | .path(repo.coordinates().repo()) |
85 | 10 | .path("/contents") |
86 | 10 | .back(); |
87 | 10 | } |
88 | |
|
89 | |
@Override |
90 | |
public Repo repo() { |
91 | 0 | return this.owner; |
92 | |
} |
93 | |
|
94 | |
@Override |
95 | |
public Content readme() throws IOException { |
96 | 3 | return new RtContent( |
97 | |
this.entry, this.owner, |
98 | 1 | this.entry.uri() |
99 | 1 | .path("/repos") |
100 | 1 | .path(this.owner.coordinates().user()) |
101 | 1 | .path(this.owner.coordinates().repo()) |
102 | 1 | .path("/readme") |
103 | 1 | .back() |
104 | 1 | .method(Request.GET) |
105 | 1 | .fetch() |
106 | 1 | .as(RestResponse.class) |
107 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
108 | 1 | .as(JsonResponse.class) |
109 | 1 | .json().readObject().getString("path") |
110 | |
); |
111 | |
} |
112 | |
|
113 | |
@Override |
114 | |
public Content readme( |
115 | |
final String branch |
116 | |
) throws IOException { |
117 | 2 | final JsonStructure json = Json.createObjectBuilder() |
118 | 1 | .add("ref", branch) |
119 | 1 | .build(); |
120 | 2 | return new RtContent( |
121 | |
this.entry, this.owner, |
122 | 1 | this.entry.uri() |
123 | 1 | .path("/repos") |
124 | 1 | .path(this.owner.coordinates().user()) |
125 | 1 | .path(this.owner.coordinates().repo()) |
126 | 1 | .path("/readme") |
127 | 1 | .back() |
128 | 1 | .method(Request.GET) |
129 | 1 | .body().set(json).back() |
130 | 1 | .fetch() |
131 | 1 | .as(RestResponse.class) |
132 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
133 | 1 | .as(JsonResponse.class) |
134 | 1 | .json().readObject().getString("path") |
135 | |
); |
136 | |
} |
137 | |
|
138 | |
@Override |
139 | |
public Content create( |
140 | |
final JsonObject content |
141 | |
) |
142 | |
throws IOException { |
143 | 2 | if (!content.containsKey("path")) { |
144 | 0 | throw new IllegalStateException( |
145 | |
"Content should have path parameter" |
146 | |
); |
147 | |
} |
148 | 1 | final String path = content.getString("path"); |
149 | 2 | return new RtContent( |
150 | |
this.entry, this.owner, |
151 | 1 | this.request.method(Request.PUT) |
152 | 1 | .uri().path(path).back() |
153 | 1 | .body().set(content).back() |
154 | 1 | .fetch() |
155 | 1 | .as(RestResponse.class) |
156 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
157 | 1 | .as(JsonResponse.class) |
158 | 1 | .json().readObject().getJsonObject("content").getString("path") |
159 | |
); |
160 | |
} |
161 | |
|
162 | |
@Override |
163 | |
public Content get( |
164 | |
final String path, |
165 | |
final String ref |
166 | |
) throws IOException { |
167 | 6 | return this.content(path, ref); |
168 | |
} |
169 | |
|
170 | |
@Override |
171 | |
public Content get( |
172 | |
final String path |
173 | |
) throws IOException { |
174 | 0 | return this.content(path, "master"); |
175 | |
} |
176 | |
|
177 | |
@Override |
178 | |
public Iterable<Content> iterate( |
179 | |
final String path, |
180 | |
final String ref |
181 | |
) { |
182 | 3 | return new RtPagination<Content>( |
183 | 1 | this.request.method(Request.GET) |
184 | 1 | .uri().path(path).queryParam("ref", ref).back(), |
185 | 3 | new RtValuePagination.Mapping<Content, JsonObject>() { |
186 | |
@Override |
187 | |
public Content map(final JsonObject object) { |
188 | 4 | return new RtContent( |
189 | 2 | RtContents.this.entry, RtContents.this.owner, |
190 | 2 | object.getString("path") |
191 | |
); |
192 | |
}; |
193 | |
} |
194 | |
); |
195 | |
} |
196 | |
|
197 | |
@Override |
198 | |
public RepoCommit remove(final JsonObject content |
199 | |
) |
200 | |
throws IOException { |
201 | 2 | if (!content.containsKey("path")) { |
202 | 0 | throw new IllegalStateException( |
203 | |
"Content should have path parameter" |
204 | |
); |
205 | |
} |
206 | 1 | final String path = content.getString("path"); |
207 | 2 | return new RtRepoCommit( |
208 | |
this.entry, |
209 | |
this.owner, |
210 | 1 | this.request.method(Request.DELETE) |
211 | 1 | .uri().path(path).back() |
212 | 1 | .body().set(content).back().fetch() |
213 | 1 | .as(RestResponse.class) |
214 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
215 | 1 | .as(JsonResponse.class).json() |
216 | 1 | .readObject().getJsonObject("commit").getString("sha") |
217 | |
); |
218 | |
} |
219 | |
|
220 | |
@Override |
221 | |
public RepoCommit update( |
222 | |
final String path, |
223 | |
final JsonObject json) |
224 | |
throws IOException { |
225 | 3 | return new RtRepoCommit( |
226 | |
this.entry, |
227 | |
this.owner, |
228 | 1 | this.request.uri().path(path).back() |
229 | 1 | .method(Request.PUT) |
230 | 1 | .body().set(json).back() |
231 | 1 | .fetch() |
232 | 1 | .as(RestResponse.class) |
233 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
234 | 1 | .as(JsonResponse.class).json() |
235 | 1 | .readObject().getJsonObject("commit").getString("sha") |
236 | |
); |
237 | |
} |
238 | |
|
239 | |
@Override |
240 | |
public boolean exists(final String path, final String ref) |
241 | |
throws IOException { |
242 | 0 | final RestResponse response = this.request.method(Request.GET) |
243 | 0 | .uri().path(path).queryParam("ref", ref).back() |
244 | 0 | .fetch().as(RestResponse.class); |
245 | 0 | return response.status() == HttpURLConnection.HTTP_OK; |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
private Content content( |
257 | |
final String path, final String ref |
258 | |
) throws IOException { |
259 | 3 | final String name = "ref"; |
260 | 3 | RtContent content = null; |
261 | 3 | final JsonStructure structure = this.request.method(Request.GET) |
262 | 3 | .uri().path(path).queryParam(name, ref).back() |
263 | 3 | .fetch() |
264 | 3 | .as(RestResponse.class) |
265 | 3 | .assertStatus(HttpURLConnection.HTTP_OK) |
266 | 3 | .as(JsonResponse.class) |
267 | 3 | .json().read(); |
268 | 3 | if (JsonValue.ValueType.OBJECT.equals(structure.getValueType())) { |
269 | 3 | content = new RtContent( |
270 | 3 | this.entry.uri().queryParam(name, ref).back(), this.owner, |
271 | 3 | ((JsonObject) structure).getString("path") |
272 | |
); |
273 | |
} |
274 | 3 | return content; |
275 | |
} |
276 | |
|
277 | |
} |