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.JsonResponse; |
36 | |
import com.jcabi.http.response.RestResponse; |
37 | |
import java.io.IOException; |
38 | |
import java.net.HttpURLConnection; |
39 | |
import java.util.EnumMap; |
40 | |
import java.util.HashMap; |
41 | |
import java.util.Map; |
42 | |
import javax.json.Json; |
43 | |
import javax.json.JsonObject; |
44 | |
import javax.json.JsonStructure; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
@Immutable |
56 | |
@Loggable(Loggable.DEBUG) |
57 | 0 | @EqualsAndHashCode(of = { "entry", "request", "owner" }) |
58 | |
final class RtIssues implements Issues { |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private final transient Request entry; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private final transient Request request; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private final transient Repo owner; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | 6 | RtIssues(final Request req, final Repo repo) { |
81 | 6 | this.entry = req; |
82 | 6 | final Coordinates coords = repo.coordinates(); |
83 | 6 | this.request = this.entry.uri() |
84 | 6 | .path("/repos") |
85 | 6 | .path(coords.user()) |
86 | 6 | .path(coords.repo()) |
87 | 6 | .path("/issues") |
88 | 6 | .back(); |
89 | 6 | this.owner = repo; |
90 | 6 | } |
91 | |
|
92 | |
@Override |
93 | |
public String toString() { |
94 | 0 | return this.request.uri().get().toString(); |
95 | |
} |
96 | |
|
97 | |
@Override |
98 | |
public Repo repo() { |
99 | 0 | return this.owner; |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public Issue get(final int number) { |
104 | 14 | return new RtIssue(this.entry, this.owner, number); |
105 | |
} |
106 | |
|
107 | |
@Override |
108 | |
public Issue create( |
109 | |
final String title, |
110 | |
final String body) |
111 | |
throws IOException { |
112 | 2 | final JsonStructure json = Json.createObjectBuilder() |
113 | 1 | .add("title", title) |
114 | 1 | .add("body", body) |
115 | 1 | .build(); |
116 | 2 | return this.get( |
117 | 1 | this.request.method(Request.POST) |
118 | 1 | .body().set(json).back() |
119 | 1 | .fetch().as(RestResponse.class) |
120 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
121 | 1 | .as(JsonResponse.class) |
122 | 1 | .json().readObject().getInt("number") |
123 | |
); |
124 | |
} |
125 | |
|
126 | |
@Override |
127 | |
public Iterable<Issue> iterate( |
128 | |
final Map<String, String> params) { |
129 | 6 | return new RtPagination<Issue>( |
130 | 2 | this.request.uri().queryParams(params).back(), |
131 | 6 | new RtValuePagination.Mapping<Issue, JsonObject>() { |
132 | |
@Override |
133 | |
public Issue map(final JsonObject object) { |
134 | 4 | return RtIssues.this.get(object.getInt("number")); |
135 | |
} |
136 | |
} |
137 | |
); |
138 | |
} |
139 | |
|
140 | |
@Override |
141 | |
@SuppressWarnings("PMD.UseConcurrentHashMap") |
142 | |
public Iterable<Issue> search( |
143 | |
final Sort sort, |
144 | |
final Search.Order direction, |
145 | |
final EnumMap<Qualifier, String> qualifiers) |
146 | |
throws IOException { |
147 | 2 | final Map<String, String> params = new HashMap<String, String>(); |
148 | 2 | for (final EnumMap.Entry<Qualifier, String> pair : qualifiers |
149 | 1 | .entrySet()) { |
150 | 0 | params.put(pair.getKey().identifier(), pair.getValue()); |
151 | 0 | } |
152 | 1 | params.put("sort", sort.identifier()); |
153 | 1 | params.put("direction", direction.identifier()); |
154 | 1 | return this.iterate(params); |
155 | |
} |
156 | |
} |