1 | 4 | |
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; |
36 | |
import com.jcabi.http.response.JsonResponse; |
37 | |
import com.jcabi.http.response.RestResponse; |
38 | |
import java.io.IOException; |
39 | |
import java.net.HttpURLConnection; |
40 | |
import java.net.URI; |
41 | |
import javax.json.Json; |
42 | |
import javax.json.JsonObject; |
43 | |
import javax.json.JsonObjectBuilder; |
44 | |
import lombok.EqualsAndHashCode; |
45 | |
import org.hamcrest.Matchers; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Immutable |
56 | |
@Loggable(Loggable.DEBUG) |
57 | 0 | @EqualsAndHashCode(of = { "ghub", "request" }) |
58 | |
@SuppressWarnings("PMD.TooManyMethods") |
59 | |
final class RtGist implements Gist { |
60 | |
|
61 | |
|
62 | |
|
63 | |
private final transient Request request; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private final transient Github ghub; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private final transient Request entry; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
private final transient String gist; |
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 13 | RtGist(final Github github, final Request req, final String name) { |
87 | 13 | this.ghub = github; |
88 | 12 | this.entry = req; |
89 | 12 | this.gist = name; |
90 | 13 | this.request = req.uri().path("/gists").path(name).back(); |
91 | 12 | } |
92 | |
|
93 | |
@Override |
94 | |
public String toString() { |
95 | 1 | return this.request.uri().get().toString(); |
96 | |
} |
97 | |
|
98 | |
@Override |
99 | |
public Github github() { |
100 | 0 | return this.ghub; |
101 | |
} |
102 | |
|
103 | |
@Override |
104 | |
public String identifier() { |
105 | 2 | return this.gist; |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | |
public String read(final String file) throws IOException { |
110 | 6 | final Response response = this.request.fetch(); |
111 | 3 | final String url = response |
112 | 3 | .as(RestResponse.class) |
113 | 3 | .assertStatus(HttpURLConnection.HTTP_OK) |
114 | 3 | .as(JsonResponse.class) |
115 | 3 | .json().readObject().getJsonObject("files") |
116 | 3 | .getJsonObject(file).getString("raw_url"); |
117 | 6 | return response |
118 | 3 | .as(RestResponse.class) |
119 | 3 | .jump(URI.create(url)) |
120 | 3 | .fetch() |
121 | 3 | .as(RestResponse.class) |
122 | 3 | .assertStatus(HttpURLConnection.HTTP_OK) |
123 | 3 | .body(); |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public void write( |
128 | |
final String file, |
129 | |
final String content) |
130 | |
throws IOException { |
131 | 2 | final JsonObjectBuilder builder = Json.createObjectBuilder() |
132 | 1 | .add("content", content); |
133 | 1 | final JsonObject json = Json.createObjectBuilder() |
134 | 1 | .add("files", Json.createObjectBuilder().add(file, builder)) |
135 | 1 | .build(); |
136 | 1 | this.patch(json); |
137 | 1 | } |
138 | |
|
139 | |
@Override |
140 | |
public void star() throws IOException { |
141 | 0 | this.request.uri().path("star").back() |
142 | 0 | .method("PUT") |
143 | 0 | .fetch().as(RestResponse.class) |
144 | 0 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
145 | 0 | } |
146 | |
|
147 | |
@Override |
148 | |
public void unstar() throws IOException { |
149 | 2 | this.request.uri().path("star").back() |
150 | 1 | .method(Request.DELETE) |
151 | 1 | .fetch().as(RestResponse.class) |
152 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
153 | 1 | } |
154 | |
|
155 | |
@Override |
156 | |
public boolean starred() throws IOException { |
157 | 0 | return this.request.uri().path("star").back() |
158 | 0 | .method("GET").fetch() |
159 | 0 | .as(RestResponse.class).assertStatus( |
160 | 0 | Matchers.isOneOf( |
161 | 0 | HttpURLConnection.HTTP_NO_CONTENT, |
162 | 0 | HttpURLConnection.HTTP_NOT_FOUND |
163 | |
) |
164 | 0 | ).status() == HttpURLConnection.HTTP_NO_CONTENT; |
165 | |
} |
166 | |
|
167 | |
@Override |
168 | |
public Gist fork() throws IOException { |
169 | 3 | return new RtGist( |
170 | |
this.ghub, this.entry, |
171 | 1 | this.request.uri().path("/forks").back() |
172 | 1 | .method(Request.POST) |
173 | 1 | .fetch().as(RestResponse.class) |
174 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
175 | 1 | .as(JsonResponse.class) |
176 | 1 | .json().readObject().getString("id") |
177 | |
); |
178 | |
} |
179 | |
|
180 | |
@Override |
181 | |
public JsonObject json() throws IOException { |
182 | 2 | return new RtJson(this.request).fetch(); |
183 | |
} |
184 | |
|
185 | |
@Override |
186 | |
public GistComments comments() throws IOException { |
187 | 0 | return new RtGistComments(this.entry, this); |
188 | |
} |
189 | |
|
190 | |
@Override |
191 | |
public void patch(final JsonObject json) throws IOException { |
192 | 4 | new RtJson(this.request).patch(json); |
193 | 2 | } |
194 | |
} |