1 | 0 | |
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 java.util.Map; |
40 | |
import javax.json.Json; |
41 | |
import javax.json.JsonObject; |
42 | |
import javax.json.JsonObjectBuilder; |
43 | |
import javax.json.JsonStructure; |
44 | |
import lombok.EqualsAndHashCode; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
@Immutable |
55 | |
@Loggable(Loggable.DEBUG) |
56 | 0 | @EqualsAndHashCode(of = { "ghub", "request" }) |
57 | |
final class RtGists implements Gists { |
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
private final transient Request entry; |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
private final transient Github ghub; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
private final transient Request request; |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | 5 | RtGists(final Github github, final Request req) { |
80 | 5 | this.entry = req; |
81 | 5 | this.ghub = github; |
82 | 5 | this.request = this.entry.uri().path("/gists").back(); |
83 | 5 | } |
84 | |
|
85 | |
@Override |
86 | |
public String toString() { |
87 | 0 | return this.request.uri().get().toString(); |
88 | |
} |
89 | |
|
90 | |
@Override |
91 | |
public Github github() { |
92 | 0 | return this.ghub; |
93 | |
} |
94 | |
|
95 | |
@Override |
96 | |
public Gist create(final Map<String, String> files, final boolean visible |
97 | |
) throws IOException { |
98 | 2 | JsonObjectBuilder builder = Json.createObjectBuilder(); |
99 | 1 | for (final Map.Entry<String, String> file : files.entrySet()) { |
100 | 2 | builder = builder.add( |
101 | 1 | file.getKey(), |
102 | 1 | Json.createObjectBuilder().add("content", file.getValue()) |
103 | |
); |
104 | 1 | } |
105 | 1 | final JsonStructure json = Json.createObjectBuilder() |
106 | 1 | .add("files", builder) |
107 | 1 | .add("public", visible) |
108 | 1 | .build(); |
109 | 2 | return this.get( |
110 | 1 | this.request.method(Request.POST) |
111 | 1 | .body().set(json).back() |
112 | 1 | .fetch().as(RestResponse.class) |
113 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
114 | 1 | .as(JsonResponse.class) |
115 | 1 | .json().readObject().getString("id") |
116 | |
); |
117 | |
} |
118 | |
|
119 | |
@Override |
120 | |
public Gist get(final String name) { |
121 | 6 | return new RtGist(this.ghub, this.entry, name); |
122 | |
} |
123 | |
|
124 | |
@Override |
125 | |
public Iterable<Gist> iterate() { |
126 | 2 | return new RtPagination<Gist>( |
127 | |
this.request, |
128 | 2 | new RtValuePagination.Mapping<Gist, JsonObject>() { |
129 | |
@Override |
130 | |
public Gist map(final JsonObject object) { |
131 | 1 | return RtGists.this.get(object.getString("id")); |
132 | |
} |
133 | |
} |
134 | |
); |
135 | |
} |
136 | |
|
137 | |
@Override |
138 | |
public void remove( |
139 | |
final String identifier |
140 | |
) throws IOException { |
141 | 2 | this.request.method(Request.DELETE) |
142 | 1 | .uri().path(identifier).back() |
143 | 1 | .fetch() |
144 | 1 | .as(RestResponse.class) |
145 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
146 | 1 | } |
147 | |
} |