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 javax.json.Json; |
40 | |
import javax.json.JsonArrayBuilder; |
41 | |
import javax.json.JsonObject; |
42 | |
import javax.json.JsonStructure; |
43 | |
import lombok.EqualsAndHashCode; |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
@Immutable |
53 | |
@Loggable(Loggable.DEBUG) |
54 | 0 | @EqualsAndHashCode(of = "entry") |
55 | 0 | final class RtIssueLabels implements IssueLabels { |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
private final transient Request entry; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
private final transient Request request; |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
private final transient Issue owner; |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | 1 | RtIssueLabels(final Request req, final Issue issue) { |
78 | 1 | this.owner = issue; |
79 | 1 | final Coordinates coords = issue.repo().coordinates(); |
80 | 1 | this.entry = req; |
81 | 1 | this.request = req.uri() |
82 | 1 | .path("/repos") |
83 | 1 | .path(coords.user()) |
84 | 1 | .path(coords.repo()) |
85 | 1 | .path("/issues") |
86 | 1 | .path(Integer.toString(issue.number())) |
87 | 1 | .path("/labels") |
88 | 1 | .back(); |
89 | 1 | } |
90 | |
|
91 | |
@Override |
92 | |
public String toString() { |
93 | 0 | return this.request.uri().get().toString(); |
94 | |
} |
95 | |
|
96 | |
@Override |
97 | |
public Issue issue() { |
98 | 0 | return this.owner; |
99 | |
} |
100 | |
|
101 | |
@Override |
102 | |
public void add(final Iterable<String> labels) throws IOException { |
103 | 0 | JsonArrayBuilder builder = Json.createArrayBuilder(); |
104 | 0 | for (final String label : labels) { |
105 | 0 | builder = builder.add(label); |
106 | 0 | } |
107 | 0 | final JsonStructure json = builder.build(); |
108 | 0 | this.request.method(Request.POST) |
109 | 0 | .body().set(json).back() |
110 | 0 | .fetch() |
111 | 0 | .as(RestResponse.class) |
112 | 0 | .assertStatus(HttpURLConnection.HTTP_OK) |
113 | 0 | .as(JsonResponse.class) |
114 | 0 | .json().readArray(); |
115 | 0 | } |
116 | |
|
117 | |
@Override |
118 | |
public void replace(final Iterable<String> labels) throws IOException { |
119 | 0 | JsonArrayBuilder builder = Json.createArrayBuilder(); |
120 | 0 | for (final String label : labels) { |
121 | 0 | builder = builder.add(label); |
122 | 0 | } |
123 | 0 | final JsonStructure json = builder.build(); |
124 | 0 | this.request.method(Request.PUT) |
125 | 0 | .body().set(json).back() |
126 | 0 | .fetch() |
127 | 0 | .as(RestResponse.class) |
128 | 0 | .assertStatus(HttpURLConnection.HTTP_OK) |
129 | 0 | .as(JsonResponse.class) |
130 | 0 | .json().readArray(); |
131 | 0 | } |
132 | |
|
133 | |
@Override |
134 | |
public void remove(final String name) throws IOException { |
135 | 0 | this.request.method(Request.DELETE) |
136 | 0 | .uri().path(name).back() |
137 | 0 | .fetch() |
138 | 0 | .as(RestResponse.class) |
139 | 0 | .assertStatus(HttpURLConnection.HTTP_OK); |
140 | 0 | } |
141 | |
|
142 | |
@Override |
143 | |
public void clear() throws IOException { |
144 | 0 | this.request.method(Request.DELETE) |
145 | 0 | .fetch() |
146 | 0 | .as(RestResponse.class) |
147 | 0 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
148 | 0 | } |
149 | |
|
150 | |
@Override |
151 | |
public Iterable<Label> iterate() { |
152 | 0 | return new RtPagination<Label>( |
153 | |
this.request, |
154 | 0 | new RtValuePagination.Mapping<Label, JsonObject>() { |
155 | |
@Override |
156 | |
public Label map(final JsonObject object) { |
157 | 0 | return new RtLabel( |
158 | 0 | RtIssueLabels.this.entry, |
159 | 0 | RtIssueLabels.this.owner.repo(), |
160 | 0 | object.getString("name") |
161 | |
); |
162 | |
} |
163 | |
} |
164 | |
); |
165 | |
} |
166 | |
|
167 | |
} |