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.ArrayList; |
40 | |
import java.util.Collection; |
41 | |
import java.util.List; |
42 | |
import javax.json.Json; |
43 | |
import javax.json.JsonArrayBuilder; |
44 | |
import javax.json.JsonObject; |
45 | |
import lombok.EqualsAndHashCode; |
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
@Immutable |
54 | |
@Loggable(Loggable.DEBUG) |
55 | 0 | @EqualsAndHashCode(of = "request") |
56 | |
final class RtUserEmails implements UserEmails { |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
private final transient Request request; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | 4 | RtUserEmails(final Request req) { |
68 | 4 | this.request = req.header("Accept", "application/vnd.github.v3") |
69 | 4 | .uri().path("/user/emails").back(); |
70 | 4 | } |
71 | |
|
72 | |
@Override |
73 | |
public Iterable<String> iterate() throws IOException { |
74 | 2 | final List<JsonObject> array = this.request.method(Request.GET) |
75 | 1 | .fetch().as(RestResponse.class) |
76 | 1 | .assertStatus(HttpURLConnection.HTTP_OK) |
77 | 1 | .as(JsonResponse.class) |
78 | 1 | .json().readArray().getValuesAs(JsonObject.class); |
79 | 1 | final Collection<String> emails = new ArrayList<String>(array.size()); |
80 | 1 | for (final JsonObject obj : array) { |
81 | |
|
82 | 1 | emails.add(obj.getString("email")); |
83 | 1 | } |
84 | 1 | return emails; |
85 | |
} |
86 | |
|
87 | |
@Override |
88 | |
public Iterable<String> add( |
89 | |
final Iterable<String> emails |
90 | |
) throws IOException { |
91 | 2 | final JsonArrayBuilder json = Json.createArrayBuilder(); |
92 | 1 | for (final String email : emails) { |
93 | 1 | json.add(email); |
94 | 1 | } |
95 | 1 | final List<JsonObject> array = this.request.method(Request.POST) |
96 | 1 | .body().set(json.build()).back() |
97 | 1 | .fetch().as(RestResponse.class) |
98 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
99 | 1 | .as(JsonResponse.class) |
100 | 1 | .json().readArray().getValuesAs(JsonObject.class); |
101 | 1 | final Collection<String> result = new ArrayList<String>(array.size()); |
102 | 1 | for (final JsonObject obj : array) { |
103 | 1 | result.add(obj.getString("email")); |
104 | 1 | } |
105 | 1 | return result; |
106 | |
} |
107 | |
|
108 | |
@Override |
109 | |
public void remove(final Iterable<String> emails) throws IOException { |
110 | 2 | final JsonArrayBuilder json = Json.createArrayBuilder(); |
111 | 1 | for (final String email : emails) { |
112 | 1 | json.add(email); |
113 | 1 | } |
114 | 1 | this.request.method(Request.DELETE) |
115 | 1 | .body().set(json.build()).back() |
116 | 1 | .fetch().as(RestResponse.class) |
117 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
118 | 1 | } |
119 | |
|
120 | |
@Override |
121 | |
public String toString() { |
122 | 0 | return this.request.uri().get().toString(); |
123 | |
} |
124 | |
|
125 | |
@Override |
126 | |
public JsonObject json() throws IOException { |
127 | 0 | return new RtJson(this.request).fetch(); |
128 | |
} |
129 | |
|
130 | |
} |