Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
RtPublicKeys |
|
| 1.0;1 | ||||
RtPublicKeys$1 |
|
| 1.0;1 | ||||
RtPublicKeys$AjcClosure1 |
|
| 1.0;1 | ||||
RtPublicKeys$AjcClosure3 |
|
| 1.0;1 | ||||
RtPublicKeys$AjcClosure5 |
|
| 1.0;1 | ||||
RtPublicKeys$AjcClosure7 |
|
| 1.0;1 | ||||
RtPublicKeys$AjcClosure9 |
|
| 1.0;1 |
1 | 2 | /** |
2 | * Copyright (c) 2013-2017, jcabi.com | |
3 | * All rights reserved. | |
4 | * | |
5 | * Redistribution and use in source and binary forms, with or without | |
6 | * modification, are permitted provided that the following conditions | |
7 | * are met: 1) Redistributions of source code must retain the above | |
8 | * copyright notice, this list of conditions and the following | |
9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
10 | * copyright notice, this list of conditions and the following | |
11 | * disclaimer in the documentation and/or other materials provided | |
12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
13 | * the names of its contributors may be used to endorse or promote | |
14 | * products derived from this software without specific prior written | |
15 | * permission. | |
16 | * | |
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
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.JsonObject; | |
41 | import lombok.EqualsAndHashCode; | |
42 | ||
43 | /** | |
44 | * Github public keys. | |
45 | * | |
46 | * @author Carlos Miranda (miranda.cma@gmail.com) | |
47 | * @version $Id: d367f9da16d41c95ec9fb6be0de0d5feb5164504 $ | |
48 | * @see <a href="http://developer.github.com/v3/users/keys/">Public Keys API</a> | |
49 | * @checkstyle MultipleStringLiteralsCheck (200 lines) | |
50 | */ | |
51 | @Immutable | |
52 | @Loggable(Loggable.DEBUG) | |
53 | 0 | @EqualsAndHashCode(of = { "entry", "request", "owner" }) |
54 | final class RtPublicKeys implements PublicKeys { | |
55 | ||
56 | /** | |
57 | * API entry point. | |
58 | */ | |
59 | private final transient Request entry; | |
60 | ||
61 | /** | |
62 | * RESTful request. | |
63 | */ | |
64 | private final transient Request request; | |
65 | ||
66 | /** | |
67 | * User we're in. | |
68 | */ | |
69 | private final transient User owner; | |
70 | ||
71 | /** | |
72 | * Public ctor. | |
73 | * | |
74 | * @param req Request | |
75 | * @param user User | |
76 | */ | |
77 | 4 | public RtPublicKeys(final Request req, final User user) { |
78 | 4 | this.entry = req; |
79 | 4 | this.owner = user; |
80 | 4 | this.request = this.entry.uri().path("/user").path("/keys").back(); |
81 | 4 | } |
82 | ||
83 | @Override | |
84 | public User user() { | |
85 | 0 | return this.owner; |
86 | } | |
87 | ||
88 | @Override | |
89 | public Iterable<PublicKey> iterate() { | |
90 | 2 | return new RtPagination<PublicKey>( |
91 | this.request, | |
92 | 3 | new RtValuePagination.Mapping<PublicKey, JsonObject>() { |
93 | @Override | |
94 | public PublicKey map(final JsonObject object) { | |
95 | 2 | return RtPublicKeys.this.get(object.getInt("id")); |
96 | } | |
97 | } | |
98 | ); | |
99 | } | |
100 | ||
101 | @Override | |
102 | public PublicKey create( | |
103 | final String title, | |
104 | final String key | |
105 | ) throws IOException { | |
106 | 3 | return this.get( |
107 | 1 | this.request.method(Request.POST) |
108 | 2 | .body().set( |
109 | 1 | Json.createObjectBuilder() |
110 | 1 | .add("title", title) |
111 | 1 | .add("key", key) |
112 | 1 | .build() |
113 | 1 | ).back() |
114 | 1 | .fetch().as(RestResponse.class) |
115 | 1 | .assertStatus(HttpURLConnection.HTTP_CREATED) |
116 | 1 | .as(JsonResponse.class) |
117 | 1 | .json().readObject().getInt("id") |
118 | ); | |
119 | } | |
120 | ||
121 | @Override | |
122 | public PublicKey get(final int number) { | |
123 | 8 | return new RtPublicKey(this.entry, this.owner, number); |
124 | } | |
125 | ||
126 | @Override | |
127 | public void remove(final int number) throws IOException { | |
128 | 2 | this.request.method(Request.DELETE) |
129 | 1 | .uri().path(Integer.toString(number)).back() |
130 | 1 | .fetch() |
131 | 1 | .as(RestResponse.class) |
132 | 1 | .assertStatus(HttpURLConnection.HTTP_NO_CONTENT); |
133 | 1 | } |
134 | ||
135 | @Override | |
136 | public String toString() { | |
137 | 0 | return this.request.uri().get().toString(); |
138 | } | |
139 | ||
140 | } |